aguycalledmax
aguycalledmax

Reputation: 589

VSCode terminal shows incorrect python version and path, launching terminal from anaconda works perfectly

I have been stuck on this one problem for hours now and believe I have tried everything outside of throwing my computer out of the window.

I have a virtual environment set up on Anaconda using python version 3.7 and Django version 2.1. If I activate this virtual environment from Anaconda everything works smoothly.

(movierecommender) bash-3.2$ python -V
Python 3.7.2
(movierecommender) bash-3.2$ python -m django --version
2.1.5

However when I try to activate the environment from a vscode terminal I get

(movierecommender) maxs-MBP:movies maxswann$ python -V
Python 2.7.10
(movierecommender) maxs-MBP:movies maxswann$ python -m django --version
/usr/bin/python: No module named django

I have Python 3.7.2 64-bit ('movierecommender':conda) showing as my python interpreter in the bottom left of my vscode window yet still get the wrong python version

I thought this may be to do with the PYTHONPATH but have tried unsetting and resetting even though I should not have to worry about this in Anaconda as it automatically adds: "python.pythonPath":"/Users/maxswann/anaconda3/envs/movierecommender/bin/python" to a settings.json.vscode file

using:

python -c "import sys; print(sys.path)"

Anaconda-launched terminal

['', '/Users/maxswann/anaconda3/envs/movierecommender/lib/python37.zip', '/Users/maxswann/anaconda3/envs/movierecommender/lib/python3.7', '/Users/maxswann/anaconda3/envs/movierecommender/lib/python3.7/lib-dynload', '/Users/maxswann/anaconda3/envs/movierecommender/lib/python3.7/site-packages']

Vs Code terminal

['', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python', '/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC']

As you can see it seems to be using the default mac OS python version.

Has anybody else had this problem before? I've been tearing hair out all day trying to fix this

Upvotes: 41

Views: 35012

Answers (8)

Chancelor Roberts
Chancelor Roberts

Reputation: 21

For me it was as simple as running source ~/.bash_profile instead of source ~/.zshrc. I'm still not sure why VScode was the only one grabbing a weird version of python when I was running the latter.

Upvotes: 0

tikej
tikej

Reputation: 312

For me simply changing the Python interpreter inside VS Code worked. You still can take advantage of Pyenvs installations there: View -> Command Palette -> Select Python Interpreter

Upvotes: 0

Enthus3d
Enthus3d

Reputation: 2165

Windows Solution:

If anyone in the future ends up scratching their heads over this particular problem, I’ve found another culprit, which blocked me for this problem on Windows:

Showcase of setting Terminal>Integrated>Env: Enable Persistent Sessions

I suspect what happens is that after you update system paths, VScode caches the old path in the terminal and persists it. In this case it persists the old python path rather than the new conda one.

Toggling this option off and restarting VSCode clears that cache, and the new path is loaded in. You can also toggle the option back on after you’re done.

Upvotes: 2

Anis R.
Anis R.

Reputation: 6902

For Windows users:

First if you haven't done already, set VS code (the editor, not its terminal) to the desired Python environment, using Ctrl+Shift+P --> Python: Select interpreter.

Then, Change VS code's default terminal from Powershell to CMD. This is what worked for me at least.

Upvotes: 3

Mike Williamson
Mike Williamson

Reputation: 3158

The officially accepted answer by @Samuel was the correct answer at the time.

But VS Code has now provided a better way to handle it.

In short, open up your user settings and add this line of code:

    "terminal.integrated.inheritEnv": false,

This prevents stomping over whatever Python environment manager you are using (eg, venv, conda, etc).

Upvotes: 36

Sheikh Abdul Wahid
Sheikh Abdul Wahid

Reputation: 2773

I ran this script. Now python3 is running from virtual env. [Windows 10]

pip3 install virtualenv
virtualenv env
call ".\env\Scripts\activate.bat"
set requirements="./Requirements.txt"
pip3 install -r %requirements%
python

Upvotes: -1

Samuel Tatipamula
Samuel Tatipamula

Reputation: 631

I have been facing the exact same problem. Finally found a workaround from a forum (https://github.com/Microsoft/vscode-python/issues/4434#issuecomment-466600591)

As long as you ADD some stuff to configuration, terminal.integrated.env.osx, the content will be appended to PATH after shell initialization(source bash_profile or zshrc). In my Mojave, I simply add following empty entry to my user configuration:

"terminal.integrated.env.osx": {
        "PATH": ""
}

Then the $PATH will be the same as the external terminal.

Upvotes: 42

Mike C.
Mike C.

Reputation: 1921

I just ran into the same problem. Try switching out of the powershell terminal to the windows terminal. Then restart. It should restart with the anaconda terminal. If that does not work you could:

First change the default terminal from within Visual Code to the CMD terminal instead of Powershell. Add the following code to a batch file.

call "c:\path\to\anaconda3\Scripts\activate"

Then I named the batch file and saved it to my root directory. In my case snake.bat. Now when I launch my CMD terminal I just type c:\snake.bat and the CMD prompt changes into an Anaconda prompt.

Upvotes: 1

Related Questions