Virtual Environments: Managing Project Dependencies with Python
Learn how to use virtual environments to isolate your Python projects. Master venv and pip to manage dependencies without breaking your system-wide setup.

Previously in this course, we covered importing standard modules to extend the functionality of your scripts. Now that we are about to start using third-party packages, we need a way to keep those packages organized so they don't interfere with your global Python installation or other projects. This lesson introduces venv, the industry standard for environment management.
Why Do We Need Virtual Environments?
When you install a Python library, it typically goes into a global directory on your machine. If Project A needs version 1.0 of a library and Project B needs version 2.0, you have a problem: you can only have one version installed globally.
A virtual environment is a self-contained directory tree that includes a Python installation and a copy of the pip tool. It creates an isolated sandbox for your project. Any package you install while the environment is active stays inside that sandbox, leaving your global system untouched.
Creating and Activating an Environment
The venv module comes built-in with modern Python. To set one up for your project, navigate to your project folder in your terminal and run the following command:
Bash# Create the environment in a folder named 'venv' python -m venv venv
Once the command finishes, you will see a new folder named venv in your directory. To start using it, you must "activate" it, which tells your terminal to prioritize the Python and pip located inside that folder over the system-wide versions.
On macOS/Linux:
Bashsource venv/bin/activate
On Windows:
Bash.\venv\Scripts\activate
Once activated, you will usually see (venv) appear at the start of your command prompt. You are now working inside your isolated environment.
Installing Packages with Pip
pip is the standard package manager for Python. When you are inside an active virtual environment, any package you install via pip is local to that specific project.
To install a package, use the pip install command. Let's install a popular package as an example:
Bash# Install the 'requests' library pip install requests
If you ever need to see what packages are currently installed in your environment, use:
Bashpip list
Managing Dependencies
As your project grows, you will eventually want to share your code with others or deploy it to a server. To ensure the environment is reproducible, we use a requirements.txt file.
- Save your dependencies:
Bash
pip freeze > requirements.txt - Install from a file:
If you download a project that has a
requirements.txtfile, you can install everything it needs at once:Bashpip install -r requirements.txt
Hands-on Exercise
- Create a new directory for a test project.
- Create and activate a virtual environment named
venv. - Use
pip installto add therequestslibrary. - Run
pip listto confirm it appears in the output. - Create a
requirements.txtfile using thepip freezecommand and inspect its contents with a text editor. - Deactivate the environment by simply typing
deactivatein your terminal.
Common Pitfalls
- Forgetting to activate: If you run
pip installand get a "Permission Denied" error, you are likely not in a virtual environment and are trying to modify system files. Always check for the(venv)prefix in your terminal. - Committing the environment: Never add your
venvfolder to version control (like Git). It contains thousands of files specific to your machine's path. Only commit yourrequirements.txtfile. - Mixing environments: If you open a new terminal tab, your environment is not active by default. You must run the
sourceoractivatecommand again for every new terminal window.
FAQ
Q: Can I delete the venv folder?
A: Yes. If you delete the folder, you lose the isolated environment and the installed packages. Since you have your requirements.txt, you can recreate it at any time by running pip install -r requirements.txt.
Q: Do I need a separate environment for every single script? A: Not necessarily. Most developers use one virtual environment per project folder.
Q: Is venv the only way to manage environments?
A: venv is the standard tool built into Python. While tools like conda or poetry exist, venv is the best place to start for beginners to understand the underlying mechanics of environment management.
Recap
Virtual environments are essential for maintaining clean, reproducible Python projects. By using python -m venv to isolate your work and pip to manage dependencies, you ensure that your code will run consistently regardless of the system it is installed on. You've now secured your project structure, which is a critical step before we start working with external APIs.
Up next: Using Third-Party Libraries
Work with me

Laravel REST API Development
Clean, secure, well-documented Laravel REST APIs โ the backend engine for your app, mobile client, or SaaS. Built by an API specialist.

FilamentPHP Admin Panel & Dashboard Development
A powerful admin panel for your Laravel app โ built with FilamentPHP so you can manage everything without touching the database.

