90

I have a Python project and navigation/autocomplete work perfectly within files in the project. I want to specify a virtualenv so that navigation/autocomplete work with packages inside that virtualenv.

I tried this in settings.py, but navigation/autocomplete does not work. Also by setting "python.pythonPath":"~/dev/venvs/proj/bin/python killed the original navigation/autocomplete.

{
    "python.autoComplete.extraPaths": [
        "~/dev/venvs/proj",
        "~/dev/venvs/proj/lib"
     ]
}
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Jesvin Jose
  • 22,498
  • 32
  • 109
  • 202
  • a shebang like this also works, if you don't want to mess with settings: #!bin/python3 – Alex R May 29 '17 at 12:08
  • https://youtu.be/6YLMWU-5H9o?t=550 here Dan Taylor explains how to run virtualenv in vscode – test30 Apr 01 '19 at 14:40
  • For latest version of Code, I just clicked the python interpreter at the left bottom, then there came the command palette at the top middle, select Enter path to a Python interpreter, then select Find... then find in ~/your venv path/bin/python, I have python3.8 – JCQian Aug 06 '20 at 03:30

11 Answers11

100

With the latest update to the extension all you need to do is just specify the "python.pythonPath" as follows.

The values for "python.autoComplete.extraPaths" will be determined during runtime, but you are still free to specify custom paths in there.

Please, remember to restart Visual Studio Code once the necessary changes have been made.

{
    "python.pythonPath": "~/dev/venvs/proj/bin/python"
}
Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Don
  • 6,632
  • 3
  • 26
  • 34
93

As of September 2016 (according to the GitHub repository documentation of the extension) you can just execute a command from within Visual Studio Code that will let you select the interpreter from an automatically generated list of known interpreters (including the one in your project's virtual environment).

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.

(*) This command has been updated to Python: Select Interpreter in the latest release of Visual Studio Code (thanks @nngeek).

Also, notice that your selected interpreter will be shown at the left side of the statusbar, e.g., Python 3.6 64-bit. This is a button you can click to trigger the Select Interpreter feature.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Daniel F.
  • 1,760
  • 13
  • 13
  • 3
    sad thing, it doesn't allow me to add a new interpreter to the list... is that even possible? – genuinefafa Mar 18 '17 at 15:58
  • @genuinefafa I don't know a lot about Code, but in the repo documentation I linked it states that the interpreters are discovered searching in standard paths (/usr/local/bin, /usr/sbin, /sbin), virtual enviroments under the current project and Conda enviroments. Maybe you could add your interpreter to one of this locations so it can pick it up automatically. But I'm just guessing. – Daniel F. Apr 05 '17 at 01:45
  • That's a good idea @DanielF. I solve it but manually setting up the directories pointing to the project environment. The `$PATH` idea didn't work (actually is what a `workon` script should do). This was tested a few weeks ago, not sure if the solve it in new releases. – genuinefafa Apr 06 '17 at 17:50
  • 4
    To find your virtualenv, `python.venvPath` needs to be set. – Mahmoud Hanafy Apr 21 '17 at 15:12
  • @genuinefaf use miniconda to add an environment to list. https://conda.io/docs/_downloads/conda-cheatsheet.pdf – johndpope Jun 18 '17 at 21:38
  • @MahmoudHossam not sure where it should be set – genuinefafa Jun 21 '17 at 13:14
  • @johndpope isn't conda an alternative to virtualenv? – genuinefafa Jun 21 '17 at 13:15
  • miniconda seems to be transparent with virtualenv. Here's a gist I prepared earlier - note you want to stop using pip install / and do conda install instead. https://gist.github.com/johndpope/187b0dd996d16152ace2f842d43e3990 – johndpope Jun 21 '17 at 16:23
  • 2
    As of Dec 2017 with code 1.18.1, the command is `Python: Select Interpreter` from the command palette. Struggled for a while to search `python workspace` from the command list ... – nngeek Dec 05 '17 at 09:07
  • But this does not select the current interpreter of your virtualenv, you need to run `which python` and add the directory in your settings `python.pythonPath` – Shift 'n Tab Mar 18 '18 at 04:45
  • @Shift'NTab I think it depends on how you make the virtual environment. For instance, (in my experience) when using python's 'venv' library vscode didn't recognize a diferent VE. But when using 'virtualenv' it did (I think venv doesn't use a different-copy binary for python and virtualenv does see here https://imgur.com/a/Aw5dj). – Daniel F. Mar 21 '18 at 02:48
38

Quite simple with the latest version of Visual Studio Code, if you have installed the official Python extension for Visual Studio Code:

Shift + Command + P

Type: Python: Select Interpreter

Choose your virtual environment.

Official site

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DingLi
  • 791
  • 10
  • 19
  • 14
    Unfortunately, this command fails to list local virtual envs. – Antonin Apr 10 '18 at 14:58
  • @Antonin I test on my vscode on latest version, it works. Are you in mac or else system, and what's your error? – DingLi Apr 11 '18 at 05:32
  • 1
    Indeed on a Mac, thank you @DingLi, it did work after setting up this : `"python.venvFolders": [ "${rootPath}", "envs", ".pyenv", ".direnv" ],` – Antonin Apr 12 '18 at 09:42
  • Hi @Antonin , what's `${rootPath}`? I found `${workspaceFolder}` only. https://code.visualstudio.com/docs/editor/variables-reference – LShi May 07 '18 at 03:23
  • @LShi ${rootPath} seems to be provided by python extension for the root path of the current project. – Antonin May 10 '18 at 23:34
  • 1
    @Antonin for the virtual envs, see the ssword's answer – carton.swing Jul 06 '18 at 17:40
  • 3
    Yes, I really wish you, and everyone else, stopped saying "quite simple". Virtual envs are not being listed, only interpreters, 2019.6 macOS version. And this is a question saying virtualenv, not just python interpreters. @ssword has the correct answer. Or at least I hope that it will bring the rest of the virtualenv-based configuration, which is NOT just the python executable to use. – JL Peyret Jun 20 '19 at 20:16
  • I suspect that, in my case at least, one reason may be that my source folder and the virtual env are siblings. i.e. **top/mysrc**, **top/venv**. It might be that this command would find venv if it was **top/mysrc/venv** – JL Peyret Jun 20 '19 at 20:27
35

With the latest Python extension for Visual Studio Code, there is a venvPath Setting:

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

On Mac OS X, go to CodePreferencesSettings and scroll down to Python Configuration.

Look for "python.venvPath: "", and click the pencil on the left-hand side to open up your user settings. Finally, add the path to where you store your virtual environments.

If you are using virtuanenvwrapper, or you have put all your virtual environment setting in one folder, this will be the one for you.

After you have configured "python.venvPath", restart Visual Studio Code. Then open the command palette and look for "Python: Select Interpreter". At this point, you should see the interpreter associated with the virtual environment you just added.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ssword
  • 905
  • 10
  • 13
13

Another way is to open Visual Studio Code from a terminal with the virtualenv set and need to perform F1 Python: Select Interpreter and select the required virtualenv.

virtualenv

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
pm.calabrese
  • 386
  • 6
  • 10
  • 8
    This doesn't appear to work for me. I have to have the settings file. Is there something I'm missing? – McKay Feb 10 '17 at 22:27
  • 1
    I used this method successfully but had to do code -a . – YoFlynn Jan 27 '18 at 16:03
  • 1
    This got me the right python interpreter, but it didn't find the rest of my venv. Adding my venv directory name "ve" to python.venvFolders fixed it. – user1055568 Apr 12 '18 at 15:22
  • 1
    My previous comment was wrong. I was having other problems that I mistook for not finding venv. – user1055568 Apr 12 '18 at 16:00
7

I put the absolute path of the virtual environment Python executable as well has the packages. I then restarted Visual Studio Code.

I am trying to get ${workspaceRoot} to avoid hardcoding absolute paths.

{
    "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"
     ]
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jesvin Jose
  • 22,498
  • 32
  • 109
  • 202
4

I was able to use the workspace setting that other people on this page have been asking for.

In Preferences, ⌘+P, search for python.pythonPath in the search bar.

You should see something like:

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

Then click on the WORKSPACE SETTINGS tab on the right side of the window. This will make it so the setting is only applicable to the workspace you're in.

Afterwards, click on the pencil icon next to "python.pythonPath". This should copy the setting over the workspace settings.

Change the value to something like:

"python.pythonPath": "${workspaceFolder}/venv"
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Charlie Le
  • 41
  • 4
2

a) Modify Visual Studio Code default virtual env path setting. It's called "python.venvPath". You do this by going into code->settings and scroll down for python settings.

