Python PIP


PIP is the package manager for Python. It helps you install, update and remove external libraries that are not included in the standard Python installation. Almost every Python project depends on packages, so learning PIP is important if you plan to work with frameworks, automation tools, data science libraries or web development.

In this tutorial, you will learn what PIP is, how to check if it is installed, how to use it to install and manage packages, how to uninstall packages, how to see which packages are installed and how to work with requirements files. All examples are simple and practical so you can start using PIP confidently.

What Is PIP?

PIP stands for "Package Installer for Python." It lets you download and install Python packages from the Python Package Index (PyPI). PyPI is an online repository of thousands of ready-to-use libraries created by developers worldwide.

Some popular packages include:

  • requests for handling HTTP requests

  • numpy for mathematical calculations

  • pandas for data analysis

  • flask and django for web applications

  • opencv-python for image processing

Instead of writing everything from scratch, you can use these libraries to speed up your work.

Check If PIP Is Installed

Most Python versions include PIP automatically. You can check it by running this command in your terminal or command prompt:

pip --version

If PIP is installed, you will see something like:

pip 24.0 from /usr/local/lib/python3.12/site-packages

If you see an error, it means PIP is not installed or not added to the system path.

Installing a Package With PIP

Use the pip install command to install any package.

Example:

pip install requests

This will download the requests library and install it on your system.

You can also install a specific version:

pip install requests==2.31.0

This is useful when your project requires a fixed version.

Updating a Package

To update an installed package to its latest version, use:

pip install --upgrade requests

This checks PyPI for new releases and updates the package automatically.

Uninstalling a Package

If you no longer need a library, remove it using:

pip uninstall requests

You will be asked to confirm the uninstall process.

Installing Multiple Packages at Once

You can install more than one package in a single command:

pip install numpy pandas matplotlib

This saves time when setting up new projects.

Viewing Installed Packages

Use this command to list all installed packages:

pip list

It shows package names and versions.

Example output:

numpy       1.26.0
pandas      2.1.1
requests    2.31.0

Checking Which Packages Need Updates

To find outdated packages:

pip list --outdated

This is helpful when you want to keep your environment up to date.

Getting Package Details

If you want to know more about a package, use:

pip show requests

This gives you information like version, summary, author and installation location.

Installing Packages Using a Requirements File

A requirements.txt file is commonly used in projects to store package names and their versions.

Example requirements.txt:

numpy==1.26.0
pandas==2.1.1
requests==2.31.0

To install everything inside the file:

pip install -r requirements.txt

This is very useful when sharing a project with someone else or deploying a project on a server.

Creating a Requirements File Automatically

You can generate a requirements.txt file using:

pip freeze > requirements.txt

This command saves all your installed packages with exact versions.

Installing Packages Locally or Globally

By default, PIP installs packages globally. You can also install packages only for a project by using a virtual environment.

Example:

python -m venv myenv
source myenv/bin/activate   # Mac/Linux
myenv\Scripts\activate      # Windows
pip install flask

This keeps your project dependencies isolated and avoids version conflicts.

Installing a Package From a URL

Some packages are stored on GitHub or a custom server. You can install them like this:

pip install https://github.com/user/repository/archive/main.zip

You can even install from a GitHub repo:

pip install git+https://github.com/psf/requests

Installing a Package in Editable Mode

Developers use editable mode when working on packages locally:

pip install -e .

This installs the package in a way that reflects live changes.

Fixing Common PIP Errors

1. Permission Denied

Try adding --user:

pip install --user package_name

2. PIP Not Recognized

Use:

python -m pip install package_name

3. Upgrade PIP

If PIP is outdated:

pip install --upgrade pip

Practical Examples

  1. Install Django

pip install django
  1. Install the latest version of NumPy

pip install --upgrade numpy
  1. Remove Pandas

pip uninstall pandas
  1. Check a package version

pip show flask
  1. List outdated packages

pip list --outdated
  1. Install packages for a project

pip install -r requirements.txt
  1. Generate a requirements file

pip freeze > requirements.txt
  1. Install a package from GitHub

pip install git+https://github.com/pallets/flask
  1. Install specific version

pip install matplotlib==3.7.1
  1. Reinstall a package

pip install --force-reinstall requests

Summary of the Tutorial

PIP makes it easy to install and manage Python packages. You learned how to install, upgrade and remove packages, how to list installed packages, how to use requirements files and how to install packages from URLs or GitHub. PIP is a key tool for every Python developer, and understanding how to use it gives you control over your project’s dependencies.


Practice Questions

Q1. Write a terminal command to check your current pip version.

Q2. Write a terminal command to install the pandas package using pip.

Q3. Write a Python program to import and use the math package (standard library).

Q4. Write a terminal command to uninstall the flask package from your system.

Q5. Write a terminal command to list all installed packages and check if requests is installed.

Q6. Write a terminal command to show full details of the numpy package using pip show.

Q7. Write a terminal command to upgrade the scikit-learn package to the latest version.

Q8. Write a terminal command to install packages from a requirements.txt file.

Q9. Write a terminal command to try installing a non-existent package and observe the error.

Q10. Write a terminal command to reinstall a broken package using pip.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top