dave vedant
dave vedant

Reputation: 727

VS Code does not change python environment

I am using VS-Code and anaconda environment for python interpreter. I select the exact anaconda base environment by ctrl + shift + ` and it also reflects in the downside panel of vscode. But, when I checked the python version it shows my system's default python environment 3.7.9. If you see the below screenshot than, the anaconda environment is with 3.8.3.

Please give me solution, enter image description hereThank you.

Upvotes: 6

Views: 25184

Answers (10)

LIU YUE
LIU YUE

Reputation: 2007

here is how I resolved it with step by step investigation:

  1. first, check all the versions you have installed for example open uninstall program to check all the versions available, uninstall those you don't need

  2. second, find the path to all your existing python versions, default should be "%AppData%\Local\Programs\Python\PythonXXX" for windows users

  3. open "edit system environment", make sure you have the correct python path configured, for example in PATH you have path to python and pip:

"%AppData%\Local\Programs\Python\Python312"

"%AppData%\Local\Programs\Python\Python312\Scripts"

  1. open powershell or cmd, execute:

    python -V

    pip list -v

make sure both are consistent with what you configured in step 3

  1. close all the vscode instances if reload window not work, close all and open up again, then the interpreter selection should work

  2. for best practice, try the virtual env introduced in the link below

refer to:

Python environments in VS Code

python programming

Upvotes: 0

Happy Dent
Happy Dent

Reputation: 1

I don't have the conventional way of doing that but when I faced this issue I simply noted the python version that it was showing and then I deleted it as soon as I deleted it the system made another python version as the default python version and then I reinstalled the python version that I deleted and it worked. like I was able to change the python version in VS code.

Upvotes: 0

user49404
user49404

Reputation: 917

I was able to fix this issue by removing the python.defaultInterpreterPath setting from my settings.json. May be worth checking your user settings if this problem persists.

Another thing I realized is that I have to right-click on the python file and click Run Python file in Terminal, if I just run it with a python command in the powershell window, it's not using the right python executable.

Upvotes: 0

Unergy
Unergy

Reputation: 1

In my case helped to uncheck at User settings -> Extensions -> Python:

  • Terminal: Activate Env In Current Terminal
  • Terminal: Activate Environment

but I don`t know why.

Upvotes: 0

user12378033
user12378033

Reputation: 122

For me it worked by changing the final step in the selection process:

The start is as usual:

  • Command Palette
  • > Python: Select Interpreter
  • + Enter interpreter path

but then, not picking the option

  • Find... Browse your file system to find a Python interpreter (which opens a file explorer to select the interpreter)

but instead

  • directly enter the full path in the text field and press Enter.

In theory, both should have the same effect, but for my case they did not. (I had tried reloading the window, restarting vscode, adding the venv to the known venv folders in the workspace settings, even recreating the venv in case something went wrong there, but none of these options worked.)

Upvotes: 8

Ankit Kr
Ankit Kr

Reputation: 51

If nothing worked from the above-suggested solution, you could try this.

Create a virtual environment in anaconda and activate it

conda create -n <environment_name> python=3.8.3

conda activate <environment_name>

Then you can directly open VS Code from same Anaconda terminal by typing

code

In the opened VS Code, you will see python 3.8.3 will be activated. Instead of the default system python 3.7.9

Upvotes: 0

TaskerBliss
TaskerBliss

Reputation: 81

For those who have tried these steps:

  • select different interpreter
  • reboot VScode
  • reinstall VScode Python extension and delete its folders

and have achieved nothing.

Probably you are working in the workspace and not in folder. You may have set interpreter at workspace level, which can't be used in one of the folders of the workspace. Try opening your folder separately from the workspace and select interpreter you want. This worked for me.

Upvotes: 6

smoothumut
smoothumut

Reputation: 3491

after changing the environment, you can restart the vs code again. it might be changed now. if not, then try changing now againg by clicking the interpreter name which is displayed on left bottom of the vscode window

Upvotes: 2

SupremeDeity
SupremeDeity

Reputation: 98

Changing the version in VSCode does not change the the instance that your PS instance will use. Try doing where python to see where the V3.7.9 that your PS instance is picking up is. Then remove that version from the environment variables and add the path to the V3.8.3 instead.

Additionally you can do: To forcefully use v3.8.3

  1. Specify python version in command
py -3.8 <command>
  1. OR set PY_PYTHON environment variable to set which version to use. Take a look at this for further help Python docs

Upvotes: 2

user13824946
user13824946

Reputation:

To check & change vs code interpreter:

  • In top left menu bar Click view
  • In the dropdown menu, Click Command Palette
  • Click Python: Select Interpreter
  • Choose & Click on your desired Interpreter

Another way to be sure to use anconda interpreter, open anaconda navigator and launch vs code from there.

original vs code How-To

Upvotes: -3

Related Questions