热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python环境an_选择用于项目的Python环境VisualStudio|MicrosoftDocs

如何选择用于项目的Python环境HowtoselectaPythonenvironmentforaproject03182019本文内容Python项目中的所有代码都在特定环境的

如何选择用于项目的 Python 环境How to select a Python environment for a project

03/18/2019

本文内容

Python 项目中的所有代码都在特定环境的上下文中运行,例如全局 Python 环境、Anaconda 环境、虚拟环境或 conda 环境。All code in a Python project runs within the context of a specific environment, such as a global Python environment, an Anaconda environment, a virtual environment, or a conda environment. Visual Studio 还将该环境用于调试、导入和成员完成、语法检查,以及需要特定于 Python 版本和一组已安装包的语言服务的任何其他任务。Visual Studio also uses that environment for debugging, import and member completions, syntax checking, and any other tasks that require language services that are specific to the Python version and a set of installed packages.

Visual Studio 中所有新的 Python 项目最初都被配置为使用默认的全局环境,它出现在“解决方案资源管理器”的“Python 环境”节点下:All new Python projects in Visual Studio are initially configured to use the default global environment, which appears under the Python Environments node in Solution Explorer:

要更改项目环境,请右键单击“Python 环境”节点,然后选择“添加/删除 Python 环境”。To change the environment for a project, right-click the Python Environments node and select Add/Remove Python Environments. 在包含全局、虚拟和 conda 环境的所示列表中,选择希望在“Python 环境”节点下显示的所有环境:From the displayed list, which includes global, virtual, and conda environments, select all the ones you want to appear under the Python Environments node:

一旦选择“确定”,所有选择的环境都将显示在“Python 环境”节点下。Once you select OK, all the selected environments appear under the Python Environments node. 当前激活的环境以粗体显示:The currently activated environment appears in bold:

要快速激活其他环境,请右键单击环境名称,并选择“激活环境”。To quickly activate a different environment, right-click that environment name and select Activate environment. 你的选择将保存在项目中,当你在将来打开该项目,环境就会被激活。Your choice is saved with the project and that environment is activated whenever you open the project in the future. 如果在“添加/删除 Python 环境”对话框中清除所有选项,Visual Studio 将激活全局默认环境。If you clear all the options in the Add/Remove Python Environments dialog, Visual Studio activates the global default environment.

“Python 环境”节点的上下文菜单还提供其他命令:The context menu on the Python Environments node also provides additional commands:

命令Command

描述Description

添加虚拟环境Add Virtual Environment

首先在项目中新建一个虚拟环境。Begins the process of creating a new virtual environment in the project. 详情请参阅创建虚拟环境。

添加现有虚拟环境Add Existing Virtual Environment

系统提示你选择一个包含虚拟环境的文件夹并将其添加到“Python 环境”下的列表,但不进行激活。Prompts you to select a folder containing a virtual environment and adds it to the list under Python Environments, but does not activate it.

创建 Conda 环境Create Conda environment

切换到“Python 环境”窗口,在此处输入环境的名称并指定其基础解释器。Switches to the Python Environments window in which you enter a name for the environment and specify its base interpreter.

要更改项目环境,请右键单击“Python 环境”节点,然后选择“添加环境”。To change the environment for a project, right-click the Python Environments node and select Add Environment. 还可以从 Python 工具栏中的“环境”下拉列表选择“添加环境”。You can also select Add Environment from the environment drop-down in the Python toolbar.

在“添加环境”对话框中,选择“现有环境”选项卡,然后从“环境”下拉列表中选择新环境:Once in the Add Environment dialog box, select the Existing environment tab, then select a new environment from the Environment drop down list:

如果已向项目中添加除全局默认值以外的环境,则可能需要激活新添加的环境。If you already added an environment other than the global default to a project, you may need to activate a newly added environment. 右键单击“Python 环境”节点下的该环境,然后选择“激活环境”。Right-click that environment under the Python Environments node and select Activate Environment. 若要从项目中删除环境,请选择“删除”。To remove an environment from the project, select Remove.

