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

P.S.:

  • I have been using Visual Studio Code for a while now and found an another way to show virtual environments in Visual Studio Code.

  • Go to the parent folder in which venv is there through a command prompt.

  • Type code . and Enter. [It is working on both Windows and Linux for me.]

  • That should also show the virtual environments present in that folder.

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.pythonPath": "Your_venv_path/bin/python" under workspace settings.
    (For Windows): Update "python.pythonPath": "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.defaultInterpreterPath instead of python.pythonPath for newer versions.

Leave a Comment