Saturday, November 29, 2025

Stata/Python integration half 3: set up Python packages


In my final publish, I confirmed you 3 ways to make use of Python inside Stata. The examples have been easy however they allowed us to begin utilizing Python. At this level, you could possibly write your individual Python applications inside Stata. However the true energy of Python lies within the 1000’s of freely obtainable packages. Right now, I wish to present you methods to obtain and set up Python packages.

Utilizing pip to put in Python packages

Let’s start by typing python question to confirm that Python is put in on our system and that Stata is ready up to make use of Python.

. python question
--------------------------------------------------------------------------------
    Python Settings
      set python_exec      C:UsersChuckStataAppDataLocalProgramsPython>
> Python38python.exe
      set python_userpath  C:UsersChuckStataAppDataLocalProgramsPython>
> Python38

    Python system data
      initialized          sure
      model              3.8.3
      structure         64-bit
      library path         C:UsersChuckStataAppDataLocalProgramsPython
> Python38python38.dll

The outcomes point out that Stata is ready up to make use of Python 3.8, so we’re prepared to put in packages.

NumPy is a well-liked bundle that’s described as “the elemental bundle for scientific computing with Python”. Many different packages depend on NumPy‘s mathematical options, so let’s start by putting in it. It’s attainable that NumPy is already put in on my system, and I can examine by typing python which numpy in Stata.

. python which numpy
Python module numpy not discovered
r(601);

NumPy is just not discovered on my system, so I’m going to put in it. I’m utilizing Home windows 10, so I’ll sort shell in Stata to open a Home windows Command Immediate.

Determine 1: Home windows Command Immediate

shell can even open a terminal in Mac or Linux working techniques. Notice that skilled Stata customers usually sort ! moderately than the phrase shell.

Subsequent, I’ll use a program named pip to put in NumPy. You possibly can sort pip -V within the Home windows Command Immediate or terminal in Mac or Linux to see the model and site of your pip program.

Determine 2: pip model and site
graph1

The trail for pip is similar as the trail returned by python question above. You need to confirm this when you have a number of variations of Python put in in your system.

Subsequent, sort pip set up numpy within the Command Immediate or terminal, and pip will obtain and set up NumPy within the acceptable location in your system.

Determine 3: pip set up numpy
graph1

The output tells us that NumPy was put in efficiently.

We will confirm that NumPy was put in efficiently by once more typing python which numpy

. python which numpy
 PythonPython38libsite-packagesnumpy__init__.py'>

Let’s set up three extra packages that we are going to use sooner or later. Pandas is a well-liked Python bundle used for importing, exporting, and manipulating information. We will set up it by typing pip set up pandas within the Command Immediate.

Determine 4: pip set up pandas
graph1

You possibly can watch a video that demonstrates methods to use pip to put in Pandas on the Stata YouTube channel.

Matplotlib is a well-liked bundle that “is a complete library for creating static, animated, and interactive visualizations in Python”. We will set up it by typing pip set up matplotlib within the Command Immediate.

Determine 5: pip set up matplotlib
graph1

Scikit-learn is a well-liked bundle for machine studying. We will set up it by typing pip set up sklearn within the Command Immediate.

Determine 6: pip set up scikit-learn
graph1

Let’s use python which to confirm that pandas, matplotlib, and scikit-learn are put in.

. python which pandas
 PythonPython38libsite-packagespandas__init__.py'>

. python which matplotlib
 PythonPython38libsite-packagesmatplotlib__init__.py'>

. python which sklearn
 PythonPython38libsite-packagessklearn__init__.py'>

Conclusion

We did it! We efficiently put in 4 of the preferred Python packages utilizing pip. You should use your Web search engine to seek out lots of of different Python packages and set up them with pip. Subsequent time, I’ll present you methods to use packages in Python.



Related Articles

Latest Articles