Day 27 45 min beginner

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:

1
2
3
4
5
6
7
8
# Install a package
pip install requests

# List installed packages
pip list

# Uninstall a package
pip uninstall requests

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):

terminal_commands.sh
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 1. Create the environment (name it 'venv')
python -m venv venv

# 2. Activate it (Windows)
.\venv\Scripts\activate

# 2. Activate it (macOS/Linux)
source venv/bin/activate

# 3. Your terminal should now show (venv) at the start!

Using requirements.txt

When sharing your project, you should list your dependencies in a text file so others can install them with one command.

sharing.sh
1
2
3
4
5
# Create the file
pip freeze > requirements.txt

# Install from the file
pip install -r requirements.txt

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.

Loading quiz...

Discussion

Have questions or want to discuss this lesson? Join the conversation below!