Marmalade
Marmalade

Reputation: 121

How can I get my vscode terminal to use my venv for python rather than the system version?

I've set up a venv using python -m venv venv, which is recognised by vscode as a valid interpreter and runs my code as expected using the code-runner vscode extension. But the vscode integrated bash terminal only uses the system version of python, even after running source /Fake/path/to/env/bin/activate. The terminal shows (env) on the input prompt but checking the version through os.path.dirname(sys.executable) shows that the version of python is the one in my system path rather than the one in the venv. So packages I'm installing are being installed to system python rather than the venv.

This is a problem with the terminal in vscode rather than with the venv itself, as when I follow the above steps in a native terminal (non-integrated) I get the correct path to the venv python.

I've tried setting the python.venvPath variable in vscode to either ${workspaceFolder}/env or to fake/path/to/env but neither works. I thought it might be possible to set bash shell arguments under the terminal.integrated.shellArgs.macos setting but haven't been able to work out how to get it to run the correct activate script on startup.For reference, my current vscode settings are below:

"settings": {
    "code-runner.fileDirectoryAsCwd": true;
    "code-runner.executorMap": {
        "python": "env/bin/python3",
    },
    "python.venvPath": "${workspaceFolder}/env",
    "terminal.integrated.cwd": "${workspaceFolder}",
    },
}

Basically, I'm expecting the integrated terminal to activate the venv activate bash script and temporarily replace the path to pick up my venv python as the default. I would then be able to use pip3 to install relevant packages to the venv rather than to the system python. But despite the prompt displaying (env) before the entry the actual behaviour is that it's pointing to the wrong python version when issuing commands e.g. python3 or pip3.

Any advice would be great!

Upvotes: 12

Views: 33608

Answers (7)

Muhammed Yilmaz
Muhammed Yilmaz

Reputation: 31

know its been a bit since you posted but i was having the same problem. After some tinkering i found the problem, so if it helps anyone, for me it was this setting:

"terminal.integrated.defaultProfile.windows": "PowerShell"

for some reason it was set to "null" for me, i guess because i imported settings from another computer. but changing that to "Powershell" now makes terminal stay in the venv even after run time/launch new terminal window.

your on mac so just check for the mac equivalent setting.


it doesnt say your in venv specifically inside the terminal but you can check it by hovering over the terminal window it should say something like "python: actived environment..."

or another way i checked was just pip show pip and it shows the env location for pip rather than global location.


for more context, related settings that people mess with are set like this on my pc (though they really didnt seem to make a difference):

"python.terminal.activateEnvironment": true

"python.terminal.activateEnvInCurrentTerminal": false

Upvotes: 0

AutoM8
AutoM8

Reputation: 69

I had the same issue, tried all kinds of things but this ended up working.

  1. python -m venv myvenv
  2. Open command palette and select interpreter myenv
  3. CTRL + SHIFT + ` to open terminal in myenv
    and then I was able to see the expected... (myenv) C:\Git\pyPilot>

Upvotes: 6

pyrocarm
pyrocarm

Reputation: 91

I had the exact same problem and was never able to get this to work with Powershell with any of these solutions, however when I changed my default VSCode terminal to classic cmd prompt instead, the terminal used the same interpreter as the code.

To change your default terminal in VSCode:

  1. Open Command Palette (Ctrl+Shift+P)
  2. Find the option 'Terminal:Select Default Terminal'enter image description here

3.Select a different terminal from the list of options enter image description here

Hope this helps someone else!

Upvotes: 2

Gordon
Gordon

Reputation: 113

This is an issue in 2021, but if you select (again) the interpreter on the bottom-left corner of VS Code, and then open a new terminal (Ctrl+Shift+`) you'll see the venv name in the terminal prompt.

After closing and opening VS Code, the venv is still active. Maybe it gets deactivated for some reason.

The terminal uses Activate.ps1, not activate.bat.

The current user must be able to execute PoserShell scripts.

Upvotes: 6

Anna Kubasiak
Anna Kubasiak

Reputation: 5

cd ./myenv/Scripts/

activate

worked for me

Upvotes: -3

MyTwoPence
MyTwoPence

Reputation: 1

I am very new to using Python in VSCode, working on a college project. I was having the same issue with the Python version in the integrated Terminal. I am not sure if it will affect anything or break anything else later but for now... I did the following, both worked in that the version of Python changed from 2 (system one) to 3 (Anaconda install).

SECOND ATTEMPT: Changing PATH to have Anaconda first. Would need to be cleaned up... but the basics work.

(base) MacBook-Pro-2:env myuserdir$ echo $PATH

/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/myuserdir/env/bin:/anaconda3/bin:/anaconda3/condabin

(base) MacBook-Pro-2:env myuserdir$ python --version

Python 2.7.10

(base) MacBook-Pro-2:env myuserdir$ PATH="/anaconda3/bin:$PATH"

(base) MacBook-Pro-2:env myuserdir$ echo $PATH

/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/myuserdir/env/bin:/anaconda3/bin:/anaconda3/condabin

(base) MacBook-Pro-2:env myuserdir$ python --version

Python 3.7.1

FIRST ATTEMPT:

source /env/bin/activate within the VSCode Terminal and that changed the Python version.

Open a new integrated Terminal in VSCode: Terminal\New Terminal:

(base) MacBook-Pro-2:env myuserdir$ source /anaconda3/bin/activate

(base) MacBook-Pro-2:env myuserdir$ conda activate base

(base) MacBook-Pro-2:env myuserdir$ python --version
Python 2.7.10

(base) MacBook-Pro-2:env myuserdir$ which python

/usr/bin/python

(base) MacBook-Pro-2:env myuserdir$ pwd

/Users/myuserdir/env/env

(base) MacBook-Pro-2:env myuserdir$ cd ..

(base) MacBook-Pro-2:env myuserdir$ ls

bin             env             include         lib             pyvenv.cfg

(base) MacBook-Pro-2:env myuserdir$ source bin/activate

(env) (base)MacBook-Pro-2:env myuserdir$ which python

/Users/myuserdir/env/bin/python

(env) (base) MacBook-Pro-2:env myuserdir$ python --version

Python 3.7.1

Upvotes: 0

Natsfan
Natsfan

Reputation: 4847

You can try setting your default interpreter. On the very bottom left of the VSCode screen you can click on interpreter name and select the one you want. See image below. The red oval indicates where to click.

enter image description here

Once you click in the space indicated a menu opens where you can select your python interpreter. See image below.

enter image description here

Upvotes: 1

Related Questions