Reputation: 161
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:
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?
Upvotes: 16
Views: 76957
Reputation: 6558
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
Now it should list out all the configured environments.
Upvotes: 1
Reputation: 133
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.
Upvotes: 11
Reputation: 43
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
Upvotes: 0
Reputation: 217
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.
Upvotes: 4
Reputation: 178
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.
Upvotes: 1
Reputation: 619
When VSCode did not let me select my Python interpreter, I added a defaultInterpreterPath
to settings.json
which I could select then.
settings.json
of your workspace as explained in this SO post."python.defaultInterpreterPath": "/path/to/your/interpreter/python.exe"
to settings.json
(as mentioned in the VSCode docs).Example settings.json
:
{
... some settings here ... ,
"python.defaultInterpreterPath": "c:/python39/python.exe"
}
Upvotes: 11
Reputation: 259
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.
Upvotes: 15
Reputation: 1031
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.
Upvotes: 2
Reputation: 6706
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
Upvotes: 0
Reputation: 15980
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:
python
point at what you wantUpvotes: -1