简体   繁体   English

让 VSCode 使用全局 python 配置,而不是虚拟环境?

[英]Make VSCode use global python config, not virtual environment?

I'd like to use Visual Studio Code to edit a Python program.我想使用 Visual Studio Code 来编辑 Python 程序。 To keep it portable, I'd like the Python program to be contained in a Python virtual environment.为了保持可移植性,我希望将 Python 程序包含在 Python 虚拟环境中。 To keep the venv small I'd like to only install those things that the Python program needs (and to specifically NOT install things that VSCode needs).为了保持 venv 小,我只想安装 Python 程序需要的东西(特别是不安装 VSCode 需要的东西)。

I told VSCode which venv to use by setting the "python.pythonPath" variable in the settings.json file for my project (ie, my Workspace Settings, not my User Settings).我通过在 settings.json 文件中为我的项目(即我的工作区设置,而不是我的用户设置)设置“python.pythonPath”变量来告诉 VSCode 使用哪个 venv。 This works fine in the sense that I can run the program / debug the program.这在我可以运行程序/调试程序的意义上工作得很好。

VSCode wants to use flake8 to do some linting, and I 100% support this - I greatly appreciate the Python linting that VSCode & flake8 do :) VSCode 想要使用 flake8 进行一些 linting,我 100% 支持这一点 - 我非常感谢 VSCode 和 flake8 所做的 Python linting :)

However, VSCode does this by installing the flake8 libraries into my program's virtual environment, NOT the computer-wide Python installation.但是,VSCode 是通过将 flake8 库安装到我程序的虚拟环境中来实现的,而不是计算机范围的 Python 安装。

How can I set up VSCode to use the virtual environment for running & debugging the program, but install all the libraries it needs (flake8, rope, etc) into a different / system-wide Python directory?如何设置 VSCode 以使用虚拟环境来运行和调试程序,但将它需要的所有库(flake8、rope 等)安装到不同的/系统范围的 Python 目录中?

You can do the installation of flake8 manually -- eg python3 -m pip install --user flake8 outside of your venv -- and then manually set the path to flake8 in your personal settings.json file.你可以手动安装flake8例如python3 -m pip install --user flake8在你的 venv 之外——然后在你的个人settings.json文件中手动设置flake8路径 That lets you run your code from your venv while installing flake8 for your user account (we don't recommend installing globally).这使您可以在为您的用户帐户安装 flake8 时从您的 venv 运行您的代码(我们不建议全局安装)。

When we create a virtual environment, a file called as pyenv.cfg is created in the virtual environment folder.当我们创建虚拟环境时,会在虚拟环境文件夹中创建一个名为pyenv.cfg文件。 Setting include-system-site-packages = true in this file results in the virtual environment using global site packages.在此文件中设置include-system-site-packages = true导致使用全局站点包的虚拟环境。

在此处输入图片说明

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

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