Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Studio code and virtualenv

I am trying to use Visual Studio Code with virtual environment. In the Launch JSON I specify the nosetests launch like this:

{
    "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.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?

like image 969
mikebz Avatar asked Jul 23 '16 18:07

mikebz


People also ask

What is virtual environment in VSCode?

A virtual environment is a folder that contains a copy (or symlink) of a specific interpreter.

How do I create a virtual environment in Visual Studio?

Create a virtual environmentUnder Install packages from file, provide the path to a requirements. txt file if desired. Activates the new environment in the selected project after the environment is created. Automatically sets and activates the virtual environment in any new projects created in Visual Studio.


2 Answers

@mikebz you need to configure the path to the python executable as follows:
"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.
With the next version of VS Code you will no longer have to do this, i.e. you won't have to configure this same setting in two files.

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

like image 64
Don Avatar answered Oct 24 '22 09:10

Don


You need to set up the path for your virtual environments in your workspace settings file: 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:

{
    "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. Then choose the one from the desired virtual environment.

like image 32
alrick87 Avatar answered Oct 24 '22 09:10

alrick87