Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages

Quick setup of Python with Stata 16

  • 7

Quick setup of Python with Stata 16

Category:Blog,Stata Programs Tags : 

With the announcement of Stata 16, Python commands can be executed directly from the Stata command prompt, do files or ado programs. That would definitely expand the possibilities of doing extraordinary things without leaving the Stata environment. However, this integration exposes Stata to all the problems of Python installations and its packages.

First of all, Python does not come as part of the Stata installation. Stata depends on the already installed version of Python. That would definitely make a Stata-Python code less portable. One solution might be the portable version of Python. Only time can tell what will work best in such situations.

In this short post, I am going to outline a few basic steps to get started with Python from Stata. These steps are mentioned below:

1.What Version of Python to Install

A number of options are available to install Python. Over the past 12 months, I found that the installation of Python using Anaconda is the least problematic one. And with Stata 16, this again came out true. The stand-alone version of Python did not work with Stata. Each time I tried to type python from the Stata command prompt, the error message generated by Stata was:

initialized          no

What I did was to uninstall the other version of Python and kept only the Anaconda installation.

2. Set the Installation path

Stata can search for any available Python installation, including the installation through Anaconda. To search and associate python with Stata, I typed the following from the Stata command prompt:

python search 
set python_exec  D:\Anaconda\python.exe, permanently

The first line of code finds the directory path and the Python executable file. The second line of code sets which Python version to use. Option permanently would save this path for future use as well. And that’s all.

3. Using Python

Once the above steps go without an error, we are ready to use Python. In the Stata command window, we can enter the Python environment by typing python, and the three greater than familiar symbol will appear on the screen

 . python
 --------- python (type end to exit) ------- 



July 31, 2019at 7:23 am

Please mention the possibility to integrate python with stata 15 or stata 14 versions

    Attaullah Shah

    August 1, 2019at 4:01 am

    Python was integrated into Stata 16, it is not available in other versions. There are several user-written packages which you can search on SSC for python integration with earlier version of Stata.

Daichi Muramastu/ kyoto univ

November 8, 2019at 8:50 am

Very same situation occured. Stata16 did not detect python3.8, which was downloaded from, and reacted ;

. python
minimum Python version required is 2.7

but after uninstalling that python and downloading anaconda3, stata recognized python, and reacted;

. python
———————————————– python (type end to exit) ——————————————————————————————————


November 28, 2019at 9:53 pm

Hi, I appreciated your blog on using python within STATA. I am running python through Anaconda and STATA v16.

I can import numpy in STATA, but when I type “import pandas” I get the following error:

Traceback (most recent call last):
File “”, line 1, in
ImportError: No module named pandas

I checked my Anaconda and pandas is installed and useable from within that environment.

Any ideas on how to fix this?

Thanks in advance

    Attaullah Shah

    November 28, 2019at 9:55 pm

    If pandas are working fine in Anacondas, and Stata is using the Anaconda installation, then there should be no problem. Please check this

    python query

    import sys

    This will tell which python installation is Stata using. If you have installed python elsewhere, then try installing pandas from the command prompt
    pip install pandas


November 29, 2019at 7:36 am

python query
I get the endless …


import sys

>>> import sys
>>> print(sys.path)
['', '/Applications/Stata/ado/base/py', '', '/System/Library/Frameworks/Python.framework/Versions/2.7
> /lib/', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Sys
> tem/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/F
> rameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python
> .framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Pyt
> hon.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Vers
> ions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/pyth
> on2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framewo
> rk/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extra
> s/lib/python/PyObjC', '.', '/Applications/Stata/', '/Applications/Stata/ado/base/', '/Applications/
> Stata/ado/base/py', '/Applications/Stata/ado/site/', '/Applications/Stata/ado/site/py', '/Users/Jon
> athan/Library/Application Support/Stata/ado/plus/', '/Users/Jonathan/Library/Application Support/St
> ata/ado/plus/py', '/Users/Jonathan/Documents/Stata/ado/personal/', '/Users/Jonathan/Documents/Stata
> /ado/personal/py', '~/ado/']

This will tell which python installation is Stata using.

How do you interpret the above? I installed Anaconda 3.7 so not sure if it is finding another version somehow somewhere??)

If you have installed python elsewhere, then try installing pandas from the command prompt
go to cmd and then
pip install pandas

>>> pip install pandas

    Attaullah Shah

    November 29, 2019at 7:47 am

    In the system paths, you have provided, I do not see any anaconda installation path. You can set this path as shown in my blog post. So first search for python in Stata, i.e. not in python prompt, it should be from Stata command

    python search 

    My search return this


    It will give you python installation path, pick the anaconda installation, set it permanently

    set python_exec  D:\Anaconda\python.exe, permanently

    And lastly, the pip install pandas should not be typed from Stata or python prompts, it should be from operating systems’ command prompt, ie. in Windows, it is the cmd, the black screen prompt.

Leave a Reply