Use Virtualenv with Python with Visual Studio Code in Ubuntu

I have a Python project and the navigation / autocomplete function works fine with the files in the project. I want to tell virtualenv for navigation / autocomplete to work with packages inside this virtualenv.

I tried this in settings.py, but navigation / autocomplete doesn't work. Also 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" ] } 
+83
python virtualenv visual-studio-code
Jun 05 '16 at 12:43 on
source share
12 answers

In the last update of the extension, all you have to do is simply specify "python.pythonPath" as follows. The values ​​for "python.autoComplete.extraPaths" will be determined at runtime, but you can still specify your own paths there.

Please do not forget to restart VS Code after making the necessary changes.

 { "editor.rulers": [80,100], "python.pythonPath":"~/dev/venvs/proj/bin/python" } 
+93
Jun 28 '16 at 9:58
source share
β€” -

As of September 2016 (according to the documentation of the Github repo extension ), you can simply execute the command from vscode, which will allow you to choose a translator from the automatically generated list of famous translators (including the one located in the virtual environment of your project).

How to use this feature?

  • Select the Python: Select Workspace Interpreter command Python: Select Workspace Interpreter (*) from the command palette (F1).

  • After selecting the above command, the list of detected translators will be displayed in the quick pick list.

  • Selecting a translator from this list will automatically update the settings.json file .

(*) This command has been upgraded to Python: Select Interpreter in the latest version of VSC (thanks, @nngeek).
Also note that the interpreter of your choice will appear on the left side of the status bar, for example, Python 3.6 64-bit. This is a button that you can click to activate the translator selection function.

+90
Jan 15 '17 at 2:58
source share

Pretty simple with the latest VSCode if you installed the official Python extension for VSCode

Shift + Command + P

Type: Python: Choose a translator

Choose your virtual environment.

Official site

+34
Jun 08 '17 at 8:16 on
source share

With the latest Python extension for visual studio code, there is a venvPath parameter:

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

On Mac OS X, go to " Code > Preferences > Settings and scroll down to" Python Configuration " .

Find "python.venvPath: "", and click on the pencil on the left to open the user settings. Finally, add the path to the storage location of your virtual environments.

If you use virtuanenvwrapper or you put all the virtual environment settings in one folder, this will be for you.

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

+31
May 3 '17 at 5:51
source share

Another way is to open VSC from a terminal with a virtualenv set and execute F1 Python: Select Interpreter and select the required virtualENV

. virtualENV

+13
Sep 08 '16 at 9:53 on
source share

Preliminary answer. I set the absolute path to the python executable of the virtual environment, as well as the packages. Then I restarted the VS code.

I am trying to get $ {workspaceRoot} to avoid absolute hard coding paths, please help if you succeed.

 { "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" ] } 
+6
Jun 06 '16 at 6:27
source share

I managed to use the workspace settings requested by other people on this page.

In ferences + P Preferences, find 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 WORKPLACE SETTINGS tab on the right side of the window. This will make the setting applicable only to the workspace in which you are located.

After that, 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" 
+4
Mar 20 '18 at 1:58
source share

a) Change the default env virtual path settings for Visual Studio Code. It is called "python.venvPath". This can be done by going to the code-> settings and scrolling down the Python settings.

b) restart the VS code

c) Now, if you pressed Shift + Command + P and typed Python: select Interpreter, you should see a list of your virtual environments.

+2
Apr 18 '18 at 14:34
source share

It seems that (by 2018.03) a directive called python.venvFolders has been introduced in code-insider:

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

All you need to do is add the name of your virtualenv folder.

+1
Mar 16 '18 at 15:40
source share

On Mac OSX using Code Version 1.34.0 (1.34.0), I had to do the following for VSCode to recognize 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"

Restarted the IDE, and now I saw a translator from my virtual environment: enter image description here

+1
Jun 02 '19 at 11:50
source share

I work on Windows and you can find instructions at https://code.visualstudio.com/docs/python/environments. Basically, do this "To select a specific environment, use Python: select the Interpreter command from the command palette (Ctrl + Shift + P)." Then enter Python: S and click on "Choose an interpreter," then select the venv you created.

0
Mar 19 '19 at 21:55
source share

Got it from Youtube Setting up Python VS ... Venv

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

Essentially, open the VS built-in terminal. Then source <your path>/activate.sh , as usual you select venv from the command line. I have a predefined bash function to find & run the required script file, and it worked very well.

I quote this comment YT directly (all thanks 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 . 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 VS Code and use its built-in terminal to cd to the script folder in you virtualenv. 6. type source activates (in vs code I use the git terminal). 7. deactivate to turn off the virtualenv. 

As you can see, it talks about activate.bat . So, if it works on a Mac and also works on Windows, most likely it is quite reliable and portable. πŸ˜€

0
Aug 24 '19 at 23:49
source share



All Articles