在 Ubuntu 中将 Virtualenv 与 Python 与 Visual Studio Code 结合使用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37642045/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use Virtualenv with Python with Visual Studio Code in Ubuntu
提问by Jesvin Jose
I have a Python project and navigation/autocomplete work perfectly within files in the project. I want to specify a virtualenv so that navigation/autocomplete work with packages inside that virtualenv.
我有一个 Python 项目,导航/自动完成在项目中的文件中完美运行。我想指定一个 virtualenv,以便导航/自动完成与该 virtualenv 中的包一起使用。
I tried this in settings.py but navigation/autocomplete does not work. Also
by setting "python.pythonPath":"~/dev/venvs/proj/bin/python
killed the original navigation/autocomplete.
我在 settings.py 中试过这个,但导航/自动完成不起作用。同样通过设置"python.pythonPath":"~/dev/venvs/proj/bin/python
杀死原始导航/自动完成。
{
"editor.rulers": [80,100],
"python.autoComplete.extraPaths": [
"~/dev/venvs/proj",
"~/dev/venvs/proj/lib"
]
}
回答by Don
With the latest update to the extensionall you need to do is just specify the "python.pythonPath"
as follows.
The values for "python.autoComplete.extraPaths"
will be determined during runtime, but you are still free to specify custom paths in there.
使用扩展的最新更新,您只需指定"python.pythonPath"
如下即可。的值"python.autoComplete.extraPaths"
将在运行时确定,但您仍然可以在其中自由指定自定义路径。
Please, remember to re-start VS Code once the necessary changes have been made.
请记住在进行必要的更改后重新启动 VS Code。
{
"editor.rulers": [80,100],
"python.pythonPath":"~/dev/venvs/proj/bin/python"
}
回答by Daniel F.
As of September 2016 (according to the Github repo documentation of the extension) you can just execute a command from within vscode that will let you select the interpreter from an automatically generated list of known interpreters (including the one in your project's virtual enviroment).
截至 2016 年 9 月(根据扩展的Github 存储库文档),您只需从 vscode 中执行一个命令,即可让您从自动生成的已知解释器列表(包括项目虚拟环境中的解释器)中选择解释器。
How to use this feature?
Select the command
Python: Select Workspace Interpreter
(*) from the command palette(F1).Upon selecting the above command a list of discovered interpreters will be displayed in a
quick pick
list.Selecting an interpreter from this list will update the settings.json file automatically.
如何使用这个功能?
Python: Select Workspace Interpreter
从命令面板(F1) 中选择命令(* )。选择上述命令后,将在
quick pick
列表中显示已发现的解释器列表。从此列表中选择一个解释器将自动更新 settings.json 文件。
(*) This command has been updatedto Python: Select Interpreter
in the latest release of VSC (thanks @nngeek).
Also, notice that your selected interpreter it will be shown at the left side of the statusbar e.g. Python 3.6 64-bit. This is a button you can click to trigger the Select Interpreter feature.
(*)此命令已被更新,以Python: Select Interpreter
在VSC(感谢@nngeek)的最新版本。
另外,请注意,您选择的解释器将显示在状态栏的左侧,例如 Python 3.6 64 位。您可以单击此按钮来触发“选择解释器”功能。
回答by ssword
With the latest Python extension for visual studio code, there is a venvPath Setting:
使用 Visual Studio 代码的最新 Python 扩展,有一个 venvPath 设置:
// Path to folder with a list of Virtual Environments (e.g. ~/.pyenv, ~/Envs, ~/.virtualenvs).
"python.venvPath": "",
On Mac OS X, go to Code > Preferences > Settings
and scroll down to Python Configuration.
在 Mac OS X 上,转到并向下Code > Preferences > Settings
滚动到Python 配置。
Look for "python.venvPath: "",
and click the pencil on the left-hand side to open up your user settings. Finally, add the path to where you store your virtual environments.
查找"python.venvPath: "",
并单击左侧的铅笔以打开您的用户设置。最后,添加存储虚拟环境的路径。
If you are using virtuanenvwrapper, or you have put all your virtual env setting in one folder, this will be the one for you.
如果您正在使用 virtuanenvwrapper,或者您已将所有虚拟环境设置放在一个文件夹中,那么这将是您的最佳选择。
After you have configured "python.venvPath"
, restart VSC. Then open the command palette and look for "Python: Select Interpreter". At this point, you should see the interpreter associated with the virtual environment you just added.
配置完成后"python.venvPath"
,重启VSC。然后打开命令面板并查找“Python: Select Interpreter”。此时,您应该会看到与您刚刚添加的虚拟环境相关联的解释器。
回答by DingLi
Quite Simple with latest VSCode, if you have installed Official Python extension for VSCode
使用最新的 VSCode 非常简单,如果你已经安装 了 VSCode 的官方 Python 扩展
Shift+ Command+ P
Shift+ Command+P
Type : Python: Select Interpreter
类型:Python:选择解释器
Choose your virtual env.
选择您的虚拟环境。
回答by pm.calabrese
回答by Jesvin Jose
Tentative answer. I put the absolute path of the virtual environment python executable as well has the packages. I then restarted VS code.
试探性的回答。我把虚拟环境 python 可执行文件的绝对路径也有包。然后我重新启动了 VS 代码。
I am trying to get ${workspaceRoot} to avoid hardcoding absolute paths, please help if you have succeeded.
我正在尝试获取 ${workspaceRoot} 以避免对绝对路径进行硬编码,如果您成功了,请提供帮助。
{
"editor.rulers": [80,100],
"python.pythonPath": "/home/jesvin/dev/ala/venv/bin/python",
"python.autoComplete.extraPaths": [
"/home/jesvin/dev/ala/venv/lib/python2.7",
"/home/jesvin/dev/ala/venv/lib/python2.7/site-packages"
]
}
回答by Charlie Le
I was able to use the workspace setting that other people on this page have been asking for.
我能够使用此页面上其他人一直要求的工作区设置。
In Preferences, ?+P, search for python.pythonPath
in the search bar.
在首选项中,?+P,python.pythonPath
在搜索栏中搜索。
You should see something like:
你应该看到类似的东西:
// Path to Python, you can use a custom version of Python by modifying this setting to include the full path.
"python.pythonPath": "python"
Then click on the WORKSPACE SETTINGS tab on the right side of the window. This will make it so the setting is only applicable to the workspace you're in.
然后单击窗口右侧的“工作区设置”选项卡。这将使设置仅适用于您所在的工作区。
Afterwards, click on the pencil icon next to "python.pythonPath". This should copy the setting over the workspace settings.
然后,单击“python.pythonPath”旁边的铅笔图标。这应该将设置复制到工作区设置上。
Change the value to something like:
将值更改为类似:
"python.pythonPath": "${workspaceFolder}/venv"
回答by Ajay K
a) Modify Visual Studio Code default virtual env path setting. It's called "python.venvPath". You do this by going into code->settings and scroll down for python settings.
a) 修改 Visual Studio Code 默认虚拟环境路径设置。它被称为“python.venvPath”。您可以通过进入 code->settings 并向下滚动 Python 设置来完成此操作。
b) Restart VS Code
b) 重启 VS 代码
c) Now if you do Shift + Command + P and type Python: Select Interpreter you should see list of your virtual environments.
c) 现在,如果您执行 Shift + Command + P 并输入 Python:选择解释器,您应该会看到您的虚拟环境列表。
回答by b.sodhi
On Mac OSX using Code Version 1.34.0 (1.34.0) I had to do the following to get VSCode to recognise the virtual environments:
在使用代码版本 1.34.0 (1.34.0) 的 Mac OSX 上,我必须执行以下操作才能让 VSCode 识别虚拟环境:
Location of my virtual environment (named ml_venv):
我的虚拟环境的位置(名为 ml_venv):
/Users/auser/.pyvenv/ml_venv
auser@HOST:~/.pyvenv$ tree -d -L 2
.
└── ml_venv
├── bin
├── include
└── lib
I added the following entry in Settings.json: "python.venvPath": "/Users/auser/.pyvenv"
我在 Settings.json 中添加了以下条目: "python.venvPath": "/Users/auser/.pyvenv"
Restarted the IDE, and now I could see the interpreter from my virtual environment:
回答by holms
Seems to be (as by 2018.03) in code-insider, directive been introduced called python.venvFolders:
似乎(截至 2018.03)在 code-insider 中,引入了名为 python.venvFolders 的指令:
"python.venvFolders": [
"envs",
".pyenv",
".direnv"
],
All you need is to add your virtualenv folder name.
您只需要添加您的 virtualenv 文件夹名称。