Installing Packages
Installing Packages in Python
Learn how to install, verify, upgrade, and manage Python packages using pip and virtual environments.
Python includes a rich standard library, but many applications need additional packages for web development, data analysis, automation, testing, machine learning, database operations, and other specialized tasks.
These third-party packages can be installed using
pip, the standard package installer for Python.
The pip install command downloads the requested package,
resolves its required dependencies, and installs everything into the
selected Python environment.
Prerequisites
Before installing Python packages, make sure the following requirements are available:
What You Need
- Python 3 should be installed on your computer.
- pip should be available in the selected Python installation.
- You should know how to open Command Prompt, PowerShell, Terminal, or another command-line shell.
- An internet connection is normally required when downloading packages from an online package source.
- A virtual environment is strongly recommended for each Python project.
Verify Python and pip
Before installing a package, verify that Python and pip are available on your computer.
Windows
py --version
py -m pip --version
Linux or macOS
python3 --version
python3 -m pip --version
If your computer uses python as the Python 3 command,
you can use:
python --version
python -m pip --version
Example output:
Python 3.x.x
pip x.x.x from C:\Python\Lib\site-packages\pip
>>>.
How Package Installation Works
Read the Package Requirement
pip reads the package name and any version condition included in the command.
Resolve Dependencies
pip identifies other packages required by the requested package.
Download Distribution Files
pip downloads a suitable package distribution and its dependencies.
Install the Package
The selected packages are installed in the active Python environment.
Create a Virtual Environment
A virtual environment creates an isolated Python environment for a project. This helps prevent package and version conflicts between different projects.
Step 1: Create a Project Folder
mkdir package-demo
cd package-demo
Step 2: Create the Virtual Environment
python -m venv .venv
On Windows, you may also use:
py -m venv .venv
On Linux or macOS, you may need to use:
python3 -m venv .venv
Step 3: Activate on Windows Command Prompt
.venv\Scripts\activate
Step 3: Activate on Windows PowerShell
.venv\Scripts\Activate.ps1
Step 3: Activate on Linux or macOS
source .venv/bin/activate
After activation, the virtual environment name normally appears before the terminal prompt:
(.venv) C:\projects\package-demo>
Install a Python Package
The basic installation syntax is:
python -m pip install package_name
For example, install the Requests package:
python -m pip install requests
Example output:
Collecting requests
Downloading requests-2.x.x-py3-none-any.whl
Installing collected packages: requests
Successfully installed requests-2.x.x
The exact package version, filename, dependencies, and displayed output may differ in your environment.
Why Use python -m pip?
You may see packages installed with the shorter command:
pip install requests
However, the following form is generally clearer:
python -m pip install requests
The -m option asks the selected Python interpreter to
run pip as a module. This helps ensure that the package is installed
for the intended Python interpreter.
Potentially Ambiguous
-
The
pipcommand may point to another Python installation. - Multiple Python versions can create confusion.
- The standalone command may not be available through PATH.
More Explicit
-
python -m pipuses the selected interpreter. - It helps reduce problems caused by multiple Python installations.
- It clearly shows which Python environment runs pip.
Verify the Installed Package
After installation, verify that the package is available.
Display Package Information
python -m pip show requests
Example output:
Name: requests
Version: 2.x.x
Summary: Python HTTP for Humans.
Location: C:\project\.venv\Lib\site-packages
Requires: certifi, charset-normalizer, idna, urllib3
List Installed Packages
python -m pip list
Import the Package
import requests
print("Requests version:", requests.__version__)
Possible output:
Requests version: 2.x.x
Use the Installed Package
Create a file named app.py and add the following code:
import requests
url = "https://example.com"
try:
response = requests.get(url, timeout=10)
print("Status code:", response.status_code)
print("Content type:", response.headers.get("content-type"))
except requests.RequestException as error:
print("Request failed:", error)
Run the program:
python app.py
Possible output:
Status code: 200
Content type: text/html
Install Multiple Packages
Multiple packages can be installed using one command:
python -m pip install requests flask pytest
pip processes the supplied package requirements and attempts to install compatible versions of the packages and their dependencies.
Install a Particular Package Version
Version specifiers can be used to control which package version should be installed.
| Specifier | Purpose | Example |
|---|---|---|
==
|
Install an exact version. |
requests==2.32.3
|
>=
|
Install the specified version or a newer version. |
requests>=2.30.0
|
<
|
Install a version lower than the specified version. |
requests<3.0
|
!=
|
Exclude a particular version. |
requests!=2.31.0
|
~=
|
Install a compatible release. |
requests~=2.32.0
|
Install an Exact Version
python -m pip install requests==2.32.3
Install a Minimum Version
python -m pip install "requests>=2.30.0"
Install Within a Version Range
python -m pip install "requests>=2.30.0,<3.0.0"
> and <.
Upgrade an Installed Package
Use the --upgrade option to request a newer compatible
package version:
python -m pip install --upgrade requests
The shorter form is:
python -m pip install -U requests
You can also upgrade or change the package to an exact version:
python -m pip install --upgrade requests==2.32.3
Reinstall a Package
If package files are damaged or need to be replaced, use:
python -m pip install --force-reinstall requests
Install Packages from requirements.txt
A requirements file allows a project to define multiple package requirements in one place.
Create a file named requirements.txt:
requests==2.32.3
Flask>=3.0,<4.0
pytest~=8.3
Install all packages listed in the file:
python -m pip install -r requirements.txt
Record Installed Packages
Use pip freeze to generate a list of installed packages:
python -m pip freeze > requirements.txt
Example generated file:
certifi==2025.4.26
charset-normalizer==3.4.2
idna==3.10
requests==2.32.3
urllib3==2.4.0
Review the generated file before sharing it because
pip freeze records direct and supporting packages
installed in the current environment.
Install from a Wheel File
A wheel is a built Python distribution whose filename ends with
.whl. A local wheel file can be installed using:
python -m pip install ./dist/package_name-1.0.0-py3-none-any.whl
Install from a Source Archive
A source distribution archive can also be installed:
python -m pip install ./dist/package_name-1.0.0.tar.gz
Source installation may require compilers, system libraries, development headers, or other build tools.
Wheel File
- Already prepared as a built distribution.
- Usually faster to install.
- May avoid local compilation.
Source Archive
- May require a local build process.
- May require compilers or system libraries.
- Installation can vary between platforms.
Install a Local Python Project
A local Python project can be installed by supplying its directory path:
python -m pip install ./my_project
If the terminal is already inside the project directory, use:
python -m pip install .
A simplified project structure may look like this:
my_project/
|-- pyproject.toml
|-- README.md
|-- src/
| `-- my_package/
| `-- __init__.py
`-- tests/
Install a Project in Editable Mode
Editable installation is useful while actively developing a local Python package:
python -m pip install --editable .
The shorter form is:
python -m pip install -e .
Install Without Dependencies
Use --no-deps to prevent automatic dependency
installation:
python -m pip install --no-deps requests
Install a Pre-release Package
Use the --pre option to allow compatible pre-release
versions:
python -m pip install --pre package_name
Check Installed Dependencies
Use pip check to identify missing or incompatible
dependencies:
python -m pip check
Successful output:
No broken requirements found.
Uninstall a Package
If a package is no longer required, uninstall it using:
python -m pip uninstall requests
pip normally requests confirmation before removing the package:
Proceed (Y/n)?
Common Installation Problems
pip Is Not Recognized
The standalone pip command is unavailable or not included in the system PATH.
'pip' is not recognized as an internal or external command
python -m pip --version,
py -m pip --version, or
python3 -m pip --version.
ModuleNotFoundError After Installation
The package may have been installed into a different Python environment.
ModuleNotFoundError: No module named 'requests'
python -m pip show requests.
No Matching Distribution Found
pip cannot find a compatible distribution for the requested package or version.
No matching distribution found for package_name
Permission Denied
The current user cannot modify the selected installation location.
Package Build Failed
pip may be trying to build a package from source because a compatible wheel is unavailable.
Package Installation Security
Installing a package introduces third-party code into your Python environment. Package selection should therefore be treated as a software supply-chain decision.
Safer Installation Practices
- Verify the exact package name before installation.
- Review package documentation, ownership, release history, repository, and license when appropriate.
- Be careful with package names that imitate or misspell popular projects.
- Install project dependencies inside a virtual environment.
- Review version changes before upgrading an important application.
- Record and review the dependencies used by the project.
- Follow organizational security, privacy, licensing, and open-source policies.
Risky and Recommended Practices
Risky Practices
- Installing every package globally.
- Using pip without checking which Python installation it belongs to.
- Installing a package with a misspelled name.
- Leaving all dependency versions uncontrolled.
- Ignoring dependency conflict messages.
- Running administrator commands unnecessarily.
Recommended Practices
- Use one virtual environment for each project.
-
Use
python -m pipfor interpreter clarity. - Verify the package identity and version before installation.
- Maintain a project requirements file.
-
Run
pip checkafter dependency changes. - Test upgrades before using them in an important system.
Complete Installation Workflow
The following command sequence demonstrates a practical package installation workflow:
# Create the project directory
mkdir requests-demo
cd requests-demo
# Create a virtual environment
python -m venv .venv
# Activate the virtual environment
# Windows Command Prompt:
.venv\Scripts\activate
# Linux or macOS:
# source .venv/bin/activate
# Verify pip
python -m pip --version
# Install the package
python -m pip install requests
# Verify the installation
python -m pip show requests
python -m pip list
python -m pip check
# Record installed packages
python -m pip freeze > requirements.txt
# Deactivate when finished
deactivate
Hands-On Practice
Practice Assignment
-
Create a project folder named
package-practice. - Create and activate a virtual environment.
- Install the Requests and pytest packages.
- Display information about both installed packages.
- List all packages in the environment.
- Run
pip check. - Create a
requirements.txtfile. - Create a second virtual environment.
- Install all packages from the generated requirements file.
- Verify the packages in the second environment.
Knowledge Check
How do you install a Python package?
python -m pip install package_name
How do you install an exact package version?
python -m pip install package_name==1.0.0
How do you install packages from requirements.txt?
python -m pip install -r requirements.txt
Why should you use a virtual environment?
It isolates the packages and versions required by one project from other Python projects.
How do you install a local project?
python -m pip install .
How do you install a project in editable mode?
python -m pip install -e .
Package Installation Quick Reference
# Verify pip
python -m pip --version
# Install a package
python -m pip install requests
# Install multiple packages
python -m pip install requests flask pytest
# Install an exact version
python -m pip install requests==2.32.3
# Install within a version range
python -m pip install "requests>=2.30.0,<3.0.0"
# Upgrade a package
python -m pip install --upgrade requests
# Reinstall a package
python -m pip install --force-reinstall requests
# Install from requirements.txt
python -m pip install -r requirements.txt
# Install a local project
python -m pip install .
# Install in editable mode
python -m pip install -e .
# Install from a wheel
python -m pip install ./dist/package_name-1.0.0-py3-none-any.whl
# Install from a source archive
python -m pip install ./dist/package_name-1.0.0.tar.gz
# Display installed packages
python -m pip list
# Display package information
python -m pip show requests
# Check dependencies
python -m pip check
# Generate requirements.txt
python -m pip freeze > requirements.txt
# Uninstall a package
python -m pip uninstall requests
# Deactivate the virtual environment
deactivate
Summary
What You Learned
-
Python packages can be installed using
python -m pip install. - Virtual environments isolate package installations for individual projects.
- Version specifiers control which package versions can be installed.
- Multiple packages can be installed from a requirements file.
- pip can install local projects, wheel files, and source archives.
-
pip show,pip list, andpip checkhelp verify an installation. - Third-party packages should be reviewed before they are installed.
Key Takeaway
Install Python packages inside a virtual environment, use python -m pip to target the intended Python interpreter, apply suitable version constraints, record project dependencies, and verify every installation before using the package in an application.