PyStata integration — Call Python from Stata 13
If you want to use a Python version different from the default, you can type python set exec
to change the setting. For example, on Linux, you can type
python set exec "/usr/local/bin/python"
If python search does not find the Python environment you wanted (for example, a user-created
virtual environment), you can type python set exec to use the version of choice.
Setting the Python version is optional, but if set, it must be done before the initialization of Python.
Otherwise, an error will be issued. The setting will be available only for the current Stata session.
If you want Stata to remember the setting and use that Python version by default the next time you
launch Stata, then use the permanently option:
python set exec "/usr/local/bin/python", permanently
Locating modules
According to the Python documentation (sec. 6.1.2 and sec. 6.1.3), “When a module named spam
is imported, the interpreter first searches for a built-in module with that name. If not found, it then
searches for a file named spam.py in a list of directories given by the variable sys.path.” When
the interpreter is initialized in Stata, Stata’s system directories (sysdir) and a py/ directory within
each system directory, except the STATA directory, are added to the list following the default module
search paths. For example, on a particular Windows computer, the following paths are added:
C:\Program Files\Stata18\
C:\Program Files\Stata18\ado\base\
C:\Program Files\Stata18\ado\base\py\
C:\Program Files\Stata18\ado\site\
C:\Program Files\Stata18\ado\site\py\
C:\ado\plus\
C:\ado\plus\py\
C:\ado\personal\
C:\ado\personal\py\
C:\ado\
C:\ado\py\
If you want to add other paths to the module search path list, you can type python set userpath
to add a list of paths at once. For example,
python set userpath "C:\mymodules1\" "C:\mymodules2\"
By default, those paths are added to the end of the list so that modules in those directories are
searched last. If you want those paths to be searched first, you can specify the prepend option,
which will add those paths to the beginning of the module search path. Paths added in this way will
be kept in the module search path list and be searched for the whole Stata session. This is different
from specifying the userpaths() option with python script, which removes the paths from the
module search path list once the script is executed.
Specifying additional module search paths is optional, but if specified, it must be done before the
initialization of Python. Otherwise, an error will be issued. The setting will be available only for
the current Stata session. If you want Stata to remember the setting and use the additional paths by
default the next time you launch Stata, then use the permanently option.
When you want to import third-party Python modules (such as numpy, pandas, etc.) in your
Python code, you need to make sure that they are already installed in the Python version that you are
currently using. Otherwise, an error will be issued claiming the specified module is not found. You
can type python which to check whether a module is available in your current Python settings.