Lesson 10.3: Installing Packages with pip
What you will learn:
- What pip is and how it works
- How to install third-party packages
- How to use a
requirements.txtfile - The basics of virtual environments
- Popular Python packages worth knowing about
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.
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
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
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
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.
Creating and Using a Virtual Environment
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 |
|---|---|---|
requests | HTTP requests (web APIs) | pip install requests |
flask | Web applications | pip install flask |
pandas | Data analysis | pip install pandas |
numpy | Scientific computing | pip install numpy |
matplotlib | Charts and graphs | pip install matplotlib |
pillow | Image processing | pip install pillow |
pytest | Testing your code | pip 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
- pip is Python's package installer that downloads packages from PyPI
- Use
pip install package_namein your terminal to install packages - A
requirements.txtfile lists all packages a project needs - Virtual environments create isolated spaces for each project's packages
- Install packages once with pip, then import them in your Python code
- Run pip commands in the terminal, not inside Python files