首页 >> 新闻动态 >> 新闻标题

How to install Python packages

2020-09-08


In my last post, I showed you three ways to use Python within Stata. The examples were simple but they allowed us to start using Python. At this point, you could write your own Python programs within Stata. But the real power of Python lies in the thousands of freely available packages. Today, I want to show you how to download and install Python packages.


Using pip to install Python packages

Let’s begin by typing python query to verify that Python is installed on our system and that Stata is set up to use Python.

1.jpg

The results indicate that Stata is set up to use Python 3.8, so we are ready to install packages.

NumPy is a popular package that is described as “the fundamental package for scientific computing with Python”. Many other packages rely on NumPy‘s mathematical features, so let’s begin by installing it. It is possible that NumPy is already installed on my system, and I can check by typing python which numpy in Stata.

2.jpg

NumPy is not found on my system, so I am going to install it. I am using Windows 10, so I will type shell in Stata to open a Windows Command Prompt.

Figure 1: Windows Command Prompt

3.jpg

shell will also open a terminal in Mac or Linux operating systems. Note that experienced Stata users often type ! rather than the word shell.

Next, I will use a program named pip to install NumPy. You can type pip -V in the Windows Command Prompt or terminal in Mac or Linux to see the version and location of your pip program.

Figure 2: pip version and location

4.jpg

The path for pip is the same as the path returned by python query above. You should verify this if you have multiple versions of Python installed on your system.

Next, type pip install numpy in the Command Prompt or terminal, and pip will download and install NumPy in the appropriate location on your system.

Figure 3: pip install numpy

5.jpg

The output tells us that NumPy was installed successfully.

We can verify that NumPy was installed successfully by again typing python which numpy

6.jpg

Let’s install three more packages that we will use in the future. Pandas is a popular Python package used for importing, exporting, and manipulating data. We can install it by typing pip install pandas in the Command Prompt.

Figure 4: pip install pandas

You can watch a video that demonstrates how to use pip to install Pandas on the Stata YouTube channel.7.jpg

Matplotlib is a popular package that “is a comprehensive library for creating static, animated, and interactive visualizations in Python”. We can install it by typing pip install matplotlib in the Command Prompt.

Figure 5: pip install matplotlib

8.jpg

Scikit-learn is a popular package for machine learning. We can install it by typing pip install sklearn in the Command Prompt.

Figure 6: pip install scikit-learn

9.jpg

Let’s use python which to verify that pandas, matplotlib, and scikit-learn are installed.

10.jpg

Conclusion

We did it! We successfully installed four of the most popular Python packages using pip. You can use your Internet search engine to find hundreds of other Python packages and install them with pip. Next time, I will show you how to use packages in Python.

近期,武汉大学经济与管理学院联合Stata软件中国授权经销商北京天演融智软件有限公司(科学软件网)在七-八月份举办Stata系列专题讲座以及Stata软件研讨会,得到了大家的一致好评。在此,科学软件网为大家提供三场讲座的回放以及“Stata洞察数据科学研讨会” 中李春涛老师主讲的主题为 “中文地图与Stata” 和 肖光恩老师主讲的主题为 “新冠疫情数据的可视化与建模方法” 的演讲回放视频,全部免费,可直接扫码(页面下方直接点 试听 )免费观看

stata(900x383)4-1(2).jpg

北京天演融智软件有限公司(科学软件网)是Stata软件在中国的授权经销商,为中国的Stata用户提供优质的软件销售和培训服务。

新stata官网图.png

联系我们

北京天演融智软件有限公司(科学软件网)

北京/广州/成都

电话:400 810 4001/18510103847

邮箱:info@sciencesoftware.com.cn 
网址:http://www.sciencesoftware.com.cn 

CSSN1(900x383)-2.jpg