pythonvisual-studio-codevirtualenv

How can I set up a virtual environment for Python in Visual Studio Code?


In my project folder I created a venv folder:

python -m venv venv

When I run command select python interpreter in Visual Studio Code, my venv folder is not shown. I went one level up like suggested here, but Visual Studio Code doesn't see my virtual interpreter.

What did I miss?


Solution

  • Original Answer

    I almost run into same problem every time I am working on Visual Studio Code using venv. I follow the below steps:

    1. Go to menu FilePreferencesSettings.

    2. Click on Workspace settings.

    3. Under Files:Association, in the JSON: Schemas section, you will find Edit in settings.json. Click on that.

    4. Update "python.defaultInterpreterPath": "Your_venv_path/bin/python" under workspace settings. (For Windows): Update "python.defaultInterpreterPath": "Your_venv_path\Scripts\python.exe" under workspace settings.

    5. Restart Visual Studio Code in case if it still doesn't show your venv.

    Note: Use python.pythonPath instead of python.defaultInterpreterPath for older versions.