使用虚拟环境Use virtual environments

虚拟环境是特定 Python 解释器和一组特定库的独特组合,它与其他全局环境以及 conda 环境不同。A virtual environment is a unique combination of a specific Python interpreter and a specific set of libraries that is different from other global and conda environments. 虚拟环境特定于项目,但保留在项目文件夹中。A virtual environment is specific to a project and is maintained in a project folder. 该文件夹包含环境中已安装的库,还有一个 pyvenv.cfg 文件用于指定到文件系统中其他位置的环境基础解释器的路径。That folder contains the environment's installed libraries along with a pyvenv.cfg file that specifies the path to the environment's base interpreter elsewhere on the file system. (即,虚拟环境中没有解释器副本,只有解释器链接。)(That is, a virtual environment doesn't contain a copy of the interpreter, only a link to it.)

使用虚拟环境的一项优势是,随着项目的开发,虚拟环境始终反映出项目的确切依赖项。A benefit to using a virtual environment is that as you develop project over time, the virtual environment always reflects the project's exact dependencies. (另一方面,无论是否在项目中使用库,共享的全局环境中都包含任意数量的库。)随后即可通过虚拟环境轻松创建 requirements.txt 文件,此文件之后用于在另一台开发/生产计算机上重新安装上述依赖项。(A shared global environment, on the other hand, contains any number of libraries whether you use them in your project or not.) You can then easily create a requirements.txt file from the virtual environment, which is then used to reinstall those dependencies on another development or production computer.

在 Visual Studio 中打开包含 requirements.txt 文件的项目时,Visual Studio 自动显示用于重新创建虚拟环境的选项。When you open a project in Visual Studio that contains a requirements.txt file, Visual Studio automatically gives you the option to recreate the virtual environment. 在未安装 Visual Studio 的计算机上,可运行 pip install -r requirements.txt 还原包。On computers where Visual Studio isn't installed, you can use pip install -r requirements.txt to restore the packages.

由于虚拟环境包含到基础解释器的硬编码路径,且你可使用 requirements.txt 重新创建环境,因此通常省略来自源代码管理功能集的整个虚拟环境。Because a virtual environment contains a hard-coded path to the base interpreter, and because you can recreate the environment using requirements.txt, you typically omit the entire virtual environment folder from source control.

下述部分介绍了如何激活项目中的现有虚拟环境以及如何创建新的虚拟环境。The following sections explain how to activate an existing virtual environment in a project and how to create a new virtual environment.

在 Visual Studio 中,可通过“解决方案资源管理器”中的“Python 环境”节点,如其他任意环境一样激活项目的虚拟环境 。In Visual Studio, a virtual environment can be activated for a project like any other through the Python Environments node in Solution Explorer.

虚拟环境添加到项目后,立即显示在“Python 环境”窗口中。Once a virtual environment is added to your project, it appears in the Python Environments window. 随后,即可像激活其他任意环境一样激活它,还可管理它的包。You can then activate it like any other environment, and you can manage its packages.

创建虚拟环境Create a virtual environment

可以按以下方式直接在 Visual Studio 中创建新的虚拟环境:You can create a new virtual environment directly in Visual Studio as follows:

在“解决方案资源管理器”中,右键单击“Python 环境”并选择“添加虚拟环境”,将会打开以下对话框:Right-click Python Environments in Solution Explorer and select Add Virtual Environment, which brings up the following dialog box:

在“虚拟环境位置”字段中,指定虚拟环境的路径。In the Location of the virtual environment field, specify a path for the virtual environment. 如果仅指定一个名称,则会在当前项目中在具有该名称的子文件夹中创建虚拟环境。If you specify a name only, the virtual environment is created within the current project in a subfolder with that name.

选择一个环境作为基础解释器,并选择“创建”。Select an environment as the base interpreter and select Create. Visual Studio 会在配置环境和下载任何必要的包时显示一个进度栏。Visual Studio displays a progress bar while it configures the environment and downloads any necessary packages. 完成后,虚拟环境将显示在所含项目的“Python 环境”窗口中。Upon completion, the virtual environment appears in the Python Environments window for the containing project.

默认情况下不会激活虚拟环境。The virtual environment is not activated by default. 若要为项目激活虚拟环境,请右键单击该项目,然后选择“激活环境”。To activate the virtual environment for the project, right-click it and select Activate Environment.

备注

如果位置路径标识一个现有的虚拟环境,Visual Studio 会自动检测基础解释器(使用环境 lib 目录中的 orig-prefix.txt 文件)并将“创建”按钮更改为“添加”。If the location path identifies an existing virtual environment, Visual Studio detects the base interpreter automatically (using the orig-prefix.txt file in the environment's lib directory) and changes the Create button to Add.

添加虚拟环境时,如果 requirements.txt 文件存在,“添加虚拟环境”对话框会显示一个自动安装包的选项,从而可以轻松地在另一台计算机上重新创建环境:If a requirements.txt file exists when adding a virtual environment, the Add Virtual Environment dialog displays an option to install the packages automatically, making it easy to recreate an environment on another computer:

无论采用哪种方式,结果都与使用“添加现有的虚拟环境”命令相同。Either way, the result is the same as if you'd used the Add Existing Virtual Environment command.

激活现有的虚拟环境Activate an existing virtual environment

如果你已经在其他地方创建了虚拟环境,可以按以下方式为项目激活它:If you've already created a virtual environment elsewhere, you can activate it for a project as follows:

在“解决方案资源管理器”中,右键单击“Python 环境”并选择“添加现有虚拟环境”。Right-click Python Environments in Solution Explorer and select Add Existing Virtual Environment.

在出现的“浏览”对话框中,导航到包含虚拟环境的文件夹并选中,然后选择“确定”。In the Browse dialog that appears, navigate to and select the folder that contains the virtual environment, and select OK. 如果 Visual Studio 在该环境中检测到 requirements.txt 文件,它会询问是否安装这些包。If Visual Studio detects a requirements.txt file in that environment, it asks whether to install those packages.

稍后,虚拟环境会出现在“解决方案资源管理器”中的“Python 环境”节点下。After a few moments, the virtual environment appears under the Python Environments node in Solution Explorer. 默认情况下不会激活虚拟环境,因此请右键单击它并选择“激活环境”。The virtual environment is not activated by default, so right-click it and select Activate Environment.

创建虚拟环境Create a virtual environment

可以按以下方式直接在 Visual Studio 中创建新的虚拟环境:You can create a new virtual environment directly in Visual Studio as follows:

右键单击“解决方案资源管理器”中的“Python 环境”,然后选择“添加环境”,或从 Python 工具栏上的环境下拉列表中选择“添加环境”。Right-click Python Environments in Solution Explorer and select Add Environment, or select Add Environment from the environments drop down list on the Python toolbar. 在出现的“添加环境”对话框中,选择“虚拟环境”选项卡:In the Add Environment dialog that appears, select the Virtual Environment tab:

为虚拟环境指定一个名称,选择基础解释器,然后验证其位置。Specify a name for the virtual environment, select a base interpreter, and verify its location. 在“从文件安装包”下,提供 requirements.txt 文件的路径(如果需要)。Under Install packages from file, provide the path to a requirements.txt file if desired.

查看对话框中的其他选项:Review the other options in the dialog:

选项Option

描述Description

设置为当前环境Set as current environment

创建环境后,在所选项目中激活新环境。Activates the new environment in the selected project after the environment is created.

设置为新项目的默认环境Set as default environment for new projects

自动设置和激活 Visual Studio 中创建的任何新项目中的虚拟环境。Automatically sets and activates the virtual environment in any new projects created in Visual Studio. 使用此选项时,应将虚拟环境置于特定项目之外的位置中。When using this option, the virtual environment should be placed in a location outside of a specific project.

在“Python 环境”窗口中查看View in Python Environments window

指定在创建环境之后是否打开“Python 环境”窗口。Specifies whether to open the Python Environments window after creating the environment.

使此环境全局可用Make this environment available globally

指定虚拟环境是否同时充当全局环境。Specifies whether the virtual environment also acts as a global environment. 使用此选项时,应将虚拟环境置于特定项目之外的位置中。When using this option, the virtual environment should be placed in a location outside of a specific project.

选择“创建”以完成虚拟环境。Select Create to finalize the virtual environment. Visual Studio 会在配置环境和下载任何必要的包时显示一个进度栏。Visual Studio displays a progress bar while it configures the environment and downloads any necessary packages. 完成后,虚拟环境将激活并显示在“解决方案资源管理器”的“Python 环境”节点中以及所含项目的“Python 环境”窗口中 。Upon completion, the virtual environment is activated and appears in the Python Environments node in Solution Explorer and the Python Environments window for the containing project.

激活现有的虚拟环境Activate an existing virtual environment

如果你已经在其他地方创建了虚拟环境,可以按以下方式为项目激活它:If you've already created a virtual environment elsewhere, you can activate it for a project as follows:

右键单击“解决方案资源管理器”中的“Python 环境”,然后选择“添加环境” 。Right-click Python Environments in Solution Explorer and select Add Environment.

在出现的“浏览”对话框中,导航到包含虚拟环境的文件夹并选中,然后选择“确定”。In the Browse dialog that appears, navigate to and select the folder that contains the virtual environment, and select OK. 如果 Visual Studio 在该环境中检测到 requirements.txt 文件,它会询问是否安装这些包。If Visual Studio detects a requirements.txt file in that environment, it asks whether to install those packages.

稍后,虚拟环境会出现在“解决方案资源管理器”中的“Python 环境”节点下。After a few moments, the virtual environment appears under the Python Environments node in Solution Explorer. 默认情况下不会激活虚拟环境,因此请右键单击它并选择“激活环境”。The virtual environment is not activated by default, so right-click it and select Activate Environment.

删除虚拟环境Remove a virtual environment

在“解决方案资源管理器”中,右键单击虚拟环境,然后选择“删除”。In Solution Explorer, right-click the virtual environment and select Remove.

Visual Studio 会询问是否移除或删除虚拟环境。Visual Studio asks whether to remove or delete the virtual environment. 选择“移除”,使其对项目不可用,但在文件系统上保留它。Selecting Remove makes it unavailable to the project but leaves it on the file system. 选择“删除”,从项目中移除环境,并从文件系统中删除它。Selecting Delete both removes the environment from the project and deletes it from the file system. 基础解释器不受影响。The base interpreter is unaffected.

查看已安装的包View installed packages

在“解决方案资源管理器”中,展开特定环境节点以快速查看在该环境中安装的包(也就是说,你可以激活环境时在代码中导入和使用这些包):In Solution Explorer, expand any specific environment's node to quickly view the packages that are installed in that environment (meaning that you can import and use those packages in your code when the environment is active):

若要安装新包,右键单击该环境并选择“安装 Python 包”以切换到“Python 环境”窗口中的相应“包”选项卡 。To install new packages, right-click the environment and select Install Python Package to switch to the appropriate Packages tab in the Python Environments window. 输入搜索词(通常是包名称),Visual Studio 将显示匹配的包。Enter a search term (usually the package name) and Visual Studio displays matching packages.

若要安装新包,右键单击该环境并选择“管理 Python 包”(或使用 Python 工具栏上的包按钮)以切换到“Python 环境”窗口中的相应“包”选项卡 。To install new packages, right-click the environment and select Manage Python Packages (or use the package button on the Python toolbar) to switch to the appropriate Packages tab in the Python Environments window. 在“包”选项卡上,输入搜索词(通常为包名称),随后 Visual Studio 将显示匹配的包。Once in the Packages tab, enter a search term (usually the package name) and Visual Studio displays matching packages.

在 Visual Studio 中,已从“Python 包索引 (PyPI)”中下载适用于大多数环境的包(和依赖项),用户可以在其中搜索可用包。Within Visual Studio, packages (and dependencies) for most environments are downloaded from the Python Package Index (PyPI), where you can also search for available packages. Visual Studio 的状态栏和输出窗口显示有关安装的信息。Visual Studio's status bar and output window show information about the install. 若要卸载包,请右键单击它,并选择“删除”。To uninstall a package, right-click it and select Remove.

Conda 包管理器通常将 https://repo.continuum.io/pkgs/ 用作默认通道,但也可以使用其他通道。The conda package manager generally uses https://repo.continuum.io/pkgs/ as the default channel, but other channels are available. 有关详细信息,请参阅管理通道 (docs.conda.io)。For more information see Manage Channels (docs.conda.io).

请注意,显示的项未必总准确,且安装和卸载可能不可靠或不可用。Be aware that the displayed entries may not always be accurate, and installation and uninstallation may not be reliable or available. Visual Studio 使用 pip 程序包管理器(如果可用),并且需要时会下载并安装它。Visual Studio uses the pip package manager if available, and downloads and installs it when required. Visual Studio 还可以使用 easy_install 程序包管理器。Visual Studio can also use the easy_install package manager. 也会显示使用 pip 或 easy_install 从命令行安装的包。Packages installed using pip or easy_install from the command line are also displayed.

另请注意,Visual Studio 目前不支持使用 conda 将包安装到 conda 环境中。Also note that Visual Studio does not presently support using conda to install packages into a conda environment. 而是使用命令行中的 conda。Use conda from the command line instead.

提示

当包中包含用于 .pyd * 文件中本机组件的源代码时,pip 无法安装包,这种情况很常见。A common situation where pip fails to install a package is when the package includes source code for native components in *.pyd files. 如果没有安装要求的 Visual Studio 版本,pip 无法编译这些组件。Without the required version of Visual Studio installed, pip cannot compile these components. 在此情况下显示的错误消息是:“错误:找不到 vcvarsall.bat”。The error message displayed in this situation is error: Unable to find vcvarsall.bat. easy_install is often able to download pre-compiled binaries, and you can download a suitable compiler for older versions of Python from https://www.microsoft.com/download/details.aspx?id=44266.

请参阅See also



推荐阅读
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • 使用 Azure Service Principal 和 Microsoft Graph API 获取 AAD 用户列表
    本文介绍了一段通用代码示例,该代码不仅能够操作 Azure Active Directory (AAD),还可以通过 Azure Service Principal 的授权访问和管理 Azure 订阅资源。Azure 的架构可以分为两个层级:AAD 和 Subscription。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 本文将介绍如何编写一些有趣的VBScript脚本,这些脚本可以在朋友之间进行无害的恶作剧。通过简单的代码示例,帮助您了解VBScript的基本语法和功能。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 1:有如下一段程序:packagea.b.c;publicclassTest{privatestaticinti0;publicintgetNext(){return ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • CentOS7源码编译安装MySQL5.6
    2019独角兽企业重金招聘Python工程师标准一、先在cmake官网下个最新的cmake源码包cmake官网:https:www.cmake.org如此时最新 ... [详细]
  • 本文介绍了如何使用JQuery实现省市二级联动和表单验证。首先,通过change事件监听用户选择的省份,并动态加载对应的城市列表。其次,详细讲解了使用Validation插件进行表单验证的方法,包括内置规则、自定义规则及实时验证功能。 ... [详细]
  • 本文详细介绍了如何使用 Yii2 的 GridView 组件在列表页面实现数据的直接编辑功能。通过具体的代码示例和步骤,帮助开发者快速掌握这一实用技巧。 ... [详细]
  • 本文详细介绍了Java中org.eclipse.ui.forms.widgets.ExpandableComposite类的addExpansionListener()方法,并提供了多个实际代码示例,帮助开发者更好地理解和使用该方法。这些示例来源于多个知名开源项目,具有很高的参考价值。 ... [详细]
  • 本文详细介绍了如何使用Python编写爬虫程序,从豆瓣电影Top250页面抓取电影信息。文章涵盖了从基础的网页请求到处理反爬虫机制,再到多页数据抓取的全过程,并提供了完整的代码示例。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
author-avatar
手机用户2502920043
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有