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

Setting up Stata to use Python

2020-09-24


stata-9.jpg

Python integration is one of the most exciting features in Stata 16. There are thousands of free Python packages that you can use to access and process data from the Internet, visualize data, explore data using machine-learning algorithms, and much more. You can use these Python packages interactively within Stata or incorporate Python code into your do-files. And there are a growing number of community-contributed commands that have familiar, Stata-style syntax that use Python packages as the computational engine. But there are a few things that we must do before we can use Python in Stata. This blog post will show you how to set up Stata to use Python.

 

Download and install Python

You may already have Python installed on your computer. You can check by typing python search in Stata.

1.jpg

The result tells us that no Python installations were found, so I will need to install Python. You may need to consult your system administrator if you are not the owner or administrator of the computer you are using.

 

You can download an installation file for Python at the Python download website. Python is available for Linux, Mac, and Windows, so you should select the installation file that corresponds to your operating system. You should select a 64-bit installation file because Stata/Python integration works only with 64-bit processors. Python 3.8.5 is the current version as I am writing this post, and I recommend using the latest version. Older versions of Python are available for download if you need them for backward compatibility, but versions older than Python 3.0 have now reached “end of life” status.

 

After you download the installation file, simply run it and follow the setup instructions. I have recorded a video that shows how to download and install Python for Windows 10 on a 64-bit computer.

 

Download and install Anaconda/Python

Python is also included in an open-source development environment called Anaconda. Many people prefer Anaconda because it automatically installs and manages many Python packages during installation. You can download the appropriate Anaconda installation file at the Anaconda download website. Anaconda is also available for Linux, Mac, and Windows, and you should select the 64-bit installation file that corresponds to your operating system. After you download the installation file, simply run it and follow the setup instructions. I have recorded a video that shows how to download and install Anaconda for Windows 10 on a 64-bit computer. 

Setting up Stata to use Python

After you install Python or Anaconda, or both, on your computer, you can return to Stata and again type python search in the Command window.

2.jpg

I have installed both Python 3.8 and Anaconda 3.0 on my computer. I can tell Stata which Python installation I wish to use with the set python_exec command. The example below tells Stata that I wish to use Python 3.8.

3.jpg

You can type python query to see which Python installation you are currently using.

4.jpg

You can use set python_userpath to set additional paths for Python to look up packages and modules that you create or download. The example below uses set python_userpath to add the folder where I save my personal Python modules.

5.jpg

You can type python query to see which Python installation you are currently using.

6.jpg

Now we’re ready to use Python within Stata! You can begin using Python interactively by typing python in the Command window. Stata will remind you that you can type end to exit Python and return to Stata. The example below uses Python to print() the sentence “Hello Stata, I am Python” to the screen.

7.jpg

It worked! This blog post is the first in a series that I am writing to show you how to use Python within Stata. My goal is to provide a basic foundation so that you will have the skills to explore Python packages that are useful to you. This post demonstrated how to install Python and set up Stata to use Python. In my next post, I will demonstrate several ways to use Python from within Stata.

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

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

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

新stata官网图.png

联系我们

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

北京/广州/成都

电话:400 810 4001
邮箱:info@sciencesoftware.com.cn 
网址:http://www.sciencesoftware.com.cn

CSSN1(900x383)-2.jpg