简体   繁体   中英

Select Python interpreter does not work in VScode

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?

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.

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.

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"
}

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

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

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.

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

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 eg /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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM