Python Virtual Environment & PIP Tutorial | venv, pip & requirements.txt
As Python projects grow in size and complexity, they often require different versions of libraries and dependencies. Installing all packages globally on your computer can lead to version conflicts and make projects difficult to manage.
To solve this problem, Python provides Virtual Environments, which create isolated environments for each project. Combined with PIP (Python Package Installer), developers can install, update, and manage project-specific libraries without affecting other projects.
Virtual environments are an industry standard and are widely used in Django, Flask, FastAPI, Artificial Intelligence, Machine Learning, Data Science, Automation, Backend Development, and DevOps.
In this lesson, you’ll learn how to create virtual environments, activate and deactivate them, install packages with PIP, manage project dependencies using requirements.txt, and build portable Python applications.
By the end of this lesson, you’ll be able to create professional Python projects with isolated environments and reproducible dependencies.
Learning Objectives
After completing this lesson, you will be able to:
- Understand Virtual Environments.
- Create and activate virtual environments.
- Install and manage packages using PIP.
- Upgrade and uninstall packages.
- Generate and use
requirements.txt. - Build isolated Python projects.
- Manage dependencies professionally.
- Apply package management best practices.
Topics Covered
- Introduction to Virtual Environments
- Why Virtual Environments?
- Creating Virtual Environments
- Activating Virtual Environments
- Installing Packages with PIP
- Updating Packages
- Removing Packages
- requirements.txt
- Real-World Applications
- Best Practices
Detailed Lesson Content
Introduction to Virtual Environments
Imagine you’re working on two different projects.
Project A
Uses:
- Django 5.2
- NumPy 2.1
Project B
Uses:
- Django 4.2
- NumPy 1.26
If both projects share the same global Python installation, version conflicts may occur.
Virtual Environments solve this problem by creating an isolated workspace for each project.
Each project has its own:
- Python interpreter
- Installed packages
- Dependencies
What is a Virtual Environment?
A Virtual Environment is an isolated Python environment where packages are installed independently of the global Python installation.
Benefits include:
- No dependency conflicts
- Easy project management
- Cleaner development workflow
- Portable projects
- Reproducible environments
Checking Python Version
Before creating a virtual environment, verify the installed Python version.
python --version
Output
Python 3.13.2
Checking PIP Version
pip --version
Output
pip 25.1
Creating a Virtual Environment
Use the built-in venv module.
python -m venv myenv
This creates a folder named:
myenv/
containing:
- Python executable
- Scripts
- Installed libraries
- Configuration files
Project Structure
AI_Project/
│── myenv/
│── app.py
│── requirements.txt
│── README.md
Activating Virtual Environment
Windows
myenv\Scripts\activate
Output
(myenv) C:\Projects>
Linux / macOS
source myenv/bin/activate
Output
(myenv) user@computer$
The environment name appears before the command prompt.
Deactivating Virtual Environment
When you’re finished:
deactivate
The terminal returns to the global Python environment.
Installing Packages Using PIP
Install a package:
pip install requests
Output
Successfully installed requests
Install multiple packages:
pip install numpy pandas matplotlib
Viewing Installed Packages
pip list
Example Output
Package Version
numpy 2.1.0
pandas 2.2.3
requests 2.32.0
Viewing Package Information
pip show requests
Output
Name: requests
Version: 2.32.0
Location: ...
Upgrading Packages
Upgrade a package:
pip install --upgrade requests
Upgrade pip itself:
python -m pip install --upgrade pip
Uninstalling Packages
Remove a package:
pip uninstall requests
Python asks for confirmation before uninstalling.
Installing Specific Versions
pip install django==5.2
This ensures the project uses the required version.
requirements.txt
A requirements.txt file stores all project dependencies.
Generate it:
pip freeze > requirements.txt
Example
Django==5.2
requests==2.32.0
numpy==2.1.0
Installing Packages from requirements.txt
pip install -r requirements.txt
This installs every dependency listed in the file.
Real-World Example 1: Django Project
python -m venv env
env\Scripts\activate
pip install django
django-admin startproject myproject
Real-World Example 2: AI Project
python -m venv ai_env
pip install numpy
pip install pandas
pip install scikit-learn
Real-World Example 3: Flask Application
pip install flask
pip freeze > requirements.txt
Real-World Example 4: FastAPI Backend
pip install fastapi
pip install uvicorn
Run the application:
uvicorn main:app --reload
Common Mistakes
Installing Packages Globally
Incorrect
pip install django
Always activate the project’s virtual environment before installing packages.
Forgetting to Activate the Environment
If the environment isn’t active, packages may install globally instead of inside the project.
Always verify that the prompt displays:
(myenv)
Not Creating requirements.txt
Without requirements.txt, other developers cannot easily recreate your project environment.
Always generate it before sharing your project.



