Home > Programming > Stata/Python integration part 1: Setting up Stata to use Python

Stata/Python integration part 1: Setting up Stata to use Python

Python integration is one of the most exciting features in Stata 16. There are thousands of free Python packages that you can use to access and process data from the Internet, visualize data, explore data using machine-learning algorithms, and much more. You can use these Python packages interactively within Stata or incorporate Python code into your do-files. And there are a growing number of community-contributed commands that have familiar, Stata-style syntax that use Python packages as the computational engine. But there are a few things that we must do before we can use Python in Stata. This blog post will show you how to set up Stata to use Python.

Download and install Python

You may already have Python installed on your computer. You can check by typing python search in Stata.

. python search

no Python installation found; minimum version required is 2.7.
r(111)

The result tells us that no Python installations were found, so I will need to install Python. You may need to consult your system administrator if you are not the owner or administrator of the computer you are using.

You can download an installation file for Python at the Python download website. Python is available for Linux, Mac, and Windows, so you should select the installation file that corresponds to your operating system. You should select a 64-bit installation file because Stata/Python integration works only with 64-bit processors. Python 3.8.5 is the current version as I am writing this post, and I recommend using the latest version. Older versions of Python are available for download if you need them for backward compatibility, but versions older than Python 3.0 have now reached “end of life” status.

After you download the installation file, simply run it and follow the setup instructions. I have recorded a video that shows how to download and install Python for Windows 10 on a 64-bit computer. You can watch the video on the Stata YouTube Channel.

Download and install Anaconda/Python

Python is also included in an open-source development environment called Anaconda. Many people prefer Anaconda because it automatically installs and manages many Python packages during installation. You can download the appropriate Anaconda installation file at the Anaconda download website. Anaconda is also available for Linux, Mac, and Windows, and you should select the 64-bit installation file that corresponds to your operating system. After you download the installation file, simply run it and follow the setup instructions. I have recorded a video that shows how to download and install Anaconda for Windows 10 on a 64-bit computer. You can watch the video on the Stata YouTube Channel.

Setting up Stata to use Python

After you install Python or Anaconda, or both, on your computer, you can return to Stata and again type python search in the Command window.

. python search
--------------------------------------------------------------------------------
 Python environments found:
 C:\Users\Chuck\AppData\Local\Programs\Python\Python38\python.exe
 C:\Users\Chuck\anaconda3\python.exe
--------------------------------------------------------------------------------

I have installed both Python 3.8 and Anaconda 3.0 on my computer. I can tell Stata which Python installation I wish to use with the set python_exec command. The example below tells Stata that I wish to use Python 3.8.

. set python_exec C:\Users\Chuck\AppData\Local\Programs\Python\Python38\
> python.exe

The default list of search paths for Python modules is stored in a Python system variable named sys.path. The default search paths in Windows will look similar to this:

C:\Program Files\Stata16\
C:\Program Files\Stata16\ado\base\
C:\Program Files\Stata16\ado\base\py\
C:\Program Files\Stata16\ado\site\
C:\Program Files\Stata16\ado\site\py\
C:\ado\plus\C:\ado\plus\py\C:\ado\personal\
C:\ado\personal\py\C:\ado\C:\ado\py\

You can use set python_userpath to set additional paths for Python to look up packages and modules that you create or download. The example below uses set python_userpath to add the folder where I save my personal Python modules.

. set python_userpath C:\Users\Chuck\MyPythonModules\

You can type python query to see which Python installation you are currently using.

. python query
--------------------------------------------------------------------------------
    Python Settings
      set python_exec      C:\Users\Chuck\AppData\Local\Programs\Python\
> Python38\python.exe
      set python_userpath  C:\Users\Chuck\MyPythonModules\

    Python system information
      initialized          no
      version              3.8.3
      architecture         64-bit
      library path         C:\Users\Chuck\AppData\Local\Programs\Python\
> Python38\python38.dll

Now we’re ready to use Python within Stata! You can begin using Python interactively by typing python in the Command window. Stata will remind you that you can type end to exit Python and return to Stata. The example below uses Python to print() the sentence “Hello Stata, I am Python” to the screen.

. python
----------------------------------------------- python (type end to exit) ------
>>> print("Hello Stata, I am Python")
Hello Stata, I am Python
>>> end
--------------------------------------------------------------------------------

It worked! This blog post is the first in a series that I am writing to show you how to use Python within Stata. My goal is to provide a basic foundation so that you will have the skills to explore Python packages that are useful to you. This post demonstrated how to install Python and set up Stata to use Python. In my next post, I will demonstrate several ways to use Python from within Stata.

Categories: Programming Tags: ,