简体   繁体   English

在不使用 settings.json 文件的情况下在 VS Code 中设置本地项目的 Python 路径

[英]Setting the Python path for local project in VS Code without using the settings.json file

This answer from a Visual Studio Code developer says that it is reasonable to keep a Visual Studio Code project's settings.json file in a project's git repository to enforce code standards across different development environments.这个来自 Visual Studio Code 开发人员的回答说,将 Visual Studio Code 项目的settings.json文件保留在项目的 git 存储库中以在不同的开发环境中强制执行代码标准是合理的。 One down-side though is that when I select the Python interpreter path at the bottom of the screen (so the linter can find the installed packages), the following entry gets added to the settings.json file:但一个缺点是,当我选择屏幕底部的 Python 解释器路径(以便 linter 可以找到已安装的包)时,以下条目将添加到settings.json文件中:

"python.pythonPath": "/path/to/conda/envs/my-env-name/bin/python",

This path is local to the machine and assumes that conda is being used.此路径是机器的本地路径,并假定正在使用 conda。 I have to avoid adding this setting to the settings.json that I commit to the git repository, which is annoying.我必须避免将此设置添加到我提交给 git 存储库的settings.json中,这很烦人。

Is there a way to set the Python path locally for a project without writing a setting into the settings.json file?有没有办法在不将设置写入settings.json文件的情况下为项目在本地设置 Python 路径?

目前不直接支持它,但我们有一个功能请求,如果您想优先查看它,您可以投票。

One workaround would be to:一种解决方法是:

  • remove that line from the local workspace settings.json从本地工作区settings.json删除该行
  • copy-paste it to your User settings.json将其复制粘贴到您的 User settings.json

See " VSCode User and Workspace Settings ".请参阅“ VSCode 用户和工作区设置”。

That way, your Git codebase can keep a generic settings.json without local path.这样,您的 Git 代码库可以保留没有本地路径的通用settings.json

Since this would work for only one project, you can instead reference all your projects in severalMulti-root workspaces .由于这仅适用于一个项目,因此您可以在多个多根工作区中引用所有项目。

Then, regarding settings in that environment, you have three files:然后,关于该环境中的设置,您有三个文件:

  • Preferences: Open User Settings - Open your global User settings首选项:打开用户设置 - 打开您的全局用户设置
  • references: Open Workspace Settings - Open the settings section of your Workspace file.参考: 打开工作区设置 - 打开工作区文件的设置部分。
  • Preferences: Open Folder Settings - Open the settings for the active folder.首选项:打开文件夹设置 - 打开活动文件夹的设置。

That means you could switch workspaces, and in each multi-root workspace (each one composed of only one root), you would keep:这意味着您可以切换工作区,并且在每个多根工作区(每个工作区仅由一个根组成)中,您将保留:

  • the global pythonPath path in user settings (applies to everything, everywhere)用户设置中的全局pythonPath路径(适用于一切,无处不在)
  • the specific pythonPath for a given multi-root workspace in the Workspace setting (outside of the project folder which is the only root for that "multi-root" workspace) Workspace 设置中给定多根工作区的特定pythonPath (在项目文件夹之外,它是该“多根”工作区的唯一根目录)
  • the public versioned settings.json project-specific settings in the project folder (which is the only root of the workspace)项目文件夹中的公共版本化settings.json项目特定设置(这是工作区的唯一根目录)

Again, by switching workspace, you can differentiate between:同样,通过切换工作区,您可以区分:

  • workspace-specific private settings, local to your computer, like a pythonPath特定于工作区的私有设置,位于您的计算机本地,如pythonPath
  • project specific settings, in the project folder, that you can version and publish.项目特定设置,位于项目文件夹中,您可以对其进行版本控制和发布。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM