Elena' s AI Blog

Python Virtual Environments

24 Jan 2025 (updated: 07 Sep 2026) / 9 minutes to read

Elena Daehnhardt

Midjourney: A minimalist illustration of a Python snake slithering through a series of interconnected boxes, each representing a virtual environment --v 6.1


TL;DR:
  • Always use a Python virtual environment for every project. Create it with 'python3 -m venv .venv', and activate it with 'source .venv/bin/activate'. It prevents dependency conflicts and keeps your global Python installation clean.

Previous: Part 4 β€” Logging in Python

Next: Part 6 β€” Python Flask TODO App

What Is a Python Virtual Environment?

A Python virtual environment is an isolated, per-project folder that holds its own Python interpreter and installed packages, so dependencies for one project never conflict with another or with your global system Python.

If you have ever tried to run two Python projects that required different versions of the same library, you already know the pain of dependency conflicts.

The solution is simple: virtual environments. A virtual environment is a lightweight, isolated folder where you can install Python packages specifically for one project, without affecting your global system Python installation or breaking any of your other projects.

Using isolated environments helps you:

  • Prevent conflicts: Install exactly the versions you need for one project without breaking another.
  • Collaborate cleanly: Share your requirements.txt so anyone else can recreate the exact same setup.
  • Clean up safely: Because all dependencies are stored in one local folder, you can simply delete it when you’re done. No lingering junk on your system.

Using venv to Create an Isolated Python Environment

Using virtual environments is good practice, especially as projects grow in complexity and require different libraries that may not be compatible. The standard-library module venv allows each project to have its customised environment.

The venv module creates a virtual environment: a folder containing scripts and a link to the Python interpreter. Virtual environments offer two main benefits:

  1. You can install project-specific libraries in isolation for better control.
  2. When sharing your project, use β€œpip freeze > requirements.txt” to create a list of necessary libraries, keeping your environment clean.

Conclusion

πŸ”’ Subscribe to keep reading.

References

πŸ”’ Subscribe to keep reading.

You've hit a Deep Dive tutorial.

I spend dozens of hours researching, coding, and breaking things to write these guides. This content is free, but reserved for my subscriber community. Drop your email below to unlock this guide (and all past/future deep dives):

Already a subscriber? Use the magic link from your last newsletter, or reset your password.

New subscribers get an inbox mail: Set a password to unlock articles. The form does not log you in β€” use the same email afterwards.

desktop bg dark

About Elena

Elena, a PhD in Computer Science, simplifies AI concepts and helps you use machine learning.

Citation
Elena Daehnhardt. (2025) 'Python Virtual Environments', daehnhardt.com, 24 January 2025. Available at: https://daehnhardt.com/blog/2025/01/24/virtual-environments-in-detail/
All Posts