热门标签 | 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



推荐阅读
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 基于PgpoolII的PostgreSQL集群安装与配置教程
    本文介绍了基于PgpoolII的PostgreSQL集群的安装与配置教程。Pgpool-II是一个位于PostgreSQL服务器和PostgreSQL数据库客户端之间的中间件,提供了连接池、复制、负载均衡、缓存、看门狗、限制链接等功能,可以用于搭建高可用的PostgreSQL集群。文章详细介绍了通过yum安装Pgpool-II的步骤,并提供了相关的官方参考地址。 ... [详细]
  • 安装mysqlclient失败解决办法
    本文介绍了在MAC系统中,使用django使用mysql数据库报错的解决办法。通过源码安装mysqlclient或将mysql_config添加到系统环境变量中,可以解决安装mysqlclient失败的问题。同时,还介绍了查看mysql安装路径和使配置文件生效的方法。 ... [详细]
  • Linux服务器密码过期策略、登录次数限制、私钥登录等配置方法
    本文介绍了在Linux服务器上进行密码过期策略、登录次数限制、私钥登录等配置的方法。通过修改配置文件中的参数,可以设置密码的有效期、最小间隔时间、最小长度,并在密码过期前进行提示。同时还介绍了如何进行公钥登录和修改默认账户用户名的操作。详细步骤和注意事项可参考本文内容。 ... [详细]
  • 学习SLAM的女生,很酷
    本文介绍了学习SLAM的女生的故事,她们选择SLAM作为研究方向,面临各种学习挑战,但坚持不懈,最终获得成功。文章鼓励未来想走科研道路的女生勇敢追求自己的梦想,同时提到了一位正在英国攻读硕士学位的女生与SLAM结缘的经历。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • 本文介绍了Oracle数据库中tnsnames.ora文件的作用和配置方法。tnsnames.ora文件在数据库启动过程中会被读取,用于解析LOCAL_LISTENER,并且与侦听无关。文章还提供了配置LOCAL_LISTENER和1522端口的示例,并展示了listener.ora文件的内容。 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文介绍了如何使用C#制作Java+Mysql+Tomcat环境安装程序,实现一键式安装。通过将JDK、Mysql、Tomcat三者制作成一个安装包,解决了客户在安装软件时的复杂配置和繁琐问题,便于管理软件版本和系统集成。具体步骤包括配置JDK环境变量和安装Mysql服务,其中使用了MySQL Server 5.5社区版和my.ini文件。安装方法为通过命令行将目录转到mysql的bin目录下,执行mysqld --install MySQL5命令。 ... [详细]
  • web.py开发web 第八章 Formalchemy 服务端验证方法
    本文介绍了在web.py开发中使用Formalchemy进行服务端表单数据验证的方法。以User表单为例,详细说明了对各字段的验证要求,包括必填、长度限制、唯一性等。同时介绍了如何自定义验证方法来实现验证唯一性和两个密码是否相等的功能。该文提供了相关代码示例。 ... [详细]
  • 本文介绍了如何使用Express App提供静态文件,同时提到了一些不需要使用的文件,如package.json和/.ssh/known_hosts,并解释了为什么app.get('*')无法捕获所有请求以及为什么app.use(express.static(__dirname))可能会提供不需要的文件。 ... [详细]
  • 基于dlib的人脸68特征点提取(眨眼张嘴检测)python版本
    文章目录引言开发环境和库流程设计张嘴和闭眼的检测引言(1)利用Dlib官方训练好的模型“shape_predictor_68_face_landmarks.dat”进行68个点标定 ... [详细]
  • 树莓派语音控制的配置方法和步骤
    本文介绍了在树莓派上实现语音控制的配置方法和步骤。首先感谢博主Eoman的帮助,文章参考了他的内容。树莓派的配置需要通过sudo raspi-config进行,然后使用Eoman的控制方法,即安装wiringPi库并编写控制引脚的脚本。具体的安装步骤和脚本编写方法在文章中详细介绍。 ... [详细]
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社区 版权所有