← Back to Chapters

Python PIP

? Python PIP

⚡ Quick Overview

PIP is the official package manager for Python. It lets you install, upgrade, list, and uninstall third-party libraries that are not included in the standard Python library. With PIP, you can quickly add powerful tools like requests, numpy, and many more to your projects.

? Key Concepts

  • PIP stands for “PIP Installs Packages”.
  • It is used to manage external Python libraries from the command line.
  • Packages are usually downloaded from the Python Package Index (PyPI).
  • You can install, list, upgrade, and uninstall packages.
  • requirements.txt is a file that stores all your project dependencies.
  • Virtual environments help isolate project-specific packages.

? Syntax and Commands

The general syntax of a PIP command is:

# General pip command syntax
pip <command> [options] [package_name]
  • install – install a new package.
  • list – show all installed packages.
  • uninstall – remove a package.
  • freeze – output installed packages in a format suitable for requirements.txt.
  • --upgrade – upgrade an existing package to the latest version.

? Code Examples with PIP

? Install a Package
# Install requests library
pip install requests
? List Installed Packages
# List all installed packages
pip list
⬆️ Upgrade a Package
# Upgrade requests library
pip install --upgrade requests
❌ Uninstall a Package
# Uninstall requests library
pip uninstall requests
? Work with a requirements.txt File
# Save installed packages to requirements.txt
pip freeze > requirements.txt

# Install packages from requirements.txt
pip install -r requirements.txt

? Output and Explanation

? What These Commands Do

  • pip install requests Downloads the requests package from PyPI and installs it so you can use import requests in your Python code.
  • pip list Shows all installed packages and their versions. Useful to quickly check what is available in your environment.
  • pip install --upgrade requests Updates the requests package to the latest version that matches your constraints.
  • pip uninstall requests Removes the package from your environment after asking for confirmation.
  • pip freeze > requirements.txt Saves all installed packages and their exact versions into a requirements.txt file.
  • pip install -r requirements.txt Reads the file and installs all listed packages, recreating the same environment on another machine.

? Tips and Best Practices

  • Use pip --version to check if PIP is installed and which version you have.
  • Create and use virtual environments to avoid conflicts between different projects.
  • Regularly run pip install --upgrade pip to keep PIP itself up to date.
  • Always pin package versions in requirements.txt (for example, requests==2.32.0) to ensure stability.
  • Commit your requirements.txt file to version control so others can recreate your environment easily.

? Try It Yourself

  • Install the numpy package using PIP and verify it appears in pip list.
  • Upgrade an already installed package (for example, pandas) using pip install --upgrade pandas.
  • Create a requirements.txt file for your current project with pip freeze > requirements.txt, then recreate the environment in a new virtual environment using pip install -r requirements.txt.