简体   繁体   English

Visual Studio代码和virtualenv

[英]Visual Studio code and virtualenv

I am trying to use Visual Studio Code with virtual environment. 我正在尝试将Visual Studio代码与虚拟环境一起使用。 In the Launch JSON I specify the nosetests launch like this: 在Launch JSON中,我指定了如此的nosetests启动:

{
    "name": "nosetests",
    "type": "python",
    "request": "launch",
    "stopOnEntry": true,
    "program": "${workspaceRoot}/env/dev/bin/nosetests",
    "args": [
        "--nocapture",
        "tests"
    ],
    "externalConsole": false,
    "debugOptions": [
        "WaitOnAbnormalExit",
        "WaitOnNormalExit"
    ]
},

However when I launch the environment variables do not get picked up. 但是,当我启动环境时,变量不会被拾取。 I've tried setting up the python path in workspace settings: 我已经尝试在工作区设置中设置python路径:

"python.pythonPath": "${workspaceRoot}/env/dev/bin/python"

but it doesn't seem to set the right environment. 但它似乎没有设置合适的环境。 There needs to be something that' the equivalent of source activate. 需要有相当于源激活的东西。 Has anyone figured it out? 有没有人想出来?

@mikebz you need to configure the path to the python executable as follows: @mikebz你需要配置python可执行文件的路径,如下所示:
"pythonPath":"${workspaceRoot}/env/dev/bin/python"

The path may not be 100% accurate (please double check it), but that's how you need to configure it in launch.json. 路径可能不是100%准确(请仔细检查),但这是您需要在launch.json中配置它的方式。
With the next version of VS Code you will no longer have to do this, ie you won't have to configure this same setting in two files. 使用下一版本的VS Code,您将不再需要这样做,即您不必在两个文件中配置相同的设置。

More details on configuring the path for debugging can be found here: https://github.com/DonJayamanne/pythonVSCode/wiki/Python-Path-and-Version#python-version-used-for-debugging 有关配置调试路径的更多详细信息,请访问: https//github.com/DonJayamanne/pythonVSCode/wiki/Python-Path-and-Version#python-version-used-for-debugging

As 2018.03 there's an article how to add virtualenv to your python path list per particular workspace: https://code.visualstudio.com/docs/python/environments 作为2018.03,有一篇文章如何将virtualenv添加到每个特定工作区的python路径列表中: https ://code.visualstudio.com/docs/python/environments

All you have to do is File->Save workspace as.. and then add to the settings of workspace virtualenv: 您所要做的就是File->Save workspace as..然后添加到工作区virtualenv的设置:

{
    "folders": [
        {
            "path": "."
        }
    ],
    "settings": {
        "python.pythonPath": "${workspaceFolder}/.venv/bin/python"
    }
}

You need to set up the path for your virtual environments in your workspace settings file: https://code.visualstudio.com/docs/python/environments 您需要在工作区设置文件中设置虚拟环境的路径: https//code.visualstudio.com/docs/python/environments

Save your workspace then open <workspace_name>.code-workspace file and add your virtual environment folder to the settings: 保存工作区,然后打开<workspace_name>.code-workspace文件并将虚拟环境文件夹添加到设置中:

{
    "folders": [
        {
            "path": "<path-to-your-workspace>"
        }
    ],
    "settings": {
        "python.venvPath": "<path-to-your-virtual-env-folder>"
    }
}

After that open command palette (CMD/CTRL + SHIFT + P) and type select interpreter . 在那之后打开命令调色板(CMD / CTRL + SHIFT + P)并键入select interpreter Then choose the one from the desired virtual environment. 然后从所需的虚拟环境中选择一个。

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

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