Search code examples
pythonvisual-studio-codejupytervirtual-environment

Visual Studio Code does not detect Virtual Environments


Visual Studio Code does not detect virtual environments. I run vscode in the folder where the venv folder is located, when I try to select the kernel in vscode I can see the main environment and one located elsewhere on the disk. Jupyter running in vscode also doesn't see this environment. I have installed ipykernel in this environment. I tried to reinstall vscode and python extension.

I tried to set the path in settings.json inside .vscode:

{
    "python.pythonPath": ".\\venv\\Scripts\\python.exe"
}

Windows 10
Python 3.6.7 (64-bit)
VSCode 1.54.3


Solution

    1. In VSCode open your command palette — Ctrl+Shift+P by default

    2. Look for Python: Select Interpreter

    3. In Select Interpreter choose Enter interpreter path... and then Find...

    4. Navigate to your venv folder — eg, ~/pyenvs/myenv/ or \Users\Foo\Bar\PyEnvs\MyEnv\

    5. In the virtual environment folder choose <your-venv-name>/bin/python or <your-venv-name>/bin/python3


    The issue is that VSCode's Python extension by default uses the main python or python3 program while venv effectively creates a "new" python/python3 executable (that is kind of the point of venv) so the extension does not have access to anything (available modules, namespaces, etc) that you have installed through a venv since the venv specific installations are not available to the main Python interpreter (again, this is by design—like how applications installed in a VM are not available to the host OS)