PYTHON

Python Package Manager (pip): Syntax, Usage, and Examples

pip is Python’s package manager, it lets you install, update, and remove libraries so you can use them in your projects. You’ll use pip anytime you want to add third-party tools like requests, pandas, or flask.


How to Use pip

Most pip commands follow a simple pattern:

pip <command> <package-name>

If your machine has multiple Python versions installed, using python -m pip is the safest option because it runs pip through the exact Python interpreter you chose.

python -m pip --version

Install a package

To install a package:

pip install requests

Or the “always correct interpreter” version:

python -m pip install requests

After that, you can import it in Python:

import requests

response = requests.get("https://example.com")
print(response.status_code)

Install a specific version

Sometimes you need an exact version because your project depends on it:

pip install"Django==4.2.7"

That’s common in work projects where everyone needs the same setup.

Upgrade a package

To upgrade a package to the latest version:

pip install --upgrade requests

Uninstall a package

Remove a package you don’t need anymore:

pip uninstall requests

pip will ask for confirmation before deleting it.


When to Use pip

pip solves one big problem: you don’t want to write everything yourself. Installing a package often saves you days of work.

1) Adding features without reinventing the wheel

Need to send an HTTP request?

  • You could write low-level socket code…
  • Or you can install requests and move on with your life.
pip install requests

2) Keeping projects consistent across machines

If your app works on your laptop but breaks on your teammate’s, the issue is often mismatched dependencies. pip helps you lock versions so everyone runs the same setup.

3) Creating clean environments for different projects

One project might need Django, another might need FastAPI. Mixing dependencies in one global Python install gets messy fast, so pip is usually paired with virtual environments.

4) Updating libraries for bug fixes and security patches

Old versions of packages can cause weird errors or security issues. pip makes updating quick and predictable.


Examples of pip in Action

Here are a few realistic examples you’ll actually run into.

Example 1: Installing a package and using it

Let’s say you want to work with JSON more comfortably, and you want to call an API.

Install requests:

python -m pip install requests

Use it in code:

import requests

data = requests.get("https://api.github.com").json()
print(data["current_user_url"])

This is one of those “pip moments” where you feel like you unlocked a cheat code.


Example 2: Installing multiple packages at once

You can install several packages in one command:

pip install flask sqlalchemy python-dotenv

That’s useful when you’re setting up a web app with a database and environment variables.


Example 3: Checking what packages are installed

To see what’s installed in your current environment:

pip list

You’ll get a table of package names and versions.

To show details for one package:

pip show requests

This includes info like:

  • installed version
  • location on disk
  • dependencies

Example 4: Saving dependencies with a requirements.txt file

Many teams store dependencies in a requirements.txt file so the project setup is repeatable.

Create a requirements file:

pip freeze > requirements.txt

Then someone else can install the same dependencies with:

pip install -r requirements.txt

That’s how you avoid the classic “it works on my machine” argument.


Example 5: Installing from a local folder

If you have a local package you’re developing, you can install it too:

pip install .

That means “install the package in the current directory.”

During development, editable installs are also common:

pip install -e .

Now changes in your code show up immediately without reinstalling every time.


Learn More About pip

pip is easy to start with, but a few extra ideas will save you pain later.

Using pip with virtual environments

Virtual environments keep your dependencies isolated per project.

Create a virtual environment:

python -m venv .venv

Activate it on macOS/Linux:

source .venv/bin/activate

Activate it on Windows:

.venv\Scripts\activate

Now when you run:

pip install flask

Flask installs inside the .venv environment, not globally.

When you’re done working, you can deactivate it:

deactivate


pip vs conda

Some people use Conda instead of pip, especially in data science.

The practical difference:

  • pip installs Python packages from the Python Package Index (PyPI)
  • conda manages both Python packages and system-level dependencies

If you’re writing general Python apps, pip is usually enough. If you work heavily with scientific libraries that depend on compiled code, conda can be helpful.


Installing packages for the right Python version

A very common beginner trap:

  • You run pip install ...
  • Then Python still says “ModuleNotFoundError”

This often happens because pip is linked to a different Python version than the one running your code.

That’s why this version is so reliable:

python -m pip install <package-name>

It ties pip to the interpreter you used.

If you specifically want Python 3:

python3 -m pip install <package-name>


Avoiding common pip mistakes

1) Installing packages globally by accident

If you install everything without a virtual environment, your global Python setup becomes a junk drawer.

Virtual environments keep things clean.

2) Forgetting to freeze dependencies

A project without pinned versions can behave differently over time as packages update.

requirements.txt keeps installs consistent.

3) Using the package name instead of the import name

Some packages install under one name but import under another.

Example:

pip install pillow

But in Python:

from PILimport Image

That mismatch is normal, and it confuses everyone at least once.

4) Trying to install standard library modules

Some modules come with Python already, so pip won’t help.

Example: you don’t install these with pip:

  • json
  • math
  • datetime

If you try pip install json, you’ll likely install some unrelated third-party package that just happens to share the name. That’s a sneaky one.


Using pip in scripts and automation

In real projects, pip commands often show up in:

  • setup scripts (Makefile, shell scripts)
  • CI pipelines (GitHub Actions, GitLab CI)
  • Docker containers

Example of a typical install step:

pip install -r requirements.txt

That one line can rebuild a whole environment from scratch.


pip caching and download behavior

pip caches packages locally so it doesn’t re-download everything every time. If something goes wrong and you need a clean install, you can force a reinstall:

pip install --force-reinstall requests

You won’t use this daily, but it’s useful when troubleshooting dependency issues.


Summary

pip is the tool you use to install and manage Python libraries. You can install packages, upgrade them, remove them, and share dependency lists with others. Combine pip with a virtual environment and a requirements.txt file, and you’ll have a setup that stays clean and predictable, even as your projects grow.