b) Restart VS Code

c) Now if you do Shift + Command + P and type Python: Select Interpreter you should see list of your virtual environments.

Ajay K
  • 21
  • 1
1

It seems to be (as of 2018.03) in code-insider. A directive has been introduced called python.venvFolders:

  "python.venvFolders": [
    "envs",
    ".pyenv",
    ".direnv"
  ],

All you need is to add your virtualenv folder name.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
holms
  • 9,112
  • 14
  • 65
  • 95
1

On Mac OS X using Visual Studio Code version 1.34.0 (1.34.0) I had to do the following to get Visual Studio Code to recognise the virtual environments:

Location of my virtual environment (named ml_venv):

/Users/auser/.pyvenv/ml_venv

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

I added the following entry in Settings.json: "python.venvPath": "/Users/auser/.pyvenv"

I restarted the IDE, and now I could see the interpreter from my virtual environment:

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
b.sodhi
  • 93
  • 4
  • 10
0

I got this from YouTube Setting up Python Visual Studio Code... Venv

OK, the video really didn't help me all that much, but... the first comment under (by the person who posted the video) makes a lot of sense and is pure gold.

Basically, open up Visual Studio Code' built-in Terminal. Then source <your path>/activate.sh, the usual way you choose a venv from the command line. I have a predefined Bash function to find & launch the right script file and that worked just fine.

Quoting that YouTube comment directly (all credit to aneuris ap):

(you really only need steps 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.

As you may notice, he's talking about activate.bat. So, if it works for me on a Mac, and it works on Windows too, chances are it's pretty robust and portable.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
JL Peyret
  • 10,917
  • 2
  • 54
  • 73