如何装python软件_Visual Studio 中的 Python 教程步骤 5,安装包 | Microsoft Docs

步骤 5:在 Python 环境中安装程序包Step 5: Install packages in your Python environment

03/09/2020

JoshuaPartlow.png?size=32

olprod.png?size=32

本文内容

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.

environments-default-view-2019.png?view=vs-2019

备注

还可选择“解决方案资源管理器”窗口,再使用 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

environments-view-all-2019.png?view=vs-2019

查看编辑器窗格,你会发现如果你将鼠标悬停到 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.

packages-unresolved-import.png?view=vs-2019

使用“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.

environments-installed-packages-2019.png?view=vs-2019

在搜索字段输入 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).

environments-add-matplotlib-2019.png?view=vs-2019

如果系统提示同意提升,请同意。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.

environments-add-matplotlib2-2019.png?view=vs-2019

备注

环境下方可能会出现一个小进度栏,指示 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:

environments-add-matplotlib3.png?view=vs-2019

下一步Next step

深入了解Go deeper

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值