Quick setup of Python with Stata 16

  • 3

Quick setup of Python with Stata 16


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 python.org, 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) ——————————————————————————————————

Leave a Reply

one + 13 =