How to Create a Python Virtual Environment in VS Code (Windows, macOS & Linux)

Educational illustration explaining Python virtual environments in VS Code, with a .venv folder, code editor and package isolation steps.

When you install Python packages for several projects, their requirements can conflict. One project may need an older version of a library while another needs the latest release. A Python virtual environment solves this by keeping each project’s packages separate. In this beginner-friendly guide, you will learn how to create a Python virtual environment in VS Code, activate it on Windows, macOS and Linux, install packages, and select the correct interpreter.

We will use Python’s built-in venv module, so you do not need to install a separate environment manager to follow along.

What Is a Python Virtual Environment?

A virtual environment is an isolated Python environment created for a particular project. It has its own package installation location, which means packages installed inside one environment do not normally affect packages installed in another project or in your system-wide Python installation.

For example, imagine you are working on two applications:

  • Project A: requires Django 4.x.
  • Project B: requires Django 5.x.

Installing both versions into one shared Python environment can cause conflicts. With separate virtual environments, each project can use the version it needs.

Why Should You Use a Virtual Environment?

  • Package isolation: keep each project’s dependencies separate.
  • Fewer version conflicts: use different library versions in different projects.
  • Cleaner system: avoid installing every development package globally.
  • Reproducible setup: record dependencies in a requirements.txt file and reinstall them later.
  • Safer experimentation: test a package upgrade without changing another project’s environment.

Prerequisites

Before starting, make sure you have:

  • Python installed on your computer.
  • Visual Studio Code installed.
  • The Microsoft Python extension installed in VS Code.
  • A project folder that you can open in VS Code.

You can download Python from the official Python website and Visual Studio Code from the official VS Code website. The Python extension is available in the VS Code Marketplace.

Step 1: Create and Open a Python Project

Create a folder for your project. For this example, name it python-demo. Open VS Code, select File → Open Folder, and choose that folder.

You can also create and open the folder from a terminal:

mkdir python-demo
cd python-demo
code .

If the code command is not available on your computer, open the folder through VS Code’s File menu instead.

Step 2: Create a Virtual Environment

Open the integrated terminal in VS Code by selecting Terminal → New Terminal. Run the command that matches your operating system.

Windows

py -m venv .venv

If the py launcher is not available, try:

python -m venv .venv

macOS and Linux

python3 -m venv .venv

The command creates a folder named .venv inside your project. This folder contains the environment’s Python executable and the location where its packages will be installed. The leading dot makes the folder hidden in many file managers by default.

Step 3: Activate the Virtual Environment

Creating an environment and activating it are two different steps. Creation makes the environment folder; activation tells your current terminal to use that environment’s Python and package tools.

Windows PowerShell

.\.venv\Scripts\Activate.ps1

Windows Command Prompt (CMD)

.venv\Scripts\activate.bat

macOS and Linux

source .venv/bin/activate

After successful activation, your terminal prompt usually displays (.venv) at the beginning. The exact appearance depends on your shell.

PowerShell execution-policy message? If PowerShell blocks the activation script, you can use Command Prompt and run the CMD command above. Alternatively, you can allow scripts for the current PowerShell process only with Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass, then activate the environment. This temporary setting does not change the policy for other PowerShell sessions.

Step 4: Select the Environment in VS Code

VS Code should use the environment you created when running, debugging and analysing your Python files. To select it manually:

  1. Open the Command Palette with Ctrl + Shift + P on Windows/Linux or Cmd + Shift + P on macOS.
  2. Search for Python: Select Interpreter.
  3. Choose the interpreter located inside your project’s .venv folder.
  4. Open a new terminal if necessary and confirm that the environment is active.

Recent versions of the Python extension also provide a Python: Create Environment command. You can use it to create a venv environment through VS Code’s guided setup instead of entering the creation command manually. The Environment Managers view also provides environment creation and selection controls.

Step 5: Install Python Packages

With the environment active, install a package using python -m pip. For example, install the popular Requests library:

python -m pip install requests

Using python -m pip helps ensure that pip belongs to the Python interpreter selected by the current terminal. You can check the installed package with:

python -m pip show requests

To install another package, replace requests with its package name. For example, you could install Flask with python -m pip install flask or install NumPy with python -m pip install numpy.

Step 6: Save Dependencies in requirements.txt

When a project uses several packages, record the installed dependencies so that you or another developer can recreate the setup later.

Generate a requirements file with:

python -m pip freeze > requirements.txt

To install the listed packages in a newly created environment, run:

python -m pip install -r requirements.txt

Keep requirements.txt in your project repository, but do not commit the entire .venv directory. The requirements file records package versions; the virtual environment itself can be recreated on another computer.

Step 7: Test Your Environment

Create a file named main.py in VS Code and add this simple program:

import requests

response = requests.get("https://httpbin.org/get", timeout=10)

print("Status code:", response.status_code)
print("Virtual environment is working!")

Run the file from the VS Code terminal:

python main.py

The example makes an internet request, so an active internet connection is required. If you only want to confirm that the selected interpreter is correct, run:

python -c "import sys; print(sys.executable)"

The printed path should point to your project’s .venv directory.

Step 8: Deactivate the Environment

When you finish working in the terminal, you can deactivate the environment with one command:

deactivate

This exits the active environment in that terminal session. It does not delete the .venv folder or uninstall its packages. You can activate it again whenever you return to the project.

Recommended Python Project Structure

A simple project can be organised like this:

python-demo/
│
├── .venv/
├── main.py
├── requirements.txt
└── .gitignore

Add this line to your .gitignore file:

.venv/

This prevents Git from tracking the environment folder. Each developer can create their own environment and install the dependencies from requirements.txt.

Common Problems and How to Fix Them

ProblemPossible reasonWhat to do
python is not recognisedPython is not on PATH or the launcher is unavailable.On Windows, try py. Otherwise check your Python installation and PATH.
Activation is blocked in PowerShellThe current execution policy blocks the script.Use CMD activation or a temporary process-scoped policy change.
ModuleNotFoundErrorThe package may have been installed in a different interpreter.Select the project’s .venv interpreter and install the package again with python -m pip.
VS Code uses the wrong PythonA different interpreter is selected.Run Python: Select Interpreter and choose the environment inside .venv.
venv module is unavailableSome Linux distributions package venv support separately.Install the matching Python venv package using your distribution’s package manager.

Frequently Asked Questions

Is venv included with Python?

Yes. venv is part of Python’s standard library in supported standard installations, so you normally do not need to install it separately. Some Linux distributions package the required venv components separately.

Should I create a separate virtual environment for every Python project?

For most independent projects, yes. A separate environment keeps dependencies and package versions isolated, making projects easier to maintain.

Should I upload the .venv folder to GitHub?

No. Add .venv/ to .gitignore and share a dependency file such as requirements.txt instead. Other developers can recreate the environment on their computers.

What is the difference between venv and pip?

venv creates an isolated Python environment. pip installs and manages packages within the environment’s Python installation. They solve related but different problems.

Can I use a virtual environment without VS Code?

Yes. Virtual environments are a Python feature and work with other editors, IDEs and terminal-based workflows. VS Code simply provides convenient interpreter selection and environment management features.

Final Thoughts

Creating a Python virtual environment in VS Code is a simple habit that helps keep your projects organised and prevents package conflicts. Use python -m venv .venv to create the environment, activate it for your terminal, select it as the VS Code interpreter, and install packages with python -m pip. Save your dependencies in requirements.txt and keep .venv out of version control.

For more details, consult the official Python virtual environments tutorial, the Python venv reference, and Microsoft’s VS Code Python environments guide.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top