16

I have installed the first Python interpreter in my Windows PC and the path of python.exe is

C:\Users\myname\AppData\Local\Programs\Python\Python38-32\python.exe 

It worked well originally (running, debugging, etc...).
Recently, I tried to install miniconda in my computer to build different Python environment and the path of python.exe is

D:\miniconda\python.exe 

I followed the tutorial on VScode office to select the conda environment I created. And the Status Bar seems to be correct:

pic

However, if I run the following python code:

import sys sys.executable 

The output is:

C:\Users\myname\AppData\Local\Programs\Python\Python38-32\python.exe 

which doesn't seem to be correct.

I have added both the two path of Python into the Path environment variable in my Windows settings. How to fix this problem?

2
  • How are you running the code? Commented Apr 9, 2020 at 21:43
  • @BrettCannon I just typed python and then run the code above step by step in the VScode terminal Commented Apr 21, 2020 at 16:45

11 Answers 11

17

I just reloaded the python extension which you will see when you go to the vscode and the "python extension" and in that the below "reload required" option will be there just click and then check the "python interpreter" in the "view" again it will resolve the current issue which you are facing.

Sign up to request clarification or add additional context in comments.

3 Comments

I had to click on deactivate for the Reload required option to show up. Then, I was on an SSH session so I had to reinstall locally the extension, and activate in the workspace. And voilà, I could use my env again.
Now it just hangs on "reactivating terminals" forever
It also helps to put path to concrete python version, e.g., venv_location/python3.10 instead of venv_location/python. Then reload the window, then you should be able to select it.
13

The reason for this, at least on Macs, is that the python/python3/python3.9 inside virtual environments is a symlink to the system interpreter in e.g. /opt/homebrew/bin/python3 and VSCode follows the symlink.

So the path for relative imports and all the packages in your virtual environment is now /opt/homebrew/bin instead of ./venv/bin, and so VSCode can't resolve any of the imports from your venv unless they happen to also be installed in /opt/homebrew/bin. This means you lose the "jump to definition" and similar functionality, "run this code" doesn't work, linters can't provide any kind of import-related feedback, etc.

A solution that works is to copy the python binary into the venv, rather than use symlinks. You can do this after you create the venv.

python -m venv venv rm venv/bin/python venv/bin/python3 venv/bin/python3.9 cp /opt/homebrew/bin/python3.9 venv/bin/ ln -s venv/bin/python3.9 venv/bin/python ln -s venv/bin/python3.9 venv/bin/python3 source venv/bin/activate pip install -r your_requirements_file.txt 

Then set the python interpreter in VSCode to venv/bin/python3.9 and everything will work.

This was reported but a fix didn't garner enough votes to be implemented.

https://github.com/microsoft/vscode-python/issues/13603

Of course, change the paths and python versions in the code above as needed.

2 Comments

Ouch, can't believe this is the only solution I found on the web and there are literally no upvotes yet. Are there no python devs with new Mac installations on the interweb? ;)
This is still an issue in 2024. There's no need to copy files around, just enter the filepath manually instead of using Finder when using the VS Code command palette to select the Python interpreter. You can also use venv --copies when creating the virtual environment in the first place. `github.com/microsoft/vscode-python/issues/22755
11

When VSCode did not let me select my Python interpreter, I added a defaultInterpreterPath to settings.json which I could select then.

  1. Open the settings.json of your workspace as explained in this SO post.
  2. Then add "python.defaultInterpreterPath": "/path/to/your/interpreter/python.exe" to settings.json (as mentioned in the VSCode docs).
  3. Then you can select this default interpreter.

Example settings.json:

{ ... some settings here ... , "python.defaultInterpreterPath": "c:/python39/python.exe" } 

Comments

4

I had the same problem. After selecting the interpreter the selected environment did not show in the status bar. It was still saying as 'Select Interpreter'. A simple restart of VScode worked.

3 Comments

Now a restart doesn't work in my case.
May I ask how did you solve this problem? Thank you.
After I restarted, VS code automatically selected the interpreter I was trying to select before restart. I did not do anything specifically
2

You can configure your VSCode workspace settings. Do you need to create a folder named .vscode/ at the source project with the settings.json file inside him. The file content is how below:

{ "python.pythonPath": "path-to-your-venv/bin/python", "editor.formatOnSave": true, "editor.formatOnType": true, "python.linting.lintOnSave": true, "python.linting.flake8Enabled": true, "python.linting.pylintEnabled": true, "python.linting.pylintArgs": ["--load-plugins", "pylint_django"], "python.linting.enabled": true, "editor.rulers": [80], "editor.tabSize": 4, "prettier.singleQuote": true, "editor.defaultFormatter": "ms-python.python", "python.formatting.provider": "autopep8" } 

With your venv activated, do you need to install the libs autopep8 and flake8 with pip:

pip install autopep8 pip install flake8 

Then, restart the VSCode.

I hope help you.

Comments

1

I had the same and it was because site-packages/sitecustomize.py (a script that runs before any other python code) was outputting something, which it isn't supposed to (my fault entirely). Simply deleting the file resolved the issue.

to investigate similar problems I suggest looking at the vscode output, tab "Python", maybe that output gives you a hint. For me it was something like

Failed to get interpreter information for "..." returned bad JSON 

Comments

1

Encountered the same problem. Turns out I can't select interpreter at workspace level so I select it for the work folder and it works.

  1. Press F1 to open the menu. Menu
  2. Select Python:Select Interpreter. If you don't see it, try to type it.
  3. Select the first option, which is your work folder. List of selection
  4. You will find a list of virtual environment and installed python. Select the one you want. List of python environment

Comments

1

This happened on my windows PC. Eventhough I have installed conda as the Virtual environment manager and created a bunch of virtual envs, VSCode didn't recognize any python interpreters.

Short Answer:

Install Extension: Python extension for Visual Studio Code

  • Go to Extensions on VSCode
  • Install python extension for visual studio code.
  • Press CTRL + P select interpreter

Now it should list out all the configured environments.

Comments

0

I had a similar issue because I edited settings.json before while trying to play with live server ( a vs code extension ) However the solution for me was to uninstall python the re-install it from the extensions section in vs code

Comments

0

as mentioned in one answer, this is happening because the python interpreter is a symlink to the global copy. To avoid getting a symlink in the first place, use the --copies flag when creating the environment

python -m venv .venv --copies 

and then you don't have a symlink anymore.

Comments

-1

The value of python in the terminal is entirely disconnected from what you select in VS Code as the terminal controls what is on PATH. You have two options:

  1. Use a virtual environment so the Python extension can activate your terminal to make python point at what you want
  2. Use the green Play button to run your code
  3. Use the run/debug functionality of VS Code

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.