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 r(7100);
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) ------- >>>2+2 4 >>>end -------------------------------------------
Please mention the possibility to integrate python with stata 15 or stata 14 versions
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.
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
r(111);
but after uninstalling that python and downloading anaconda3, stata recognized python, and reacted;
. python
———————————————– python (type end to exit) ——————————————————————————————————
>>>
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
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
Then
import sys
print(sys.path)
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
python query
I get the endless …
Then
import sys
print(sys.path)
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
…
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
My search return this
D:\Anaconda\python.exe
It will give you python installation path, pick the anaconda installation, set it 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.Hello Iam from Perú ,what is the solution
? Check here https://drive.google.com/file/d/1iLEsmyxjsHyrpo-7P2EkkmGrcTJwqUUM/view?usp=sharing
Andres
I think you are running the python code from a do file. In such a case, you have to provide the end statement at the end of the do file. For example
Dear Dr Attaullah,
I’m having trouble using pandas and numpy in the python environment using stata. I also have python installed using anaconda as you can see from this output:
What can I do to solve this issue?
Thanks
A more detailed version:
. python query
——————————————————————————–
Python Settings
set python_exec C:\Program Files (x86)\Microsoft Visual Studio\Shared
> \Python37_64\python.exe
set python_userpath
Python system information
initialized no
version 3.7.5
architecture 64-bit
library path C:\Program Files (x86)\Microsoft Visual Studio\Shared
> \Python37_64\python37.dll
. python
———————————————– python (type end to exit) ——
>>> import sys
>>> print(sys.path)
[”, ‘C:\\Program Files\\Stata16\\ado\\base\\py’, ”, ‘C:\\Program Files (x86)\\
> Microsoft Visual Studio\\Shared\\Python37_64\\python37.zip’, ‘C:\\Program File
> s (x86)\\Microsoft Visual Studio\\Shared\\Python37_64\\DLLs’, ‘C:\\Program Fil
> es (x86)\\Microsoft Visual Studio\\Shared\\Python37_64\\lib’, ‘C:\\Program Fil
> es\\Stata16’, ‘C:\\Users\\rmhikme\\AppData\\Roaming\\Python\\Python37\\site-pa
> ckages’, ‘C:\\Users\\rmhikme\\AppData\\Roaming\\Python\\Python37\\site-package
> s\\pysurvival-0.2.1-py3.7-win-amd64.egg’, ‘c:\\programdata\\anaconda3\\lib\\si
> te-packages\\sklearn-0.0-py3.7.egg’, ‘c:\\programdata\\anaconda3\\lib\\site-pa
> ckages\\pyarrow-0.17.0-py3.7-win-amd64.egg’, ‘c:\\programdata\\anaconda3\\lib\
> \site-packages\\progressbar-2.5-py3.7.egg’, ‘c:\\programdata\\anaconda3\\lib\\
> site-packages’, ‘C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Pyt
> hon37_64’, ‘C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\Python37
> _64\\lib\\site-packages’, ‘.’, ‘C:\\Program Files\\Stata16\\’, ‘C:\\Program Fi
> les\\Stata16\\ado\\base\\’, ‘C:\\Program Files\\Stata16\\ado\\base\\py’, ‘C:\\
> Program Files\\Stata16\\ado\\site\\’, ‘C:\\Program Files\\Stata16\\ado\\site\\
> py’, ‘C:\\Users\\rmhikme\\ado\\plus\\’, ‘C:\\Users\\rmhikme\\ado\\plus\\py’, ‘
> C:\\Users\\rmhikme\\ado\\personal\\’, ‘C:\\Users\\rmhikme\\ado\\personal\\py’,
> ‘c:\\ado\\’]
you have to write the word ‘end’ at the end of your code if you are doing from a do file.
python
import sys
print(sys.path)
end
Appreciate your feed back.