Reputation: 121
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
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
Reputation: 69
I had the same issue, tried all kinds of things but this ended up working.
Upvotes: 6
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:
3.Select a different terminal from the list of options
Hope this helps someone else!
Upvotes: 2
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
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).
(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
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
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.
Once you click in the space indicated a menu opens where you can select your python interpreter. See image below.
Upvotes: 1