Skip to content
Happy Programming Guide
Start learning
Python

Python Virtual Environments Explained

A virtual environment is a private folder of packages for one project. Why you need one, how to create and activate it on any system, and the mistakes that waste an afternoon.

A virtual environment is a private folder of packages belonging to one project. Install something inside it and only that project sees it.

Terminal
python -m venv .venv          # create it
.venv\Scripts\activate        # activate it (Windows)
source .venv/bin/activate     # activate it (macOS / Linux)
pip install requests          # installs into this project only

That is the whole idea. The rest of this page is why it matters and what goes wrong.

The problem it solves#

Without one, every pip install goes into a single shared pile used by every Python project on your machine. That is fine until it is not:

  • Project A needs version 1 of a library. Project B needs version 3. Only one can be installed.
  • You upgrade a package for a new project, and an old project stops working.
  • You want to hand your project to someone else and cannot say which of your ninety installed packages it actually needs.
  • On some systems, installing globally needs administrator rights or interferes with tools the operating system uses.

A virtual environment gives each project its own pile. The projects stop being able to break each other.

Creating one#

From inside your project folder:

Terminal
python -m venv .venv

That creates a .venv folder containing a private copy of Python and a private site-packages. The leading dot is only a naming convention — it keeps the folder out of the way and is what most tools expect.

On macOS and Linux you may need python3 -m venv .venv.

Activating it#

System Command
Windows PowerShell .venv\Scripts\Activate.ps1
Windows Command Prompt .venv\Scripts\activate.bat
macOS / Linux source .venv/bin/activate

You know it worked because your prompt gains a prefix:

Terminal
(.venv) C:\projects\my-app>

That prefix is the whole signal. If it is not there, you are not in the environment and pip install is going somewhere else.

To leave:

Terminal
deactivate

Installing packages#

With the environment active, install normally:

Terminal
python -m pip install requests
python -m pip list

python -m pip rather than plain pip guarantees you are installing for the interpreter that will actually run your code. It is the single habit that prevents most “I installed it but it says no module named” confusion.

requirements.txt#

Recording what your project needs is what makes it runnable by anyone else, including you on a different machine.

Terminal
python -m pip freeze > requirements.txt

That writes a file listing every installed package and its exact version:

Output
certifi==2026.1.31
charset-normalizer==3.4.1
idna==3.10
requests==2.32.3
urllib3==2.3.0

Someone else then recreates your setup in two commands:

Terminal
python -m venv .venv
python -m pip install -r requirements.txt

Regenerate the file whenever you add a package, and commit it. It is small, it is text, and it is the difference between a project that runs and one that does not.

Never commit the environment itself#

The .venv folder is thousands of files, specific to your operating system, and completely rebuildable from requirements.txt. Add it to .gitignore before your first commit:

Output
.venv/
__pycache__/
*.pyc
.env

See Git and GitHub explained.

Making VS Code use it#

VS Code keeps its own interpreter setting, separate from whatever your terminal has activated. If it is pointing elsewhere you get red underlines on imports that run perfectly well.

  1. Press Ctrl + Shift + P
  2. Run Python: Select Interpreter
  3. Choose the one showing .venv

The selected interpreter appears in the status bar at the bottom. New terminals you open inside VS Code will then activate it automatically. More in VS Code problems beginners hit.

Do you need one on day one?#

Honestly, no. If you are writing single-file scripts with no external packages, a virtual environment adds a step and solves a problem you do not have yet.

Start using them when either of these becomes true:

  • You are installing packages with pip
  • You have more than one project on the go

That usually lands somewhere around your second or third real project, which is exactly the right time.

Questions people ask#

venv or virtualenv or conda?

venv ships with Python and is the right default. virtualenv is an older third-party tool with a few extra features. conda is a different system aimed at data science, which also manages non-Python dependencies — use it if you are following a data-science course that assumes it, otherwise stick with venv.

Can I use one environment for several projects?

You can, and it reintroduces the exact problem environments exist to solve. One per project.

How do I delete an environment?

Delete the .venv folder. There is nothing else to clean up — no registry entry, no global state.

Why is my environment so large?

An empty one is a few megabytes. Some packages are genuinely big. It does not matter, because the folder is never committed or shared.

Do I need to activate it every time?

Every new terminal session, yes. VS Code does it for you once the interpreter is selected, which is most of the appeal of setting that.

Where to go next#

Next lessonReading and writing files in Python

Keep reading

Keep going — pick your next guide

The fastest way to improve is to read one guide, then build the thing it describes. Start with the basics, or jump straight to a project.

Ask a question or share what worked

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