In my last post, I showed you how to install Python and set up Stata to use Python. Now, we’re ready to use Python. There are three ways to use Python within Stata: calling Python interactively, including Python code in do-files and ado-files, and executing Python script files. Each is useful in different circumstances, so I will demonstrate all three. The examples are intentionally simple and somewhat silly. I’ll show you some more complex examples in future posts, but I want to keep things simple in this post.
Calling Python interactively
You can use Python interactively within Stata by typing python in Stata’s Command window.
Stata reminds you that you can type end to exit Python when you are finished. You can now type Python code in the Command window. For example, you could instruct Python to print a phrase.
You can also use Python as an interactive calculator.
Then, you can type end to exit Python and return to Stata.
You can also simply type python: followed by a Python statement. Python will run the statement and then return to Stata. Here is a simple example:
You can also use this syntax for quick calculations:
Python in do-files and ado-files
You can use python: followed by a simple command such as
in do-files and ado files, just as you can interactively.
You can also use Python code blocks to run multiple Python statements within do-files and ado-files. A Python code block begins with python and ends with end, as we saw in the interactive example above. Example 1 below is a do-file that begins with a Stata display command, begins a Python code block with python, executes two Python print() statements, ends the Python code block with end, and finishes with a Stata display command.
Example 1: hello.do
When we type do hello, it is easy to see the Python code block in the output because the code block begins and ends with rows of dashed lines.
There are two important things to know about code blocks. First, you can begin a code block with python or with python:. Both will run the code block, but they behave differently if Python encounters an error within the code block. If you begin the code block with python and Python encounters an error, Python will continue to execute the remaining code in the code block. If you begin the code block with python: and Python encounters an error, Python will return control to Stata without executing the remaining Python code. A stack trace will be printed, and an error code will be issued in both cases.
Second, indentation within Python code blocks is functional rather than aesthetic. For example, you may be tempted to write a Python code block and indent the code within the block as in Example 2.
Example 2: hello2.do
But Python uses indentation to “determine the grouping of statements” (Python Documentation 2.1.7). Python returns an indentation error when we run Example 2.
You can read more about the use of indentation in the Python documentation.
Running Python scripts
You can also run Python scripts within Stata. A Python script is simply a collection of Python statements saved in a file with a .py extension. Example 3 shows the contents of a Python script named hello.py.
Example 3: hello.py
We can run the Python script by typing python script followed by the name of the script we wish to run.
This method of running Python within Stata is useful when you encounter a Python script that is written by someone else. It is also useful for saving a collection of Python functions. I’ll show you how to pass arguments from Stata to Python scripts in a future blog post.
You can use python script in the Command window, do-files, or ado-files.
Conclusion
In this blog post, I have shown you three different ways to run Python within Stata. Each of them is useful in different circumstances, and I will demonstrate their uses in future blog posts. Next time, I will show you how to download and install Python packages.
近期,武汉大学经济与管理学院联合Stata软件中国授权经销商北京天演融智软件有限公司(科学软件网)在七-八月份举办Stata系列专题讲座以及Stata软件研讨会,得到了大家的一致**。在此,科学软件网为大家提供三场讲座的回放以及“Stata洞察数据科学研讨会” 中李春涛老师主讲的主题为 “中文地图与Stata” 和 肖光恩老师主讲的主题为 “新冠疫情数据的可视化与建模方法” 的演讲回放视频,全部免费,可直接扫码(页面下方直接点 试听 )免费观看。
北京天演融智软件有限公司(科学软件网)是Stata软件在中国的授权经销商,为中国的Stata用户提供优质的软件销售和培训服务。