Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 49

Installing Python Packages with Pip

Recommended resources

Hello! Welcome to the final lesson in our Python Fundamentals module.

In our last session, we built a complete, practical data processing script. We read a CSV file, cleaned the data, calculated new metrics like CTR and CPC, and wrote the results to a new report file. To do this, we used Python's built-in csv module. While it's a great tool, it's just the tip of the iceberg.

Today, we take a significant step forward. We're going to unlock the vast ecosystem of powerful, specialized tools that the Python community has built. To do that, you need to master pip, Python's package manager.

This lesson is the gateway to advanced data analysis. As a Business Analyst, you won't be building every function from scratch. Instead, you'll leverage powerful external libraries like pandas for data manipulation, matplotlib for visualization, and scikit-learn for basic modeling. Learning how to install and manage these packages is the essential skill that makes all of that possible.

By the end of this 60-minute lesson, you will be able to:

  • Understand what packages are and why we need a manager like pip.
  • Create isolated project environments using venv (a crucial best practice).
  • Install, list, and manage packages for a project.
  • Capture your project's dependencies in a requirements.txt file to ensure your work is reproducible.

1. What Are External Packages and Why Do We Need Them?

Python comes "batteries included," with a rich Standard Library that contains modules like csv and math. However, for more specialized tasks like complex data analysis or creating interactive charts, relying solely on the standard library would be like building a house using only a hammer and a screwdriver. It's possible, but incredibly inefficient.

The global Python community has developed hundreds of thousands of specialized tools, or packages, for nearly every imaginable task. These are hosted in a central online repository called the Python Package Index (PyPI), which you can browse at pypi.org.

Think of PyPI as an enormous app store for programmers. pip (a recursive acronym for "Pip Installs Packages") is the tool you use to download and install these "apps" (packages) into your Python environment.

Pip Python Tutorial for Package Management

To get a clear overview of these concepts, let's start with a brief reading from the DataCamp article 'Pip Python Tutorial for Package Management'.

Please read the first two sections, 'Understanding Packages in Python' and 'Understanding package managers: pip'. These sections do a great job of explaining the relationship between the Python Standard Library, external packages on PyPI, and pip's role as the manager.


2. The Problem with Global Installations: Introducing Virtual Environments

When you install a package, you could install it into your main, system-wide Python installation. This is called a global installation. While simple, it's a bad practice that leads to major headaches.

Imagine you're working on two projects:

  • Project A (Legacy Ad Report): Requires an old version of a data analysis package, pandas==1.5.
  • Project B (New Funnel Analysis): Needs a new feature from the latest version, pandas==2.2.

If you install pandas globally, you can only have one version. Upgrading for Project B will break Project A. This is often called "dependency hell."

The solution is to use virtual environments. A virtual environment is an isolated, self-contained Python setup for a single project. Each project gets its own environment with its own set of installed packages, completely separate from your global Python and all other projects.

This is a non-negotiable best practice in professional development.

Installing Packages With pip: Python Basics

The Real Python video 'Installing Packages With pip' provides an excellent explanation of why virtual environments are so critical before showing how to use them.

Watch the section from 09:26 to 11:03. The presenter clearly articulates the three main advantages of using virtual environments, which directly address the problems of global installation.


3. Your Professional Workflow: venv and pip in Action

Now, let's walk through the standard, professional workflow for starting any new Python project. This is a practical sequence of commands you will use constantly.

We'll follow along with the Real Python video, which demonstrates this process clearly. You should open a terminal or command prompt and perform these steps yourself.

Installing Packages With pip: Python Basics

This next segment of the video will guide you through creating a project folder, setting up a virtual environment inside it, and activating that environment.

Watch from 11:03 to 15:31. Follow the steps shown in the video in your own terminal. Don't worry about the ls or pwd commands if you're not familiar with them; focus on mkdir, cd, creating the venv, and activating it. I will summarize the key commands below.

Here's a summary of the essential commands for setting up your project:

Step 1: Create and navigate to your project directory.

# Creates a new folder for your project
mkdir my_adtech_analysis

# Moves you inside that folder
cd my_adtech_analysis

Step 2: Create the virtual environment.
We use Python's built-in venv module for this.

# This command tells python to run the venv module
# and create a virtual environment in a folder named 'venv'
python -m venv venv

You will now see a new folder named venv inside my_adtech_analysis. This folder contains a copy of the Python interpreter and is where your project-specific packages will be installed.

