pip & Package
Installation
Learn how to extend Python's powers by installing and importing third-party packages using pip: the conceptual workflow every developer needs.
What Is a Package?
A package is a bundle of pre-written Python code that someone else has created and shared. Instead of writing everything from scratch, you can install a package and reuse its functionality in your own programs.
What Is pip?
pip stands for "pip Installs Packages" (a recursive acronym). It is Python's default package manager: a command-line tool that downloads, installs, upgrades, and removes packages from the Python Package Index (PyPI).
Why Do We Need pip?
Python is powerful on its own, but real-world projects almost always require third-party packages. pip solves the problem of getting those packages safely and reliably.
The pip install Command
The most important pip command is pip install. You run it in your terminal (command line), not inside a Python file. Here is the basic syntax:
The Full Workflow
Terminal vs Python Shell
One of the biggest sources of confusion: knowing where to type commands. pip commands go in the terminal, and import statements go in Python code.
| Terminal / Command Line | Python Shell / .py File | |
|---|---|---|
| Prompt looks like | $ or C:\> | >>> |
| Used for | pip install, pip list | import, code execution |
| Language | Shell commands (bash, cmd) | Python statements |
| Example | pip install requests | import requests |
Step-by-Step Guide
Example 1: Installing requests
The requests package lets you make HTTP requests (fetch web pages, call APIs). Here is how to install and use it:
Example 2: Version Pinning
Sometimes you need a specific version of a package. pip lets you pin versions using comparison operators:
Example 3: requirements.txt
Real projects use many packages. A requirements.txt file lists all packages a project needs, so anyone can install them in one command:
Other Useful pip Commands
Ways to import
Once a package is installed, there are several ways to import it into your Python code:
Understanding Dependencies
Packages often rely on other packages to work. These are called dependencies. When you install a package, pip automatically installs all its dependencies too.
Common Mistakes
Summary Table
| Command / Action | Where? | What It Does |
|---|---|---|
| pip install pkg | Terminal | Downloads and installs a package |
| pip uninstall pkg | Terminal | Removes an installed package |
| pip list | Terminal | Shows all installed packages |
| pip freeze | Terminal | Lists packages with exact versions |
| pip install -r file.txt | Terminal | Installs from a requirements file |
| import pkg | Python file | Makes the package usable in code |
| from pkg import func | Python file | Imports a specific function |
Question 1 of 5
Where should you type the command pip install requests?
Question 2 of 5
After running pip install numpy in the terminal, what is the next step to use numpy in your code?
Question 3 of 5
What does the command pip install -r requirements.txt do?
Question 4 of 5
Which import statement lets you use numpy.array() as just np.array()?
Question 5 of 5
When you run pip install flask, pip also installs Werkzeug, Jinja2, click, and other packages. What is the correct term for these additional packages?