How to Add a Package in Python: pip, Virtual Environments, and What to Know First

Adding packages to Python is one of the most common tasks any developer faces — whether you're building a web app, scraping data, or automating a workflow. Python's packaging ecosystem is powerful, but it has enough moving parts that the "right" approach genuinely depends on your setup.

What Is a Python Package?

A Python package is a bundle of pre-written code that extends what Python can do out of the box. Instead of writing everything from scratch, you install a package and import its functionality into your project.

Packages are hosted on PyPI (the Python Package Index), a public repository containing hundreds of thousands of libraries — from web frameworks like Django and Flask to data tools like NumPy and pandas.

The Standard Way: Using pip

pip is Python's built-in package manager. It comes pre-installed with Python 3.4 and later. For most use cases, this is where you start.

Basic install command

pip install package-name 

Replace package-name with the actual library you want. For example:

pip install requests 

This downloads and installs the latest version of the requests library.

Installing a specific version

pip install requests==2.28.0 

Pinning a version matters when your project depends on a specific API or behavior that may have changed in newer releases.

Upgrading an existing package

pip install --upgrade package-name 

Uninstalling a package

pip uninstall package-name 

pip3 vs pip — Which One Do You Use?

On systems that have both Python 2 and Python 3 installed, pip may point to the Python 2 version. In that case, use:

pip3 install package-name 

If you're unsure which Python version pip is tied to, run:

pip --version 

This will show the Python version it's associated with.

Virtual Environments: Why They Matter 🔧

Installing packages globally (directly onto your system Python) works for quick experiments, but it creates real problems in professional or multi-project workflows.

A virtual environment is an isolated Python installation scoped to a single project. Packages installed inside it don't affect anything outside it.

Creating a virtual environment

python -m venv myenv 

This creates a folder called myenv containing a self-contained Python interpreter and its own pip.

Activating it

Operating SystemCommand
macOS / Linuxsource myenv/bin/activate
Windows (CMD)myenvScriptsactivate.bat
Windows (PowerShell)myenvScriptsActivate.ps1

Once activated, any pip install commands go into that environment only.

Deactivating

deactivate 

Installing from a Requirements File

On collaborative or reproducible projects, dependencies are typically listed in a requirements.txt file:

requests==2.28.0 flask>=2.0 numpy 

Install everything in one command:

pip install -r requirements.txt 

This is the standard practice for sharing a project so others can replicate your exact environment.

Alternative Package Managers Worth Knowing

pip isn't the only option. Depending on your workflow, other tools may be more appropriate:

ToolBest For
condaData science, scientific computing, managing non-Python dependencies
poetryModern dependency management and packaging in one tool
pipenvCombines pip + virtual environments with a Pipfile format
uvExtremely fast pip-compatible installer, growing in adoption

Each has a different philosophy around dependency resolution, lockfiles, and environment management. conda, for instance, can install packages that include compiled C libraries — something pip alone can struggle with in certain data science contexts.

Common Issues When Adding Packages

"pip is not recognized" — Python may not be added to your system PATH. Reinstalling Python and checking the "Add to PATH" option usually fixes this on Windows.

Permission errors — On macOS/Linux, avoid using sudo pip install globally. It's better practice to use a virtual environment instead.

Package installs but won't import — This almost always means you installed into a different Python environment than the one your script is running in. Check which Python interpreter your editor or terminal is using.

Dependency conflicts — Two packages requiring incompatible versions of a shared dependency. This is where tools like poetry or conda's solver provide more robust conflict resolution than plain pip.

What Affects Your Best Approach 🐍

The "right" way to add packages in Python shifts significantly based on a few factors:

  • Operating system — Windows, macOS, and Linux each have slightly different pip behavior and PATH setups
  • Python version — Python 3.11+ and older versions can behave differently with certain packages
  • Project type — A quick script, a production web app, and a data science notebook have very different dependency management needs
  • Team or solo work — Reproducibility requirements change whether a simple pip install or a full lockfile workflow makes sense
  • IDE or environment — Tools like VS Code, PyCharm, Jupyter, and Anaconda each have their own ways of managing Python environments that may interact with pip

For a simple personal script, a global pip install might be perfectly adequate. For a Django web project deployed to a server, a virtual environment with a pinned requirements file is standard. For heavy scientific computing, conda environments are often the more reliable path.

The mechanics of installing a package are straightforward — but which tool to use and how to structure your environment depends entirely on what you're building and how you work.