Concept Explanation
Imagine you are working on two different projects.
- Project A needs "Requests version 1.0".
- Project B needs "Requests version 2.0".
If you install them globally on your computer, they will conflict with each other. One project will always be broken.
A Virtual Environment is a self-contained folder that contains a private copy of Python and all the libraries your project needs. It’s like a "bubble" that protects your project from the rest of your computer. Anything you install inside that bubble stays inside that bubble.
Syntax or Core Structure
The venv Module
Python has a built-in module called venv to create these environments.
1. Creating the Environment:
python -m venv myenv
This creates a folder named myenv.
2. Activating it:
- Windows:
myenv\Scripts\activate - Mac/Linux:
source myenv/bin/activate
Once activated, your terminal prompt will change to show (myenv).
3. Deactivating: Simply type deactivate.
Code Example #1 (Basic)
The workflow of starting a new project.
# Create the environment
python -m venv project_env
# Activate it (Windows)
project_env\Scripts\activate
# Now install libraries safely
pip install pandas
# Check that it's unique to this environment
pip list
Code Example #2 (Intermediate)
Using a virtual environment to manage requirements for teamwork.
# Export the environment's list
pip freeze > requirements.txt
# On another computer, recreates the same environment
pip install -r requirements.txt
Real World Example
Professional Development Workflow: Every professional Python repo you see on GitHub (like Django or Flask) expects you to use a virtual environment.
# 📂 Setting up a new web project
mkdir my_website
cd my_website
python -m venv venv
venv\Scripts\activate
pip install flask
printf "from flask import Flask\napp = Flask(__name__)" > app.py
Common Mistakes
- Forgetting to Activate: You create the environment but forget to activate it. You then run
pip install, and the libraries go to your global system instead of the project. - Checking venv into Git: Never upload your
venvfolder to GitHub. It’s too large and specific to your computer. Instead, uploadrequirements.txt. - Scripts not found: If you try to run your script and it says "ModuleNotFound", you probably forgot to activate your environment.
Best Practices
- One Project, One venv: Never share an environment between two different projects.
- Name it 'venv': Use a standard name like
venvor.venv. Most code editors recognize these names and will automatically use them. - Use .gitignore: Always add your environment folder name to your
.gitignorefile.
Practice Exercises
- The Bubble Creator: Create a virtual environment named
test_env, activate it, install therequestslibrary, and verify it’s there withpip list. - The Cleaner: Deactivate your current environment and try to run a script that imports
requests. See what error you get! - Portability Test: Create a
requirements.txtfile usingpip freeze, delete your environment folder, and then rebuild it using the requirements file.