Stata/Python integration part 2: Three ways to use Python in Stata
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.
. python ----------------------------------------------- python (type end to exit) ------
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.
>>> print("Hello Stata, I am Python") Hello Stata, I am Python
You can also use Python as an interactive calculator.
>>> 2*3 6 >>> 3*4 12
Then, you can type end to exit Python and return to Stata.
>>> end --------------------------------------------------------------------------------
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:
. python: print("Hello Stata, I am Python") Hello Stata, I am Python
You can also use this syntax for quick calculations:
. python: 2*3 6
Python in do-files and ado-files
You can use python: followed by a simple command such as
. python: print("Hello Stata, I am Python")
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
display "Hello Python, I am Stata." python print("Hello Stata.") print("I am Python.") end display "Nice to meet you Python!"
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.
. display "Hello Python, I am Stata." Hello Python, I am Stata. . python ----------------------------------------------- python (type end to exit) ------ >>> print("Hello Stata.") Hello Stata. >>> print("I am Python.") I am Python. >>> end -------------------------------------------------------------------------------- . display "Nice to meet you Python!" Nice to meet you Python!
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
display "Hello Python, I am Stata." python: print("Hello Stata.") print("I am Python.") end display "Nice to meet you Python!"
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.
. display "Hello Python, I am Stata." Hello Python, I am Stata. . python: ----------------------------------------------- python (type end to exit) ------ >>> print("Hello Stata.") File "", line 1 print("Hello Stata.") ^ IndentationError: unexpected indent (1 line skipped) -------------------------------------------------------------------------------- r(7102);
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
print("Hello Stata.") print("I am Python.")
We can run the Python script by typing python script followed by the name of the script we wish to run.
. python script hello.py Hello Stata. I am Python.
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.