如何在 Windows 上安装 Python
Python

> 
> 安装 Python，启动 IDE，然后你就可以在 Windows 系统下进行编程了。
> 
> 
> 


![](/data/attachment/album/202006/20/232632iggga8agp5gablg5.jpg)


你是否想学习编程呢？可以从 [Python](https://www.python.org/) 开始，它因为简洁的语法和面向对象的特性而非常受欢迎，是最常见的编程语言之一。而且 Python 是一门解释型语言，这意味着你无需知道如何把 Python 代码编译为机器语言 —— Python 会帮你做到这一点，从而使你能够在编写代码的同时立即进行测试。


但是你也不能仅仅因为 Python 学习起来简单而低估了它强大的潜能，无论是在[电影](https://github.com/edniemeyer/weta_python_db)[工作室](https://www.python.org/about/success/ilm/)、视频游戏工作室，还是在金融机构、IT 机构、制造业，都有 Python 的身影，甚至很多业余爱好者、[艺术家](https://opensource.com/article/19/7/rgb-cube-python-scribus)、教师和许多其他人都使用 Python。


另一方面，Python 也是一门严肃的编程语言，学习它需要付出和实践。还有，你什么都不需要马上做，就几乎可以在任何计算机平台上安装并尝试使用 Python ，因此，如果你使用的是 Windows 操作系统，那本文正适合你。


如果你使用的是开源的 Linux 操作系统，那你可以 [在 Linux 上安装](https://opensource.com/article/19/7/ways-get-started-linux) 并 [尝试使用 Python](https://opensource.com/article/17/10/python-101)。


### 如何获取 Python


你可以从 [Python 官方网站](https://www.python.org/downloads/) 上去下载 Python。在 Python 官方网站下载页，可以将鼠标悬停在 “Downloads” 按钮上，待菜单展开后，再将鼠标悬停在 “Windows” 选项，最后点击按钮就可以下载最新版的 Python 了。


![Downloading Python on Windows](/data/attachment/album/202006/20/232658bjmlg33igjiizvsc.jpg "Downloading Python on Windows")


或者你也可以直接点击 “Downloads” 按钮，然后在下载页中选择特定的版本去下载。


### 安装 Python


下载好安装包后，直接打开就可以启动安装程序了。


安装时，选择默认的安装路径比较好，最重要的是要将 Python 添加到搜索路径中，否则 Python 应用程序不知道它该从哪里找到 Python 必须的运行时环境；默认认安装时并没有选中这一项，需要手动勾选安装窗口的 “Add Python 3.7 to Path” ，然后点击继续。


![Select "Add Python 3 to PATH"](/data/attachment/album/202006/20/232704bkwkcx3jcwk23zpd.jpg "Select \"Add Python 3 to PATH\"")


由于 Windows 需要经过你的批准才会允许安装非微软官方发行的应用程序，因此你必须要在弹出 “User Account Control”（UAC） 系统提示窗口的时候 点击 “Yes” 来继续完成安装。


![Windows UAC](/data/attachment/album/202006/20/232714ftsggwnaswsg2jja.jpg "Windows UAC")


这时候你就需要耐心等待，系统会自动将 Python 软件包分发到合适的位置，等待过后，也就意味着你已经成功的安装了 Python ，你就可以愉快的与 Python 玩耍了！


### 安装一个 IDE


虽然说你仅需要一个文本编辑器，就可以开始编写 Python 代码了，但是我依然建议你使用 IDE（集成开发环境）来进行开发，这样开发起来就会变得方便很多。IDE 会将文本编辑器和一些好用的 Python 功能集成到一起，使用起来非常友好。你可以考虑选择 IDLE 3 或者 NINJA-IDE 来作为你的 IDE。


#### IDLE 3


Python 自带一款 IDE，名字叫 IDLE。虽然你可以使用任何文本编辑器编写 Python 代码，但 IDE 通常会提供 [Notepad++](https://notepad-plus-plus.org/) 之类的纯文本编辑器所没有的代码高亮、语法检测等功能，甚至在 IDE 里可以直接通过鼠标点击 “Run” 按钮就能快速运行 Python 代码。


想要启动 IDLE ，可以点击 Windows 的 “开始” （或者 “Windows”）按钮，然后输入 `python` 来进行搜索。这时候可能会搜索到多个选项，选择 IDLE 启动就好了。


![IDLE 3 IDE](/data/attachment/album/202006/20/232717g88luljljujuo0r0.png "IDLE 3 IDE")


如果在 “开始” 菜单中没有搜到，你也可以在 “开始” 菜单输入 `cmd` (或同时按下键盘 `win + R` 键) 来启动 Windows 命令提示符，然后输入下面命令来启动 IDLE ：



```
C:\Windows\py.exe

```

如果还是无法启动的话，可是试着重新安装 Python ,安装时记得务必要在安装向导中选中 “Add Python to PATH”。详细说明，可以参考 [Python 官方文档](http://docs.python.org/3/using/windows.html) 。


#### Ninja-IDE


如果你已经有一些编程经验，而且你觉得 IDLE 对你来说有些简陋的话，你也可以试试 [Ninja-IDE](http://ninja-ide.org/)。 Ninja-IDE 是一款非常出色的 Python IDE，它具有代码高亮功能，并且它会自动帮你检测诸如拼写错误、引号或括号缺失以及其他语法错误。而且它还可以显示行号（调试的时候会非常有用）、缩进标记甚至可以直接通过点击 “Run” 按钮来运行你的代码。


![Ninja-IDE](/data/attachment/album/202006/20/232726nge75gme6pgt379g.jpg "Ninja-IDE")


如果要安装 Ninja-IDE ，你可以访问 Ninja-IDE 的官网 [下载 Windows 安装程序](http://ninja-ide.org/downloads/) 。步骤跟安装 Python 大同小异：下载安装包、允许 Windows 安装非微软官方的应用程序，然后等待完成安装即可。


Ninja-IDE 安装完成后，双击 Windows 桌面或开始菜单下的 Ninja-IDE 就可以启动了。


### 告诉 Python 怎么做


关键字能让 Python 知道你想要做什么。在 IDLE 或者 Ninja-IDE ，打开 “文件” 菜单，创建一个新文件。


在 Ninja-IDE 中，不要创建一个新项目，而是单独创建一个新文件就可以了。


在你用 IDLE 或者 Ninja-IDE 新建的文件中，输入以下代码：



```
print("Hello world.")

```

* 如果你使用的是 IDLE ，请点击 “运行” 菜单，选择 “运行模块” 选项来运行你的程序。
* 如果你使用的是 Ninja ，请单击左侧按钮栏中的 “运行文件” 按钮。


![Running code in Ninja-IDE](/data/attachment/album/202006/20/232734xi9hx7k82hah4hde.png "Running code in Ninja-IDE")


任何时间当你想要执行代码，IDE 都会提示你先保存当前正在处理的文件，然后再继续。


`print` 关键字会告诉 Python 打印出你在括号和引号中输入的所有文本。


但是别高兴地太早，Python 的核心库也就只能访问 `print` 和 `help` 之类的基本关键字、函数等。


如果想要使用更多的关键字和函数，你就需要使用 `import` 关键字去加载它们。好了，先在你的 IDLE 或者 Ninja 中新建一个文件，命名为 `pen.py` 。


**警告**：不要把你新建的文件命名为 `turtle.py` ，因为 `turtle.py` 是包含了你要控制的 turtle 程序的文件名称，使用 `turtle.py` 作为文件名会让 Python 感到困惑，因为它以为你想要引入你自己的文件。


在你新建的文件中输入一下代码，并运行：



```
import turtle

```

[Turtle](https://opensource.com/life/15/8/python-turtle-graphics) 是一个用起来非常有意思的模块. 把这段代码添加到你的文件内：



```
turtle.begin_fill()
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.end_fill()

```

来看看你可以使用 turtle 模块来绘制出哪些图案。


想要清空 turtle 绘制的区域的话，你可以使用 `turtle.clear()` 函数。那你知道 `turtle.color("blue")` 是用来做什么的吗？


我们来试一下更复杂点的程序：



```
import turtle as t
import time

t.color("blue")
t.begin_fill()

counter = 0

while counter &lt; 4:
    t.forward(100)
    t.left(90)
    counter = counter+1

t.end_fill()
time.sleep(2)

```

给你个挑战，试试修改代码，让它得到下图所示的结果：


![Example Python turtle output](/data/attachment/album/202006/20/232737el3l57qmkgzyklby.jpg "Example Python turtle output")


当你完成这段代码后，你就可以继续学习更多有意思的模块了。这个 [入门级骰子游戏](https://opensource.com/article/17/10/python-101#python-101-dice-game) 就是个不错的开始。


### 保持 Pythonic


Python 是一门非常有趣的语言，它的模块几乎能实现所有你想要实现的功能。正如你所看到的，Python 入门很容易，只要你对自己有耐心，很快就会发现自己在理解和编写 Python 时能像写汉字一样流畅。你可以多阅读关于 [Python 的文章](https://opensource.com/sitewide-search?search_api_views_fulltext=Python)，试着自己编写一些小片段，然后看看 Python 会执行出什么结果。如果想要把 Python 真正融合到你实际工作中，你可以试试 Linux ，Linux 具有在本地可编写脚本的功能，而其他系统却没有。


祝你好运，记得保持 Pythonic。




---


via: <https://opensource.com/article/19/8/how-install-python-windows>


作者：[Seth Kenlon](https://opensource.com/users/sethhttps://opensource.com/users/greg-p) 选题：[lujun9972](https://github.com/lujun9972) 译者：[LiuWenlong](https://github.com/011011100010110101101111) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
So you want to learn to program? One of the most common languages to start with is [Python](https://www.python.org/), popular for its unique blend of [object-oriented](https://opensource.com/article/19/7/get-modular-python-classes) structure and simple syntax. Python is also an *interpreted* *language*, meaning you don't need to learn how to compile code into machine language: Python does that for you, allowing you to test your programs sometimes instantly and, in a way, while you write your code.

Just because Python is easy to learn doesn't mean you should underestimate its potential power. Python is used by [movie](https://github.com/edniemeyer/weta_python_db) [studios](https://www.python.org/about/success/ilm/), financial institutions, IT houses, video game studios, makers, hobbyists, [artists](https://opensource.com/article/19/7/rgb-cube-python-scribus), teachers, and many others.

On the other hand, Python is also a serious programming language, and learning it takes dedication and practice. Then again, you don't have to commit to anything just yet. You can install and try Python on nearly any computing platform, so if you're on Windows, this article is for you.

If you want to try Python on a completely open source operating system, you can [install Linux](https://opensource.com/article/19/7/ways-get-started-linux) and then [try Python](https://opensource.com/article/17/10/python-101).

## Get Python

Python is available from its website, [Python.org](https://www.python.org/downloads/). Once there, hover your mouse over the **Downloads** menu, then over the **Windows** option, and then click the button to download the latest release.

![Installing Python on Windows Downloading Python on Windows](https://opensource.com/sites/default/files/uploads/win-python-install.jpg)

Alternatively, you can click the **Downloads** menu button and select a specific version from the downloads page.

## Install Python

Once the package is downloaded, open it to start the installer.

It is safe to accept the default install location, and it's vital to add Python to PATH. If you don't add Python to your PATH, then Python applications won't know where to find Python (which they require in order to run). This is *not* selected by default, so activate it at the bottom of the install window before continuing!

![Select "Add Python 3 to PATH" Select "Add Python 3 to PATH"](https://opensource.com/sites/default/files/uploads/win-python-path.jpg)

Before Windows allows you to install an application from a publisher other than Microsoft, you must give your approval. Click the **Yes** button when prompted by the **User Account Control** system.

![Windows UAC Windows UAC](https://opensource.com/sites/default/files/uploads/win-python-publisher.jpg)

Wait patiently for Windows to distribute the files from the Python package into the appropriate locations, and when it's finished, you're done installing Python.

Time to play.

## Install an IDE

To write programs in Python, all you really need is a text editor, but it's convenient to have an integrated development environment (IDE). An IDE integrates a text editor with some friendly and helpful Python features. IDLE 3 and Pycharm (Community Edition) are two great open source options to consider.

### IDLE 3

Python comes with an IDE called IDLE. You can write code in any text editor, but using an IDE provides you with keyword highlighting to help detect typos, a **Run** button to test code quickly and easily, and other code-specific features that a plain text editor like [Notepad++](https://notepad-plus-plus.org/) normally doesn't have.

To start IDLE, click the **Start** (or **Window**) menu and type **python** for matches. You may find a few matches, since Python provides more than one interface, so make sure you launch IDLE.

![IDLE 3 IDE IDLE 3 IDE](https://opensource.com/sites/default/files/uploads/idle3.png)

If you don't see Python in the Start menu, reinstall Python. Be sure to select **Add Python to PATH** in the install wizard. Refer to the [Python docs](http://docs.python.org/3/using/windows.html) for detailed instructions.

### PyCharm IDE

If you already have some coding experience and IDLE seems too simple for you, try [PyCharm (Community Edition)](https://www.jetbrains.com/pycharm/download/#section=windows), an open source IDE for Python. It has keyword highlighting to help detect typos, quotation and parenthesis completion to avoid syntax errors, line numbers (helpful when debugging), indentation markers, and a **Run** button to test code quickly and easily.

To install it, visit the PyCharm IDE website, download the installer, and run it. The process is the same as with Python: start the installer, allow Windows to install a non-Microsoft application, and wait for the installer to finish.

Once PyCharm is installed, double-click the PyCharm icon on your desktop or select it from the Start menu.

## Tell Python what to do

Keywords tell Python what you want it to do. In IDLE, go to the File menu and create a new file. In PyCharm, click the **New Project** button.

In your new, empty file, type this into IDLE or PyCharm:

`print("Hello world.")`


- If you are using IDLE, go to the Run menu and select the Run Module option.
- If you are using PyCharm, click the Run button in the top right corner of the window.

![Run code Run code](https://opensource.com/sites/default/files/pycharm-button-run.jpeg)

opensource.com

Any time you run code, your IDE prompts you to save the file you're working on. Do that before continuing.

The keyword **print** tells Python to print out whatever text you give it in parentheses and quotes.


That's not very exciting, though. At its core, Python has access to only basic keywords like **print** and **help**, basic math functions, and so on.

Use the **import** keyword to load more keywords. Start a new file and name it **pen.py**.

**Warning**: Do *not* call your file **turtle.py**, because **turtle.py** is the name of the file that contains the turtle program you are controlling. Naming your file **turtle.py** confuses Python because it thinks you want to import your own file.

[Turtle](https://opensource.com/life/15/8/python-turtle-graphics) is a fun module to use. Add this code to your file:

```
import turtle
turtle.begin_fill()
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.left(90)
turtle.forward(100)
turtle.end_fill()
```


See what shapes you can draw with the turtle module.

To clear your turtle drawing area, use the **turtle.clear()** keyword. What do you think the keyword **turtle.color("blue")** does?

Try more complex code:

```
import turtle as t
import time
t.color("blue")
t.begin_fill()
counter = 0
while counter < 4:
t.forward(100)
t.left(90)
counter = counter+1
t.end_fill()
time.sleep(2)
```


Notice that turtle, in this example code, has not only been imported, but it's also been given the shorter nickname **t**, which is quicker and easier to type. This is a convenience function in Python.

### Challenge

As a challenge, try changing your script to get this result:

![Example Python turtle output Example Python turtle output](https://opensource.com/sites/default/files/uploads/win-python-idle-turtle.jpg)

Once you complete that script, you're ready to move on to more exciting modules. A good place to start is this [introductory dice game](https://opensource.com/article/17/10/python-101#python-101-dice-game).

## Stay Pythonic

Python is a fun language with modules for practically anything you can think to do with it. As you can see, it's easy to get started with Python, and as long as you're patient with yourself, you may find yourself understanding and writing Python code with the same fluidity as you write your native language. Work through some [Python articles](https://opensource.com/sitewide-search?search_api_views_fulltext=Python) here on Opensource.com, try scripting some small tasks for yourself, and see where Python takes you. To really integrate Python with your daily workflow, you might even try Linux, which is natively scriptable in ways no other operating system is. You might find yourself, given enough time, using the applications you create!

Good luck, and stay Pythonic.

*This article was originally published in August 2019 and has been updated by the author.*

## 6 Comments