visual studio 2019 python 2.7_管理 Python 环境和解释器 - Visual Studio | Microsoft Docs

如何在 Visual Studio 中创建和管理 Python 环境How to create and manage Python environments in Visual Studio

08/06/2019

本文内容

Python 环境是运行 Python 代码的上下文,包括全局、虚拟和 Conda 环境 。A Python environment is a context in which you run Python code and includes global, virtual, and conda environments. 环境由解释器、库(通常是 Python 标准库)以及一组已安装的包组成。An environment consists of an interpreter, a library (typically the Python Standard Library), and a set of installed packages. 这些组成部分共同确定哪些语言结构和语法有效、哪些操作系统功能可访问以及哪些包可使用。These components together determine which language constructs and syntax are valid, what operating-system functionality you can access, and which packages you can use.

在 Windows 上的 Visual Studio 中,可使用“Python 环境”窗口(如本文中所述)管理这些环境并选择其中一个作为新项目的默认环境 。In Visual Studio on Windows, you use the Python Environments window, as described in this article, to manage environments and select one as the default for new projects. 环境的其他方面可在以下文章中找到:Other aspects of environments are found in the following articles:

对于任何给定的项目,可选择特定环境而不使用默认环境。For any given project, you can select a specific environment rather than use the default.

有关为 Python 项目创建和使用虚拟环境的详细信息,请参阅使用虚拟环境。For details on creating and using virtual environments for Python projects, see Use virtual environments.

如果想在环境中安装包,请参阅“包”选项卡引用。If you want to install packages in an environment, refer to the Packages tab reference.

若要安装另一个 Python 解释器,请参阅安装 Python 解释器。To install another Python interpreter, see Install Python interpreters. 通常,如果下载并运行传统 Python 分发版的安装程序,Visual Studio 会检测新的安装和环境是否出现在“Python 环境”窗口中并且是否可以为项目选择它们 。In general, if you download and run an installer for a mainline Python distribution, Visual Studio detects that new installation and the environment appears in the Python Environments window and can be selected for projects.

如果不熟悉 Visual Studio 中的 Python,以下文章还提供了一般背景知识:If you're new to Python in Visual Studio, the following articles also provide from general background:

备注

你无法管理使用“文件” > “打开” > “文件夹”命令仅以文件夹方式打开的 Python 代码的环境 。You can't manage environments for Python code that is opened only as a folder using the File > Open > Folder command. 但是,从现有代码创建 Python 项目即可享受 Visual Studio 的环境功能。Instead, Create a Python project from existing code to enjoy the environment features of Visual Studio.

备注

可以使用“文件” > “打开” > “文件夹”命令管理作为文件夹打开的 Python 代码环境 。You can manage environments for Python code that is opened as a folder using the File > Open > Folder command. 使用 Python 工具栏,可以在所有检测到的环境之间切换,还可以添加新环境。The Python toolbar allows you switch between all detected environments, and also add a new environment. 环境信息存储在 Workspace.vs 文件夹中的 PythonSettings.json 文件中。The environment information is stored in the PythonSettings.json file in the Workspace .vs folder.

“Python 环境”窗口The Python Environments window

Visual Studio 了解的环境显示在“Python 环境” 窗口中。The environments that Visual Studio knows about are displayed in the Python Environments window. 要打开该窗口,请使用以下某个方法:To open the window, use one of the following methods:

选择“视图” > “其他窗口” > “Python 环境”菜单命令 。Select the View > Other Windows > Python Environments menu command.

在解决方案资源管理器中,右键单击某项目的“Python 环境”节点,选择“查看所有 Python 环境” :Right-click the Python Environments node for a project in Solution Explorer and select View All Python Environments:

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

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

在任一情况下,“Python 环境”窗口将出现在解决方案资源管理器旁边 :In either case, the Python Environments window appears alongside Solution Explorer:

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

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

Visual Studio 使用注册表查找已安装的全局环境(遵循 PEP 514),以及查找虚拟环境和 conda 环境(请参阅环境类型)。Visual Studio looks for installed global environments using the registry (following PEP 514), along with virtual environments and conda environments (see Types of environments). 如果在列表中看不到预期的环境,请参阅手动标识现有环境。If you don't see an expected environment in the list, see Manually identify an existing environment.

选择列表中的环境时,Visual Studio 将在“概述”选项卡上显示该环境的各种属性和命令 。例如,可在上图中看到解释器的位置是 C:\Python36-32 。When you select an environment in the list, Visual Studio displays various properties and commands for that environment on the Overview tab. For example, you can see in the image above that the interpreter's location is C:\Python36-32. “概述“选项卡底部的四个命令在解释器运行时分别打开一个命令提示符 。The four commands at the bottom of the Overview tab each open a command prompt with the interpreter running.

使用环境列表下方的下拉列表可切换到不同的选项卡,例如“包”和“IntelliSense” 。Use the drop-down list below the list of environments to switch to different tabs such as Packages, and IntelliSense.

选择环境不会改变其与任何项目的关系。Selecting an environ

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值