使用Visual Studio Code与Python一起使用virtualenv

91

我有一个Python项目,在该项目的文件中,导航/自动补全功能能够完美地工作。我希望指定一个virtualenv,以便在该虚拟环境中的包中也能正常使用导航/自动补全功能。

我尝试在settings.py中进行设置,但导航/自动补全功能无法正常工作。同时,通过设置"python.pythonPath":"~/dev/venvs/proj/bin/python会破坏原有的导航/自动补全功能。

{
    "python.autoComplete.extraPaths": [
        "~/dev/venvs/proj",
        "~/dev/venvs/proj/lib"
     ]
}

像这样的shebang也可以工作,如果你不想去纠结设置:#!bin/python3 - Alex R
在这个视频中,丹·泰勒解释了如何在VSCode中运行virtualenv。 - test30
对于代码的最新版本,我只需点击左下角的Python解释器,然后在中上部出现命令面板,选择“输入Python解释器路径”,然后选择“查找...”,然后在~/your venv path/bin/python中找到python3.8。 - JCQian
11个回答

0

我从YouTube Setting up Python Visual Studio Code... Venv得到了这个信息。

好的,这个视频并没有帮助我很多,但是……第一个评论(发布该视频的人)非常有意义,而且十分珍贵。

基本上,打开Visual Studio Code内置的终端。然后使用命令行中选择虚拟环境的通常方式,输入source <your path>/activate.sh。我有一个预定义的Bash函数来查找和启动正确的脚本文件,这很有效。

引用该YouTube评论(所有信用归aneuris ap所有):

(你真的只需要步骤5-7)

1. Open your command line/terminal and type `pip virtualenv`.
2. Create a folder in which the virtualenv will be placed in.
3. 'cd' to the script folder in the virtualenv and run activate.bat (CMD).
4. Deactivate to turn of the virtualenv (CMD).
5. Open the project in Visual Studio Code and use its built-in terminal to 'cd' to the script folder in you virtualenv.
6. Type source activates (in Visual Studio Code I use the Git terminal).
7. Deactivate to turn off the virtualenv.

正如你可能已经注意到的那样,他在谈论“activate.bat”。因此,如果它对我在 Mac 上有效,并且在 Windows 上也有效,则很有可能它是非常强大和便携的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接