Step 3: Activate the virtual environment.
This is the one step that differs by operating system. Activating the environment tells your terminal to use this project's isolated Python setup instead of the global one.

  • On Windows (Command Prompt or PowerShell):
    .\venv\Scripts\activate
    
  • On macOS and Linux:
    source venv/bin/activate
    

After running this, your command prompt will change to show (venv) at the beginning. This is your confirmation that the virtual environment is active!

Step 4: Install packages into the active environment.
Now that our isolated environment is active, we can safely install packages. The recommended way to run pip is with python -m pip. This ensures you are using the pip that belongs to the currently active Python interpreter, avoiding any confusion.

Let's continue with the video to see how to install a package (requests) and verify that it's installed only within our virtual environment.

Installing Packages With pip: Python Basics

This part of the tutorial demonstrates the core pip install command and beautifully illustrates the power of isolation by showing that the installed package only exists inside the activated environment.

Watch from 15:31 to 20:41. Pay close attention to: How pip list shows different results inside and outside the virtual environment. The pip install requests command and the output showing that it also installed dependencies (other packages that requests needs to function). The final deactivate command, which exits the virtual environment.

Test your understanding!

You've just activated a new virtual environment for a project. You run pip list and see only two packages: pip and setuptools. You then run python -m pip install pandas.

If you run pip list again, what do you expect to see?

Show answer

You would expect to see pandas in the list, along with several other packages you didn't explicitly install, such as numpy, pytz, and python-dateutil. This is because pip automatically identifies and installs all the dependencies that pandas requires to work correctly.


4. Sharing Your Work: requirements.txt

You've set up your project and installed the necessary packages. How do you share this project with a colleague or deploy it on another machine, ensuring they use the exact same package versions?

You do this with a requirements file, conventionally named requirements.txt.

This file acts as a precise bill of materials for your project's environment. There are two key commands:

  1. To generate the file:
    pip freeze > requirements.txt
    This command "freezes" all the packages and their exact versions in your current active environment and saves that list to requirements.txt.

  2. To install from the file:
    pip install -r requirements.txt
    This command reads the file and installs all the listed packages with their specified versions into the active environment. This is how someone else can perfectly replicate your setup.

Python Virtual Environment and pip for Beginners

The YouTuber Dave Gray has a great, concise segment on creating and using a requirements file. Let's watch it.

Watch this short clip from 11:39 to 12:47. It perfectly demonstrates the pip freeze command and explains the purpose of the resulting requirements.txt file.

This requirements.txt file is a critical piece of any project and should always be saved and shared along with your code.


5. Practical Summary: All pip Commands

Here is a quick reference table for the commands we've learned today.

Action Command Notes
Create a virtual environment python -m venv venv Creates a venv folder in your current directory.
Activate virtual environment (Windows) .\venv\Scripts\activate Your terminal prompt will change to show (venv).
Activate virtual environment (macOS/Linux) source venv/bin/activate Your terminal prompt will change to show (venv).
Deactivate virtual environment deactivate Run this when you're done working on the project.
Install a package python -m pip install <package_name> Installs the latest version of the package.
List installed packages python -m pip list Shows packages in the active environment.
Get package details python -m pip show <package_name> Shows version, dependencies, author, etc.
Create requirements file python -m pip freeze > requirements.txt Saves all packages and their exact versions from the active environment into a file.
Install from requirements file python -m pip install -r requirements.txt Installs all packages specified in the file. A crucial command for collaboration and deployment.
Uninstall a package python -m pip uninstall <package_name> Removes a package from the active environment.

Conclusion

Congratulations on completing the Python Fundamentals module! Today, you've learned a skill that separates beginners from practitioners. Managing packages and environments is fundamental to all modern software and data science work.

Key Takeaways:

  • pip is the standard tool for installing external Python packages from the Python Package Index (PyPI).
  • Always use virtual environments (venv) for your projects. This practice prevents dependency conflicts and keeps your projects self-contained and reproducible.
  • The professional workflow is: Create a project folder -> Create a venv -> Activate it -> Install packages.
  • Use pip freeze > requirements.txt to document your project's dependencies, and pip install -r requirements.txt to recreate the environment elsewhere.

Preview of the Next Module:
You are now fully equipped to move into serious data analysis. In our next module, "Data Analysis and Manipulation with Pandas," we will install the pandas library into a new virtual environment and discover how it allows you to load, clean, transform, and analyze data with a power and elegance that the built-in csv module can't match. We'll start by learning how to create and inspect the core data structure in pandas: the DataFrame.