Visual studio:Python 源代码控制中的虚拟环境

Visual studio: Python virtual environments in source control

我已经成功设置了一个 Visual Studio Python 项目。我现在想通过源代码管理(我工作的公司使用SVN)与其他开发人员共享这个项目。

因为我想避免每个同事都必须手动设置相同的 Python 环境,所以我研究了使用 Virtual Environment。在我看来,这与 NPM 模块在本地的存储方式非常相似。

我毫不费力地设置了一个虚拟环境,效果非常好。

但是,当我注意到 "pyproj"- 文件包含对我的本地虚拟环境的引用时,我准备将我的 "Virtual Environment"- 文件夹排除在签入 SVN 之外:

<ItemGroup>
  <Interpreter Include="VirtualEnvironment\">
  <Id>VirtualEnvironment</Id>
  <Version>3.6</Version>
  <Description>VirtualEnvironment (Python 3.6 (64-bit))</Description>
  <InterpreterPath>Scripts\python.exe</InterpreterPath>
  <WindowsInterpreterPath>Scripts\pythonw.exe</WindowsInterpreterPath>
  <PathEnvironmentVariable>PYTHONPATH</PathEnvironmentVariable>
  <Architecture>X64</Architecture>
</Interpreter>

如果我删除 "Virtual Environment"-文件夹并打开 Visual Studio 解决方案,我没有任何选项可以根据生成的 "requirements.txt"-文件恢复环境(如我期望)。除非我删除不工作的 "Virtual Environment" 并添加一个全新的

这让我相信我的工作流程或假设有问题。

旁注:

恕我直言:最好的方法是拥有一个 requirements.txt,并写下如何安装 python env。在 readme.txt.

您将 check-in 是 requirements.txt 和自述文件。

发布 issue on the MicrosoftDocs GitHub, I received the following reply from zooba 后:

There's certainly some work in progress around this area. We're looking at different designs here and ways to better align VS and VS Code.

For Visual Studio: the intent for virtual environments in a Python project file is that you have the environment within your project directory, so it is only referenced by a relative path. If you also keep a requirements.txt file in your project, it should only be a couple of clicks to recreate it on a new machine (we had considered automatic prompts to help out here, but most user feedback indicated we had other stuff to fix first).

So our broad recommendations would be:

  • have the "main" virtual environment be at the default location (env in the project folder)
  • exclude the entire environment itself from version control
  • keep development requirements in a requirements.txt file
  • recreate the virtual environment on new machines with the normal Add Virtual Environment command (having the default location and requirements file will make this smoothest - we'll change it from "missing" to found as soon as it's created)

Obviously feel free to vary these as they make sense. You can also have a different task create the virtual environment (e.g. a batch file that runs python -m venv path\to\env) and we'll still pick it up just fine without having to modify the project file.

And as we work on improvements here for the overall process, it should get easier to just have an environment "somewhere" and use it, rather than having to make specific configuration settings in your project.