Select Python interpreter does not work in VScode

Viewed 17500

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?

7 Answers

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.

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

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.

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

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
Related