01 / 20

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.

Python

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.

Standard Library
Built into Python itself: math, random, os. No installation needed.
Third-Party
Created by the community: requests, flask, numpy. Must be installed first.

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

Your Computer pip installed pip install Internet (download) PyPI 500,000+ packages pip connects your computer to PyPI's massive library

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.

Saves Time
Don't reinvent the wheel: use battle-tested code others have already written and debugged.
Manages Versions
pip tracks which version you install so your project stays consistent and reproducible.
Handles Dependencies
When a package needs other packages, pip automatically installs them all for you.

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:

$ pip install package_name
# Examples: $ pip install requests $ pip install flask $ pip install numpy

The Full Workflow

Step 1 pip install pkg (in terminal) Step 2 pip downloads (from PyPI) Step 3 Installed! Step 4: In your .py file import package_name Step 5: Use it in your code! Install once → import in every file that needs it

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

1
Open your terminal. On Windows: search for "Command Prompt" or "PowerShell". On Mac/Linux: open "Terminal".
2
Type the install command. Enter pip install package_name and press Enter. Wait for the download to finish.
3
Verify the installation. Type pip show package_name to confirm it was installed correctly.
4
Import in your Python file. At the top of your .py file, write import package_name.
5
Use the package. Call functions and classes from the package in your code. Check the package's documentation for examples.

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:

# Step 1: In your TERMINAL $ pip install requests
# Step 2: In your Python file (app.py) import requests response = requests.get("https://api.github.com") print(response.status_code) # 200 print(response.json()) # JSON data

Example 2: Version Pinning

Sometimes you need a specific version of a package. pip lets you pin versions using comparison operators:

# Install exact version $ pip install requests==2.31.0 # Install minimum version $ pip install requests>=2.28.0 # Install latest (default) $ pip install requests
==
Exact version: install only this specific version.
>=
Minimum version: this version or newer.
(none)
Latest: always grabs the newest release from PyPI.

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:

# requirements.txt file contents: requests==2.31.0 flask==3.0.0 numpy>=1.24.0 pandas==2.1.4
# Install everything at once: $ pip install -r requirements.txt

Other Useful pip Commands

1
pip list: shows all packages currently installed in your environment.
2
pip show package_name: displays details about a specific installed package (version, location, dependencies).
3
pip uninstall package_name: removes a package from your system.
4
pip freeze: outputs all installed packages and their exact versions (great for creating requirements.txt).
$ pip freeze > requirements.txt # save to file

Ways to import

Once a package is installed, there are several ways to import it into your Python code:

# Import the entire package import numpy arr = numpy.array([1, 2, 3]) # Import with an alias (shortcut name) import numpy as np arr = np.array([1, 2, 3]) # Import a specific function from numpy import array arr = array([1, 2, 3])

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.

flask Werkzeug Jinja2 click MarkupSafe pip install flask → automatically installs 4+ dependencies

Common Mistakes

✕ pip in Python Shell
Typing pip install requests at the >>> prompt causes a SyntaxError. Use the terminal, not Python.
✕ Misspelled Package Name
Typing pip install reqeusts (typo) fails silently or installs the wrong package. Always double-check PyPI.
✕ Forgetting to Import
Installing a package is not enough. You must write import package_name at the top of each file that uses it.
✕ Wrong Python Environment
Installing in one Python version but running code in another. Use pip3 or python -m pip to be explicit.

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?