Home  /  Resources & support  /  FAQs  /  Third-party Python packages

How do I use third-party Python packages in the Python environment from within Stata?

Title   Third-party Python packages
Author Bingsheng Zhang, StataCorp

Stata 16 allows the execution of Python code from within Stata using the command python. Both Python 3 and Python 2 (Python 2.7 or later) are supported at this time.

In Python, the import statement is probably the most commonly used way to gain access to other modules. To use third-party Python modules (for example, NumPy, pandas, etc.) in Python from within Stata, one has to make sure these modules have been properly installed. pip and Conda are popular package management systems that run on Windows, Mac, and Linux. They facilitate the installation and management of thousands of Python modules and their dependencies.

Some versions of Python have pip bundled with their installation. For example, Python 2.7.9 and later, as well as Python 3.4 and later include pip by default. If your Python installation does not have pip included, you can find out how to install pip here. Once installed, you can use pip to manage packages using the cmd window on Windows or a terminal application on Mac or Unix.

On Windows systems, you will need to make sure pip has been added to your Path environment variable. Fortunately, there is usually an option with the Python installer that you can select. If you did not select this option during the installation, you will either need to run the installer again or manually add pip to your Path. pip is often located in the Scripts\ directory within the Python installation folder. For example, if your Python is installed in "C:\Program Files\Python36", pip is usually found in "C:\Program Files\Python36\Scripts\". You need to add this path to your Path environment variable. You can get more information about setting paths here.

After you are done, you can test pip with the cmd window or terminal application by typing

      pip -V
to check whether pip is set up correctly. If everything has been set up correctly, you will see information about where pip is installed and its version.

Now you could use pip to install packages. For example, typing

      pip install numpy
would install the numpy package.

Note that if you have multiple versions of Python installed, you will need make sure you are calling the pip that is associated with the version of Python you intend to use.

The output from

     pip -V
contains not only the version of pip, but also the Python version it links to, which indicates where the packages will be installed when the command pip install is used. If multiple Python versions have been installed on your system, you may need to specify the correct version of pip to install packages for the specific Python version. For example, if you have Python 2.7, Python 3.6, and Python 3.7 installed, you can type
    pip2.7 install packagename
    pip3.6 install packagename
    pip3.7 install packagename
to install packages for each Python version.

If your Python environment is created through Anaconda, you can also use conda to install packages. See Getting started with conda for more instructions.