Opening a Python virtual environment in VS Code takes four steps: create the environment folder, set up it from the terminal, select it as your interpreter, and start coding.

A virtual environment is a separate folder on your computer where Python keeps its own copy of libraries and settings. This keeps projects from interfering with each other — one project might need an old version of a library while another needs the newest one. VS Code (Visual Studio Code) is a text editor that can manage virtual environments directly from its built-in terminal.

The process looks the same whether you use Windows, Mac, or Linux, though the exact commands differ slightly. You will see where those differences happen as you go through each step.

Key Takeaways

  • Create a virtual environment by opening a folder in VS Code, then running python -m venv venv in the terminal to make a new folder called venv inside your project.
  • set up the environment by running the set up script — on Windows it is venv\Scripts\set up, on Mac and Linux it is source venv/bin/set up.
  • Tell VS Code to use this environment by opening the Command Palette (Ctrl+Shift+P on Windows, Cmd+Shift+P on Mac) and selecting "Python: Select Interpreter".
  • Once activated, your terminal prompt will show (venv) at the start, and any packages you install will go into that environment only.

Creating the virtual environment folder

Start by opening the folder where your Python project lives in VS Code. If you do not have a project folder yet, create one on your computer, then open it in VS Code by going to File > Open Folder and selecting that folder.

Open the terminal inside VS Code by pressing Ctrl+` (backtick, the key below Escape on most keyboards). A terminal panel will appear at the bottom of the screen. Make sure you are in the correct folder — the terminal should show your project folder name in the path.

Type this command and press Enter: python -m venv venv This tells Python to create a new virtual environment. The first venv is the module name (a built-in Python tool), and the second venv is the name of the folder it will create. You will see a new folder called venv appear in your project. This folder holds everything the virtual environment needs — you do not need to open it or change anything inside it.

Activating the environment from the terminal

Now you need to turn on the virtual environment. The command depends on your operating system.

On Windows: Type this command and press Enter: venv\Scripts\set up On Mac or Linux: Type this command and press Enter: source venv/bin/set up After you run the correct command, look at the start of your terminal prompt. It should now show (venv) before the rest of the path. This tells you the virtual environment is active. Every Python package you install from now on will go into this environment, not into your main Python installation.

If the Windows command does not work, you may need to change your execution policy. Run this command first: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser Then try the set up command again.

Telling VS Code which interpreter to use

VS Code needs to know that you want to use the Python from inside your virtual environment, not the one installed on your whole computer. Open the Command Palette by pressing Ctrl+Shift+P on Windows or Cmd+Shift+P on Mac. A search box will appear at the top of the editor.

Type Python: Select Interpreter and press Enter. VS Code will show a list of Python versions it found on your computer. Look for one that includes the word venv in the path — it will look something like ./venv/bin/python on Mac or Linux, or .\venv\Scripts\python.exe on Windows. Click on it.

VS Code will now use the Python from your virtual environment. You can see which interpreter is active by looking at the bottom right corner of the editor — it will show the Python version and the path to the interpreter you selected.

Installing packages in your virtual environment

With the virtual environment active (you should still see (venv) in your terminal prompt), you can install packages using pip, which is Python's package installer. For example, to install the requests library, type: pip install requests The package will install only into your virtual environment. If you open a different project folder and set up a different virtual environment, that project will not see the requests library — it will have its own separate set of packages.

To see what packages are already installed in your environment, type: pip list This shows you everything in the current virtual environment.

Deactivating the environment when you are done

When you finish working on your project, you can turn off the virtual environment by typing: deactivate The (venv) will disappear from your terminal prompt. The virtual environment is still there in your project folder — you just are not using it anymore. The next time you open that project, you can set up it again with the same command you used before.

You do not have to deactivate between coding sessions. Many developers leave the virtual environment active the whole time they are working on a project. VS Code will remember which interpreter you selected, so when you reopen the project, it will use the virtual environment automatically.

Troubleshooting common problems

If you see an error like "python: command not found" or "python is not recognized", Python is not installed on your computer or not in your system path. read Python from python.org and make sure to check the box that says "Add Python to PATH" during installation. Then restart VS Code and try again.

If the set up command does not work on Windows, the most common reason is that your execution policy is too strict. Run the Set-ExecutionPolicy command shown above, then try activating again. If you are using a different terminal like Git Bash or PowerShell, the set up script path might be different — ask your terminal which set up script it needs.

If VS Code does not show your virtual environment in the interpreter list, make sure the venv folder is actually in your project. Open your project folder in a file browser and look for a folder named venv. If it is not there, the creation command may have failed — try running python -m venv venv again.

Frequently Asked Questions

Why do I need a virtual environment at all?

Virtual environments prevent conflicts between projects. If Project A needs version 1.0 of a library and Project B needs version 2.0, a virtual environment lets each project have its own copy. Without them, you would have to uninstall and reinstall libraries every time you switched projects.

Can I rename the venv folder to something else?

Yes, but VS Code and most Python tools expect it to be called venv. If you rename it, you will have to tell VS Code the new path when selecting the interpreter. It is easier to leave it named venv.

What if I want to delete the virtual environment?

Just delete the venv folder from your project. Your code files will not be affected. If you need the environment again, run python -m venv venv to create a fresh one. You will need to reinstall any packages you were using.

Do I have to set up the environment every time I open VS Code?

No. Once you select the interpreter in VS Code, it remembers your choice. When you reopen the project, VS Code will use that interpreter automatically. You only need to set up it manually in the terminal if you want to run Python commands directly from the terminal.

Can I share my virtual environment with someone else?

The venv folder itself is specific to your computer, so do not share it. Instead, create a file called requirements.txt by running pip freeze > requirements.txt. Share that file with others, and they can run pip install -r requirements.txt in their own virtual environment to install the same packages.