作为学习(best programming languages to learn)和使用的最佳编程语言之一,难怪数以百万计的新程序员将注意力转向学习如何使用Python。它提供了一个低门槛,在所有主要操作系统上都支持Python ,允许您构建从网站到桌面游戏(以及更多)的项目。(Python)
Python通常预装在 macOS 和Linux上,但是Windows用户在开始将(Windows)Python用于他们的项目之前还需要跳过一些额外的障碍。如果您想学习如何在Windows上使用(Windows)Python ,以下是您需要了解的内容。
如何在 Windows 上安装 Python
由于Windows没有预装Python,因此您首先需要安装它。有两个可用的 Python 版本——Python 3(Python—Python 3)和Python 2。出于兼容性原因,您可能需要安装Python 2并将其与较旧的Python软件一起使用。
但是,对Python 2(Python 2)的最后一个版本(Python 2.7)的支持将于 2020 年结束,因此在这个阶段,最好还是坚持安装Python 3。
- 要开始使用Python,您需要前往Python 网站(Python website)并下载最新版本的安装程序。如前所述,我们建议您下载最新的 Python 3 稳定版。
- 下载后,运行Python安装程序。您将看到一个非常易于使用的安装程序菜单,其中包含使用自动设置运行安装程序或在安装前对其进行自定义的选项。
但是,在选择任一选项之前,请按下底部的Add Python to PATH复选框以将(Add Python to PATH)Python添加到您的PATH 变量(PATH variable)中。这将使通过简单地键入python而不是完整路径从命令行或PowerShell运行(PowerShell)Python变得更加容易。(Python)
- 当您准备好开始在Windows上安装(Windows)Python时,单击立即安装(Install Now)选项以使用默认设置安装Python ,或单击(Python)自定义安装(Customize Installation)以在安装开始之前进行更改。
- 如果您选择自定义安装,您将看到一些额外的菜单。可选功能(Optional Features)菜单显示将与Python一起安装的各种功能,包括Python文档。将安装启用复选框的功能,因此单击其中任何一个以防止安装它们,然后按下一步(Next)继续。
- 在“高级选项(Advanced Options)”菜单中,您将能够自定义Python的安装方式。大多数情况下,您可以保留这些设置,但如果您的 PC 有多个用户帐户,您可能希望单击启用为所有用户安装复选框。(Install for all users)
您还可以在自定义安装位置(Customize Install Location)框下为您的Python安装选择自定义安装点。(Python)此时单击安装(Install)开始安装。
- 如果安装成功,您应该会看到最终的安装成功(Install was successful)屏幕。如果向您显示该选项,请按禁用路径长度限制(Disable path length limit)按钮。这是为了绕过文件夹和文件路径长度的 260 个字符限制。否则,按关闭(Close)按钮完成。
如何在 Windows 上运行Python 项目(Projects)
安装Python后,您可以自由地使用它来运行现有的Python软件(Python)或开始开发自己的Python项目。您需要一个Python IDE才能开始编写您自己的Python项目,例如可以从Windows 开始(Windows Start)菜单运行的预包含的IDLE 。
要运行Python代码本身,您需要使用Python解释器。这是转换Python代码并在您的 Windows PC 上适当执行它的软件。您需要打开命令行或PowerShell窗口才能使用解释器。
- 要打开PowerShell窗口,请右键单击Windows 开始(Windows Start)菜单并按Windows PowerShell开始。
- 如果您在安装期间将Python添加到Windows PATH变量中,您应该能够通过在终端窗口中键入python或py来启动解释器。(py)如果没有,则需要使用Python解释器的完整路径才能启动它。
- 解释器允许您键入Python代码并手动运行它。例如,print(“Hello World”)将返回Hello World,由解释器打印。
- 这对于测试和学习Python非常有用,但Python解释器也用于运行带有Python代码的较长文件,以及编译的Python软件。
您可以通过在 PowerShell 窗口中键入python filename.py或py filename.py来执行此操作,将(py filename.py )filename.py替换为您的Python文件。使用与filename.pyc(filename.pyc)等效的文件来运行具有PYC文件扩展名的已编译Python文件。(Python)
- 如果您在使用Python解释器时不确定任何事情,请键入help()以加载帮助实用程序。您可以通过键入模块(modules)来查找可用Python模块的列表,通过键入主题来查找主题(topics),通过键入关键字来查找关键字(keywords),通过键入符号来查找符号(symbols)。完成后,键入quit退出帮助实用程序并返回解释器。
- 要退出Python解释器并返回标准PowerShell窗口,请键入exit()并按 Enter。
安装额外的 Python 模块(Additional Python Modules)
许多Python项目都是为了利用其他模块而构建的——其他项目可以使用共享软件来节省时间,而不是“重新发明轮子”。您可以使用Python 包索引(Python Package Index)PIP查找和安装其他模块。
在开始之前,您需要先安装 Python PIP(install Python PIP),但如果您安装了Python 3.4或更高版本,则应该预先安装它,除非您在Python预安装配置期间删除了该选项。
您可以通过打开PowerShell窗口并键入pip install package-name或python -m pip install package-name来使用(python -m pip install package-name)PIP安装新模块,将(PIP)package-(package-name) name替换为您可以安装的模块包的名称。您可以使用PIP 网站上(PIP website)的搜索工具搜索要安装的软件包。
下一步 Python 步骤
正如我们在这里所展示的,您无需切换到Linux或Mac即可学习这种对初学者友好的编程语言。一旦您知道如何在Windows上使用(Windows)Python,您就可以开始研究如何将它用于您的爱好和兴趣。
如果您想将您的Windows安装变成一个更好的平台来测试您的代码,您可能会考虑从PowerShell切换并安装 Windows 终端(installing the Windows Terminal)。您有自己的Windows编码技巧要分享吗?请(Please)把它们留在下面。
How to Use Python on Windows
Aѕ one of the best programming languages to learn and use, it’s no wonder that millions of new coders turn their attention to learning how to use Python. It offers a low barrier of entry, with support for Python available on all major operating systems, allowing you to build projects from websites to desktop games (and more).
Python is usually found pre-installed on macOS and Linux, but there are a few additional hoops Windows users will need to jump through before they can start using Python for their projects. Here’s what you need to know if you want to learn how to use Python on Windows.
How to Install Python on Windows
As Python doesn’t come pre-installed with Windows, you’ll first need to install it. There are two available versions of Python—Python 3 and Python 2. For compatibility reasons, you might be required to install and use Python 2 with older Python software.
Support for the last release of Python 2 (Python 2.7) ends in 2020, however, so at this stage, it’s probably best to stick with installing Python 3.
- To get started with Python, you’ll need to head to the Python website and download the installer for the latest release. As mentioned, we recommend you download the latest Python 3 Stable release.
- Once downloaded, run the Python installer. You’ll be presented with a very easy-to-use installer menu, with options to run the installer with automatic settings or customize it prior to installation.
Before selecting either option, however, press the Add Python to PATH checkbox at the bottom to add Python to your PATH variable. This will make it easier to run Python from a command line or PowerShell by simply typing python, rather than the full path.
- When you’re ready to begin installing Python on Windows, click the Install Now option to install Python using the default settings, or Customize Installation to make changes before the installation begins.
- If you choose to customize your installation, you’ll be presented with a few additional menus. The Optional Features menu shows various features that will be installed with Python, including the Python documentation. Features with enabled checkboxes will be installed, so click on any of these to prevent them from being installed, then press Next to continue.
- In the Advanced Options menu, you’ll be able to customize how Python will be installed. You can leave these settings for the most part, although you may wish to click to enable the Install for all users checkbox if your PC has multiple user accounts.
You can also choose a custom installation point for your Python installation under the Customize Install Location box. Click Install to begin the installation at this point.
- If the installation was successful, you should see the final Install was successful screen. If the option is shown to you, press the Disable path length limit button. This is to bypass the 260-character limit for folder and file path lengths. Otherwise, press the Close button to finish.
How to Run Python Projects on Windows
Once Python is installed, you’ll be free to use it to run existing Python software or begin developing your own Python projects. You’ll need a Python IDE to be able to begin coding your own Python projects, such as the pre-included IDLE, which you can run from the Windows Start menu.
To run Python code itself, you need to use the Python interpreter. This is the software that converts Python code and executes it appropriately on your Windows PC. You’ll need to open a command line or PowerShell window to be able to use the interpreter.
- To open a PowerShell window, right-click the Windows Start menu and press Windows PowerShell to begin.
- If you added Python to your Windows PATH variable during installation, you should be able to launch the interpreter by typing python or py at the terminal window. If you didn’t, you’ll need to use the full path to your Python interpreter to be able to launch it.
- The interpreter allows you to type Python code and run it manually. For instance, print(“Hello World”) will return Hello World, printed by the interpreter.
- This is great for testing and learning Python, but the Python interpreter is also used to run longer files with Python code, as well as compiled Python software.
You can do this by typing python filename.py or py filename.py at a PowerShell window, replacing filename.py with your Python file. Use an equivalent to filename.pyc to run compiled Python files with the PYC file extension.
- If you’re unsure about anything while you’re using the Python interpreter, type help() to load the help utility. You can find a list of available Python modules by typing modules, topics by typing topics, keywords by typing keywords, and symbols by typing symbols. Once you’re finished, type quit to exit the help utility and return to the interpreter.
- To exit the Python interpreter and return to a standard PowerShell window, type exit() and press enter.
Installing Additional Python Modules
Many Python projects are built to take advantage of other modules—shared software that other projects can use to save time, rather than “reinventing the wheel.” You can find and install additional modules using PIP, the Python Package Index.
You’ll need to install Python PIP first before you can begin, although if you’ve installed Python 3.4 or later, it should come pre-installed unless you removed the option during your Python pre-installation configuration.
You can use PIP to install new modules by opening a PowerShell window and typing pip install package-name or python -m pip install package-name, replacing package-name with the name of a module package you can install. You can search for packages to install by using the search tool at the PIP website.
Next Python Steps
As we’ve shown here, you don’t need to switch to Linux or a Mac to be able to pick up this beginner-friendly programming language. Once you know how to use Python on Windows, you can start looking at how to use it for your hobbies and interests.
If you want to turn your Windows installation into a better platform to test out your code, you might think about switching from the PowerShell and installing the Windows Terminal instead. Do you have your own Windows coding tips to share? Please leave them below.