Open a Python virtual environment in VS Code by creating a folder, running the venv command in your terminal, activating the environment, and then selecting it as your interpreter

A virtual environment is a separate folder on your computer where Python packages live for just one project. Instead of installing everything globally, each project gets its own isolated set of tools. VS Code can work directly with these environments, and the setup takes about five minutes.

The steps below work on Windows, Mac, and Linux. You will create the environment, activate it, and tell VS Code to use it. After that, any packages you install stay contained in that project folder.

Key Takeaways

  • A virtual environment is a folder that holds Python packages for one project only, keeping your system clean and preventing version conflicts between projects.
  • You create a virtual environment by running python -m venv env in your project folder, then activate it with a command that differs by operating system.
  • VS Code will prompt you to select the virtual environment as your interpreter, or you can manually choose it from the Python extension settings.
  • Once activated, any new terminal you open in VS Code will automatically use the virtual environment, and packages you install will stay isolated to that project.

Create a project folder and open it in VS Code

Start by making a new folder on your computer where your project will live. Name it something clear, like my_python_project. Then open VS Code, go to File > Open Folder, and select that folder.

VS Code will show the folder name in the Explorer panel on the left. This folder is now your workspace. Everything you do from here stays inside it, including the virtual environment you are about to create.

Open the terminal and create the virtual environment

In VS Code, open the terminal by pressing Ctrl + ` (backtick) on Windows or Linux, or Cmd + ` on Mac. You should see a command prompt at the bottom of the screen. The terminal should already be pointing to your project folder.

Type this command and press Enter:

python -m venv env

This tells Python to create a virtual environment named env inside your project folder. On some systems, you may need to use python3 instead of python. The command will take a few seconds to run. When it finishes, you will see the prompt return. Do not close the terminal.

If you get an error saying Python is not found, make sure Python is installed on your computer and added to your system PATH. On Windows, you can also try py -m venv env instead.

Activate the virtual environment

The virtual environment now exists, but it is not active yet. You need to run an activation script. The command depends on your operating system.

On Windows: Type this and press Enter:

env\Scripts\activate

On Mac or Linux: Type this and press Enter:

source env/bin/activate

After you run the correct command, look at the terminal prompt. You should see (env) at the beginning of the line. This tells you the virtual environment is now active. Any Python packages you install from now on will go into the env folder, not your system Python.

If you see an error on Windows about execution policies, you may need to run this command first:

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser

Then try the activation command again.

Tell VS Code to use the virtual environment

VS Code should detect your new virtual environment automatically. Look for a notification at the bottom right of the screen asking if you want to select the virtual environment as your interpreter. Click Yes if you see it.

If you do not see a notification, you can select it manually. Click on the Python version number in the bottom right corner of VS Code (it looks like Python 3.x.x). A dropdown menu will appear showing available interpreters. Look for one that says ./env or env and click it.

Once selected, the bottom right will show the path to your virtual environment interpreter. This means VS Code is now using the isolated Python environment for your project.

Verify the setup and start coding

To confirm everything is working, create a new Python file in your project folder. Click File > New File, name it test.py, and type this:

import sysprint(sys.prefix)

Press Ctrl + F5 (or Cmd + F5 on Mac) to run the file. The output should show a path that includes env in it, confirming that Python is running from your virtual environment, not your system installation.

From now on, when you open a new terminal in this VS Code workspace, the virtual environment will activate automatically. You can install packages with pip install package_name, and they will stay isolated to this project.

Deactivate the environment when you are done

When you finish working on your project, you can deactivate the virtual environment by typing deactivate in the terminal and pressing Enter. The (env) prefix will disappear from the prompt.

You do not need to deactivate every time you close VS Code. The environment only stays active in that specific terminal window. If you open a new terminal later, you will need to activate it again with the same command you used before.

The env folder stays on your computer and keeps all your installed packages. You can reactivate it anytime by running the activation command again.

Frequently Asked Questions

Do I need a virtual environment for every Python project?

It is not required, but it is strongly recommended. Virtual environments prevent package version conflicts when you work on multiple projects. If one project needs an older version of a package and another needs a newer version, a virtual environment keeps them separate.

What if I accidentally delete the env folder?

You can recreate it by running python -m venv env again in your project folder. You will need to reinstall your packages with pip install, but your project code is not affected. Keep a requirements.txt file listing your packages so you can reinstall them quickly.

Can I use a different name instead of env?

Yes. Replace env with any name you want in the creation command, like python -m venv venv or python -m venv myenv. Just use the same name in the activation command. Most projects use env or venv by convention.

Why does VS Code not show my virtual environment in the interpreter list?

Make sure the virtual environment was created successfully by checking that the env folder exists in your project. Also confirm that you have the Python extension installed in VS Code. If it still does not appear, try closing and reopening VS Code, or manually navigate to the interpreter by clicking the dropdown and selecting "Enter interpreter path".

Can I share my virtual environment with someone else?

No, virtual environments are tied to your computer's file paths. Instead, create a requirements.txt file by running pip freeze > requirements.txt in your activated environment. Share this file with others, and they can recreate the same environment by running pip install -r requirements.txt in their own virtual environment.