Pip and Virtual Environments
Learn how to manage external packages and keep your projects organized.
Learning Objectives
- Understand the role of 'pip' (the package manager)
- Install external libraries (like requests)
- Create and activate a Virtual Environment (venv)
- Understand why project isolation is important
Beyond the Standard Library
The Python community has created over 400,000 external packages that you can use in your own projects. These range from website builders to AI tools. Today, we learn how to manage them.
What is pip?
pip is the standard package manager for Python. It allows you to download and install libraries from the Python Package Index (PyPI).
Common Commands:
|
|
The Problem: Dependency Hell
If you install all packages globally on your computer, eventually two projects will need different versions of the same package, causing a crash. We fix this with Virtual Environments.
Virtual Environments (venv)
A virtual environment is a “sandbox” for your project. It contains its own copy of Python and its own set of packages.
Setting one up (Windows/macOS/Linux):
|
|
Using requirements.txt
When sharing your project, you should list your dependencies in a text file so others can install them with one command.
|
|
Interactive Practice
Imagine you are starting a Data Science project. You need pandas and numpy. Would you install them globally or in a virtual environment?
| Concept | Description |
|---|---|
| pip | Downloads packages from the web |
| venv | Creates a project sandbox |
| PyPI | The “App Store” for Python packages |
Quiz
Complete this quiz with a minimum score of 80% to mark Day 27 as complete.
Discussion
Have questions or want to discuss this lesson? Join the conversation below!