Saturday, November 29, 2025

Stata/Python integration half 2: 3 ways to make use of Python in Stata


In my final put up, I confirmed you the best way to set up Python and arrange Stata to make use of Python. Now, we’re prepared to make use of Python. There are 3 ways to make use of Python inside Stata: calling Python interactively, together with Python code in do-files and ado-files, and executing Python script recordsdata. Every is helpful in several circumstances, so I’ll display all three. The examples are deliberately easy and considerably foolish. I’ll present you some extra complicated examples in future posts, however I need to hold issues easy on this put up.

Calling Python interactively

You should use Python interactively inside Stata by typing python in Stata’s Command window.

. python
----------------------------------------------- python (kind finish to exit) ------

Stata reminds you that you would be able to kind finish to exit Python if you end up completed. Now you can kind Python code within the Command window. For instance, you would instruct Python to print a phrase.

>>> print("Good day Stata, I'm Python")
Good day Stata, I'm Python

You can even use Python as an interactive calculator.

>>> 2*3
6
>>> 3*4
12

Then, you may kind finish to exit Python and return to Stata.

>>> finish
--------------------------------------------------------------------------------

You can even merely kind python: adopted by a Python assertion. Python will run the assertion after which return to Stata. Right here is an easy instance:

. python: print("Good day Stata, I'm Python")
Good day Stata, I'm Python

You can even use this syntax for fast calculations:

. python: 2*3
6

Python in do-files and ado-files

You should use python: adopted by a easy command reminiscent of

. python: print("Good day Stata, I'm Python")

in do-files and ado recordsdata, simply as you may interactively.

You can even use Python code blocks to run a number of Python statements inside do-files and ado-files. A Python code block begins with python and ends with finish, as we noticed within the interactive instance above. Instance 1 beneath is a do-file that begins with a Stata show command, begins a Python code block with python, executes two Python print() statements, ends the Python code block with finish, and finishes with a Stata show command.

Instance 1: good day.do

show "Good day Python, I'm Stata."
python
print("Good day Stata.")
print("I'm Python.")
finish
show "Good to satisfy you Python!"

Once we kind do good day, it’s simple to see the Python code block within the output as a result of the code block begins and ends with rows of dashed traces.

. show "Good day Python, I'm Stata."
Good day Python, I'm Stata.

. python
----------------------------------------------- python (kind finish to exit) ------
>>> print("Good day Stata.")
Good day Stata.
>>> print("I'm Python.")
I'm Python.
>>> finish
--------------------------------------------------------------------------------

. show "Good to satisfy you Python!"
Good to satisfy you Python!

There are two vital issues to find out about code blocks. First, you may start a code block with python or with python:. Each will run the code block, however they behave in a different way if Python encounters an error throughout the code block. In case you start the code block with python and Python encounters an error, Python will proceed to execute the remaining code within the code block. In case you start the code block with python: and Python encounters an error, Python will return management to Stata with out executing the remaining Python code. A stack hint might be printed, and an error code might be issued in each circumstances.

Second, indentation inside Python code blocks is practical fairly than aesthetic. For instance, you could be tempted to jot down a Python code block and indent the code throughout the block as in Instance 2.

Instance 2: hello2.do

show "Good day Python, I'm Stata."
python:
    print("Good day Stata.")
    print("I'm Python.")
finish
show "Good to satisfy you Python!"

However Python makes use of indentation to “decide the grouping of statements” (Python Documentation 2.1.7). Python returns an indentation error once we run Instance 2.

. show "Good day Python, I'm Stata."
Good day Python, I'm Stata.

. python:
----------------------------------------------- python (kind finish to exit) ------
>>>     print("Good day Stata.")
  File "", line 1
    print("Good day Stata.")
    ^
IndentationError: surprising indent
(1 line skipped)
--------------------------------------------------------------------------------
r(7102);

You may learn extra about the usage of indentation within the Python documentation.

Working Python scripts

You can even run Python scripts inside Stata. A Python script is solely a group of Python statements saved in a file with a .py extension. Instance 3 reveals the contents of a Python script named good day.py.

Instance 3: good day.py

print("Good day Stata.")
print("I'm Python.")

We are able to run the Python script by typing python script adopted by the identify of the script we want to run.

. python script good day.py
Good day Stata.
I'm Python.

This methodology of working Python inside Stata is helpful once you encounter a Python script that’s written by another person. It is usually helpful for saving a group of Python capabilities. I’ll present you the best way to go arguments from Stata to Python scripts in a future weblog put up.

You should use python script within the Command window, do-files, or ado-files.

Conclusion

On this weblog put up, I’ve proven you three alternative ways to run Python inside Stata. Every of them is helpful in several circumstances, and I’ll display their makes use of in future weblog posts. Subsequent time, I’ll present you the best way to obtain and set up Python packages.



Related Articles

Latest Articles