Learn Without Walls
← Previous Lesson Lesson 3 of 4 Next Lesson →

Lesson 10.3: Installing Packages with pip

What you will learn:

1. What Is pip?

Python's standard library is powerful, but sometimes you need functionality that does not come built in. That is where pip comes in.

pip: Python's package installer. It downloads and installs packages (libraries) from the Python Package Index (PyPI), an online repository of over 400,000 Python packages created by the community.

Think of pip like an app store for Python. Just as you download apps for your phone, you use pip to download packages for Python. The packages on PyPI are free and open-source.

pip comes automatically with Python 3.4 and later, so if you have Python installed, you likely already have pip.

Check if pip Is Installed

$ pip --version pip 23.2.1 from /usr/lib/python3.11/site-packages (python 3.11) $ pip3 --version pip 23.2.1 from /usr/lib/python3.11/site-packages (python 3.11)

Note: On some systems, you may need to use pip3 instead of pip to target Python 3.

2. Installing Packages

Installing a package is straightforward. You run a command in your terminal (not inside Python):

Basic pip install

# Install a package $ pip install requests # Install a specific version $ pip install requests==2.31.0 # Upgrade an existing package $ pip install --upgrade requests # Uninstall a package $ pip uninstall requests # See what's installed $ pip list
Important: Run pip commands in your terminal/command prompt, not inside a Python script or the Python interpreter. These are command-line tools, not Python code.

After installing a package, you can import and use it in your Python code just like any built-in module:

# After running: pip install requests
import requests

# Now you can use the requests library
# response = requests.get("https://api.example.com/data")

3. Using requirements.txt

When you share your project with others, they need to know which packages to install. A requirements.txt file lists all the packages your project needs.

Creating requirements.txt

# requirements.txt
# Each line lists a package (optionally with a version)
requests==2.31.0
flask==3.0.0
pandas>=2.0.0
numpy

Installing from requirements.txt

# Install all packages listed in requirements.txt $ pip install -r requirements.txt # Generate requirements.txt from currently installed packages $ pip freeze > requirements.txt

The pip freeze command outputs a list of all installed packages with their exact versions. Redirecting this to a file (> requirements.txt) creates a complete snapshot of your environment.

Try It Yourself

Open your terminal and run pip list to see what packages are already installed on your system. You might be surprised by how many come pre-installed!

4. Virtual Environments (Introduction)

As you work on different projects, they might need different versions of the same package. Virtual environments solve this problem by creating isolated Python installations for each project.

Virtual environment: An isolated Python environment where you can install packages without affecting your system-wide Python installation or other projects.

Creating and Using a Virtual Environment

# Create a virtual environment named "myenv" $ python -m venv myenv # Activate it (macOS/Linux) $ source myenv/bin/activate # Activate it (Windows) $ myenv\Scripts\activate # Your prompt changes to show the active environment (myenv) $ pip install requests # Deactivate when you're done (myenv) $ deactivate

Think of a virtual environment like a separate workspace. Each workspace has its own set of tools (packages), and installing or removing tools in one workspace does not affect the others.

5. Popular Python Packages

Here are some popular packages you might want to explore as you continue learning Python:

Package Purpose Install Command
requestsHTTP requests (web APIs)pip install requests
flaskWeb applicationspip install flask
pandasData analysispip install pandas
numpyScientific computingpip install numpy
matplotlibCharts and graphspip install matplotlib
pillowImage processingpip install pillow
pytestTesting your codepip install pytest

Check Your Understanding

What is the difference between running pip install requests and import requests?

pip install requests is a terminal command that downloads and installs the requests package onto your computer. You only need to do this once. import requests is a Python statement that loads the already-installed package into your current program. You do this in every Python file that needs to use the package.

Key Takeaways

← Previous Lesson Lesson 3 of 4 Next Lesson →