Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use virtualenv with Python with Visual Studio Code in Ubuntu

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.

{     "editor.rulers": [80,100],     "python.autoComplete.extraPaths": [         "~/dev/venvs/proj",         "~/dev/venvs/proj/lib"      ] } 
like image 272
Jesvin Jose Avatar asked Jun 05 '16 12:06

Jesvin Jose


2 Answers

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.

{     "editor.rulers": [80,100],     "python.pythonPath":"~/dev/venvs/proj/bin/python" } 
like image 75
Don Avatar answered Oct 15 '22 06:10

Don


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.

like image 26
Daniel F. Avatar answered Oct 15 '22 04:10

Daniel F.