🐍 Lesson 26: Python Virtual Environments & Dependency Management
Welcome to Lesson 26! Today we’ll learn how to manage project dependencies using virtual environments. This is one of the most important skills for real-world and full-stack Python development. Using virtual environments ensures that each project is self-contained, with only the necessary packages installed, preventing conflicts and making it easier to deploy and manage your projects.
⭐ What You Will Learn in This Lesson
- How to create and activate a virtual environment
- How to install and manage project-specific dependencies
- How to save and share dependencies using
requirements.txt - The importance of virtual environments for real-world Python development
👥 Who Is This Lesson For?
- Python developers looking to manage project dependencies efficiently
- Anyone working with web frameworks like Flask, Django, or FastAPI
- Developers looking to avoid version conflicts in their projects
- Those interested in deploying Python applications seamlessly
🧰 What Is a Virtual Environment?
A virtual environment is an isolated Python workspace where you install only the packages a specific project needs. It ensures that your projects don't interfere with each other and that the correct dependencies are available for each project.
Why this matters:
- Prevents version conflicts between projects
- Keeps your system Python clean
- Ensures projects run the same on any machine
- Required for Django, Flask, FastAPI, AI projects, etc.
📦 1. Creating a Virtual Environment
python -m venv myenv
This creates a folder named myenv containing a lightweight Python installation that’s completely separate from the global Python environment.
⚙ 2. Activating the Virtual Environment
🔹 Windows
myenv\Scripts\activate
🔹 macOS / Linux
source myenv/bin/activate
After activation, your terminal will show:
(myenv)
📦 3. Installing Packages Inside the Environment
pip install requests
pip install flask
pip install numpy
Packages now install only into myenv, not globally. This keeps your project isolated and prevents conflicts with other projects.
📋 4. Listing Installed Packages
pip list
Use this command to check which packages are installed in your virtual environment.
📄 5. Saving Dependencies (requirements.txt)
To make sure your project’s dependencies are saved, use:
pip freeze > requirements.txt
This generates a requirements.txt file that lists all the installed packages with their specific versions.
📥 6. Installing Dependencies from requirements.txt
pip install -r requirements.txt
This is commonly used in:
- Team projects
- Web deployment (Flask/Django/FastAPI)
- GitHub repositories
❌ 7. Deactivating the Virtual Environment
deactivate
Once you’re done working in the virtual environment, use deactivate to return to the global environment.
🧩 Why Virtual Environments Matter
- Allows different Python versions and packages per project
- Prevents broken apps due to version conflicts
- Critical for backend & full-stack development
- You cannot deploy Django/Flask apps without one
🧪 Practice
- Create a virtual environment named
projectenv. - Activate it and install
flask,numpy, andrequests. - Generate a
requirements.txtfile usingpip freeze. - Create a second environment and install packages from the
requirements.txtfile usingpip install -r requirements.txt.
❓ Common Mistakes
- Forgetting to activate the virtual environment before installing packages
- Installing packages globally instead of within the virtual environment
- Not using
requirements.txtto save and share dependencies in team projects
❓ Frequently Asked Questions (FAQ)
1. Why do I need a virtual environment?
A virtual environment ensures your project has the specific packages and versions it needs, isolated from the global environment, preventing conflicts between projects.
2. Can I use a virtual environment with Python 2?
Yes, you can create a virtual environment with Python 2, but we recommend using Python 3 for all new projects due to its enhanced features and support.
3. How do I activate my environment after closing my terminal?
Simply navigate to the project directory and run the activation command again:
source myenv/bin/activate # macOS/Linux
myenv\Scripts\activate # Windows
🚀 What’s Next?
In the next lesson, you’ll learn about:
- Web scraping with Python using the
requestsandBeautifulSouplibraries - How to extract data from websites and process it
- Basic concepts and techniques for web scraping
Comments
Post a Comment