步骤 5:在 Python 环境中安装程序包Step 5: Install packages in your Python environment
03/09/2020
本文内容
Python 开发者社区制作了数千个有用的程序包,用户可以将它们合并到自己的项目中。The Python developer community has produced thousands of useful packages that you can incorporate into your own projects. Visual Studio 提供一个 UI,用于管理 Python 环境中的程序包。Visual Studio provides a UI to manage packages in your Python environments.
查看环境View environments
选择“视图” > “其他窗口” > “Python 环境”菜单命令 。Select the View > Other Windows > Python Environments menu command. “Python 环境” 窗口作为“解决方案资源管理器” 的同级打开,并向用户显示各种可用的环境。The Python Environments window opens as a peer to Solution Explorer and shows the different environments available to you. 列表中显示了使用 Visual Studio 安装程序安装的环境以及单独安装的环境。The list shows both environments that you installed using the Visual Studio installer and those you installed separately. 其中包括全局环境、虚拟环境和 conda 环境。That includes global, virtual, and conda environments. 粗体显示的环境是用于新项目的默认环境。The environment in bold is the default environment that's used for new projects.
备注
还可选择“解决方案资源管理器”窗口,再使用 Ctrl+K 或 Ctrl+` 键盘快捷方式打开“Python 环境”窗口。You can also open the Python Environments window by selecting the Solution Explorer window and using the Ctrl+K, Ctrl+` keyboard shortcut. 如果快捷方式不起作用,并且在菜单中找不到“Python 环境”窗口,则可能是你未安装 Python 工作负载。If the shortcut doesn't work and you can't find the Python Environments window in the menu, it's possible you haven't installed the Python workload.
通过环境的“概述” 选项卡,可以快速访问该环境的交互 窗口以及安装文件夹和解释器。The environment's Overview tab provides quick access to an Interactive window for that environment along with the environment's installation folder and interpreters. 例如,选择“打开交互窗口” ,会在 Visual Studio 中显示该特定环境的交互 窗口。For example, select Open interactive window and an Interactive window for that specific environment appears in Visual Studio.
现在,通过选择“文件” > “新建” > “项目”来创建新项目,然后选择“Python 应用程序”模板 。Now, create a new project with File > New > Project, selecting the Python Application template. 在随即出现的代码文件中,粘贴以下代码来创建像之前的教程步骤一样的余弦波,只不过这次以图形方式绘制。In the code file that appears, paste the following code, which creates a cosine wave like the previous tutorial steps, only this time plotted graphically. 或者,可使用之前创建的项目并替换代码。Alternatively, you can use the project you previously created and replace the code.
from math import radians
import numpy as np # installed with matplotlib
import matplotlib.pyplot as plt
def main():
x = np.arange(0, radians(1800), radians(12))
plt.plot(x, np.cos(x), 'b')
plt.show()
main()
打开 Python 项目后,还可右键单击 Python 环境,再选择“查看所有 Python 环境”,从解决方案资源管理器中打开“Python 环境”窗口With a Python project open, you can also open the Python Environments window from Solution Explorer by right-clicking Python Environments and selecting View All Python Environments
查看编辑器窗格,你会发现如果你将鼠标悬停到 numpy 和 matplotlib 上,会导入显示未解析的语句。Looking at the editor window, you'll notice that if you hover over the numpy and matplotlib import statements that they are not resolved. 这是因为尚未在默认全局环境中安装包。That's because the packages have not been installed to the default global environment.
使用“Python 环境”窗口安装包Install packages using the Python Environments window
在“Python 环境”窗口中,为新的 Python 环境选择默认环境,然后选择“包”选项卡。然后,你将看到环境中当前已安装的包的列表。From the Python Environments window, select the default environment for new Python projects and choose the Packages tab. You will then see a list of packages that are currently installed in the environment.
在搜索字段输入 matplotlib 的名称,再选择“运行命令: pip install matplotlib”选项来安装此项目 。Install matplotlib by entering its name into the search field and then selecting the Run command: pip install matplotlib option. 这将安装 matplotlib还将安装它依赖的所有包(在本例中,包含 numpy)。This will install matplotlib, as well as any packages it depends on (in this case that includes numpy).
如果系统提示同意提升,请同意。Consent to elevation if prompted to do so.
安装程序包后,它会显示在“Python 环境” 窗口中。After the package is installed, it appears in the Python Environments window. 单击程序包右侧的 X 可卸载它。The X to the right of the package uninstalls it.
备注
环境下方可能会出现一个小进度栏,指示 Visual Studio 正在为新安装的程序包生成 IntelliSense 数据库。A small progress bar might appear underneath the environment to indicate that Visual Studio is building its IntelliSense database for the newly-installed package. “IntelliSense” 选项卡也显示了更多详细信息。The IntelliSense tab also shows more detailed information. 请注意,完成该数据库之前,编辑器中的自动完成和语法检查等 IntelliSense 功能针对该程序包处于非活动状态。Be aware that until that database is complete, IntelliSense features like auto-completion and syntax checking won't be active in the editor for that package.
Visual Studio 2017 15.6 及更高版本采用不同且更快的方法来使用 IntelliSense,并在“IntelliSense”选项卡上显示一条简要介绍此内容的消息 。Visual Studio 2017 version 15.6 and later uses a different and faster method for working with IntelliSense, and displays a message to that effect on the IntelliSense tab.
运行程序Run the program
现已安装 matplotlib,请按 (F5) 运行项目来查看输出;如果没有调试器,则使用 (Ctrl+F5) :Now that matplotlib is installed, run the program with (F5) or without the debugger (Ctrl+F5) to see the output:
下一步Next step
深入了解Go deeper