pythonvisual-studio-codepython-venv

How to know if Virtual Environment is working in VS Code?


I’ve read through VS Code’s documentation on creating a virtual environment and I’ve used the venv command to do so, but how do I know if my current Python files are working using that Virtual Environment?

In the folder I’m saving my .py files in, I see the .venv folder which was created. Is that it or is there more to it?

I’ve used VS Code’s terminal commands and followed the documentation instructions, but there is no documentation to tell me exactly what I’m supposed to see when it works, so I don’t know if I’m done.


Solution

  • You want to check if you are in the venv or not:

    Type which python into VS Code's integrated terminal.

    If you are using the venv, it will show the path to the python executable inside the venv. For example, for me, the .venv folder is located in /Users/lion/example_folder, but for you the path would be different.

    Example of which python in the venv

    If the path does not include the .venv folder inside your Python Projects folder, then you are not inside the venv. For example, the path shown by the command is /usr/bin/python, but the .venv is at /user/Python Projects.

    You are not inside the venv and you want to activate it

    Inside VS Code's terminal, type source .venv/bin/activate (or specify the absolute path source ~/example_folder/.venv/bin/activate).