使用 virtualenv Python Visual Studio Ubuntu 中的代码

Use virtualenv with Python with Visual Studio Code in Ubuntu

我有一个 Python 项目并且 navigation/autocomplete 在项目的文件中工作得很好。我想指定一个 virtualenv 以便 navigation/autocomplete 使用该 virtualenv 中的包。

我在 settings.py 中试过了,但是 navigation/autocomplete 不起作用。同样通过设置"python.pythonPath":"~/dev/venvs/proj/bin/python杀死了原来的navigation/autocomplete.

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

我放了虚拟环境的绝对路径 Python 可执行文件还有包。然后我重新启动 Visual Studio 代码。

我正在尝试获取 ${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"
     ]
}

使用最新的 update to the extension,您只需按如下方式指定 "python.pythonPath"

"python.autoComplete.extraPaths" 的值将在运行时确定,但您仍然可以在其中自由指定自定义路径。

请记得在完成必要的更改后重新启动 Visual Studio 代码。

{
    "editor.rulers": [80,100],
    "python.pythonPath":"~/dev/venvs/proj/bin/python"
}

另一种方法是从设置了 virtualenv 的终端打开 Visual Studio 代码,需要执行 F1 Python: Select Interpreter 和 select需要 virtualenv.

截至 2016 年 9 月(根据 GitHub repository documentation of the extension),您只需从 Visual Studio 代码中执行命令,即可让您 select 自动生成列表中的解释器已知口译员(包括项目虚拟环境中的口译员)。

How can I 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.

(*) 此命令已在最新版本的 Visual Studio 代码中更新Python: Select Interpreter(感谢@nngeek)。

另外,请注意您的 selected 解释器将显示在状态栏的左侧,例如 Python 3.6 64 位。您可以单击此按钮来触发 Select Interpreter 功能。

对于 Visual Studio 代码的最新 Python 扩展,有一个 venvPath 设置:

// Path to folder with a list of Virtual Environments (e.g. ~/.pyenv, ~/Envs, ~/.virtualenvs).
  "python.venvPath": "",

在 Mac OS X 上,转到 代码首选项设置 并向下滚动到 Python 配置 .

查找 "python.venvPath: "", 并单击左侧的铅笔以打开您的用户设置。最后,添加存储虚拟环境的路径。

如果您使用的是 virtuanenvwrapper,或者您已将所有虚拟环境设置放在一个文件夹中,那么这就是适合您的文件夹。

配置 "python.venvPath" 后,重新启动 Visual Studio 代码。然后打开命令面板并查找“Python: Select Interpreter”。此时,您应该会看到与刚刚添加的虚拟环境关联的解释器。

使用最新版Visual Studio代码很简单,如果你已经安装了the official Python extension for Visual Studio Code:

Shift + Command + P

类型:Python:Select 解释器

选择您的虚拟环境。

Official site

code-insider好像是(截至2018.03)。引入了一个名为 python.venvFolders:

的指令
  "python.venvFolders": [
    "envs",
    ".pyenv",
    ".direnv"
  ],

您只需添加您的 virtualenv 文件夹名称即可。

我能够使用此页面上其他人一直要求的工作区设置。

在首选项中,⌘+P,在搜索栏中搜索 python.pythonPath

您应该会看到如下内容:

// Path to Python, you can use a custom version of Python by modifying this setting to include the full path.
"python.pythonPath": "python"

然后单击 window 右侧的 WORKSPACE SETTINGS 选项卡。这将使设置仅适用于您所在的工作区。

然后,单击 "python.pythonPath" 旁边的铅笔图标。这应该将设置复制到工作区设置上。

将值更改为:

"python.pythonPath": "${workspaceFolder}/venv"

a) 修改 Visual Studio 代码默认虚拟环境路径设置。它被称为"python.venvPath"。您可以通过进入代码-> 设置并向下滚动找到 python 设置来执行此操作。

b) 重启 VS 代码

c) 现在,如果您执行 Shift + Command + P 并键入 Python: Select Interpreter 你应该看到你的虚拟环境列表。

在 Mac OS X 使用 Visual Studio 代码版本 1.34.0 (1.34.0) 我必须执行以下操作才能获得 Visual Studio 代码来识别虚拟环境:

我的虚拟环境的位置(名为 ml_venv):

/Users/auser/.pyvenv/ml_venv

auser@HOST:~/.pyvenv$ tree -d -L 2
.
└── ml_venv
    ├── bin
    ├── include
    └── lib

我在 Settings.json 中添加了以下条目:"python.venvPath": "/Users/auser/.pyvenv"

我重新启动了 IDE,现在我可以从我的虚拟环境中看到解释器了:

我从 YouTube Setting up Python Visual Studio Code... Venv

那里得到了这个

好吧,视频确实对我帮助不大,但是...(发布视频的人)下面的第一条评论很有道理 并且是纯金。

基本上,打开Visual Studio代码的内置终端。然后source <your path>/activate.sh,你从命令行选择一个venv的通常方式。我有一个预定义的 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 上也有效,那么它很可能非常健壮且便于携带。