🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

pip

AI & DATA SCIENCE // pip

pip is Python's standard package manager, used from the command line to install, upgrade, and remove third-party packages from the Python Package Index (PyPI).

Syntax

pip install requests
pip install requests==2.31.0
pip uninstall requests
pip freeze > requirements.txt

Deep Dive Course

pip downloads packages from PyPI, or another configured index, and installs them into your Python environment's site-packages directory, making them importable. It's not part of the Python language itself — it's a separate command-line tool, though it ships bundled with modern Python installations. Installing from a requirements file installs every package and version listed there, which is the standard way to reproduce a consistent set of dependencies across machines, and freezing the current environment generates that file from whatever's currently installed.

1Understanding pip

pip downloads packages from PyPI, or another configured index, and installs them into your Python environment's site-packages directory, making them importable. It's not part of the Python language itself — it's a separate command-line tool, though it ships bundled with modern Python installations. Installing from a requirements file installs every package and version listed there, which is the standard way to reproduce a consistent set of dependencies across machines, and freezing the current environment generates that file from whatever's currently installed.

💡

Install packages inside a virtual environment, created with venv or a similar tool, not into your system-wide Python installation — this keeps each project's dependencies isolated and avoids version conflicts between unrelated projects.

editor.html
# Run from a terminal, not inside a Python script:
# pip install requests

import requests
print(requests.__name__)
localhost:3000

2Practical Example

Here is a real-world application of pip showing how it is used in production Python code.

editor.html
# requirements.txt
# requests==2.31.0
# numpy==1.26.0

# Install everything listed, from a terminal:
# pip install -r requirements.txt
localhost:3000

3Best Practices

Follow these guidelines when working with pip:

1. Use a virtual environment per project instead of installing packages globally, to keep dependencies isolated

2. Pin exact versions in a requirements file, or use a lockfile-based tool, for reproducible installs across machines and CI

3. Upgrade pip itself occasionally, since it's versioned and updated separately from Python

⚠️

Tip: Install packages inside a virtual environment, created with venv or a similar tool, not into your system-wide Python installation — this keeps each project's dependencies isolated and avoids version conflicts between unrelated projects.

editor.html
# Run from a terminal, not inside a Python script:
# pip install requests

import requests
print(requests.__name__)
localhost:3000

Examples

Example 01Basic Usage
# Run from a terminal, not inside a Python script:
# pip install requests

import requests
print(requests.__name__)
Example 02Advanced Example
# requirements.txt
# requests==2.31.0
# numpy==1.26.0

# Install everything listed, from a terminal:
# pip install -r requirements.txt

Best Practices

  • Use a virtual environment per project instead of installing packages globally, to keep dependencies isolated
  • Pin exact versions in a requirements file, or use a lockfile-based tool, for reproducible installs across machines and CI
  • Upgrade pip itself occasionally, since it's versioned and updated separately from Python

Interview Question

Why is it considered best practice to install packages inside a virtual environment instead of directly into the system Python?

Hint: Think about what happens when two different projects need different versions of the same package.

A virtual environment gives each project its own isolated set of installed packages, completely separate from the system Python and from other projects. Without one, installing a specific version of a package for one project can silently break another project on the same machine that needs a different, incompatible version of that same package, since there'd only be one shared, global installation. Virtual environments avoid this by letting each project pin exactly the dependency versions it needs, independently of everything else on the machine.

Exercises

MediumPractice using pip in a real scenario.
View Solution
# Run from a terminal, not inside a Python script:
# pip install requests

import requests
print(requests.__name__)

Frequently Asked Questions

Why is it considered best practice to install packages inside a virtual environment instead of directly into the system Python?

A virtual environment gives each project its own isolated set of installed packages, completely separate from the system Python and from other projects. Without one, installing a specific version of a package for one project can silently break another project on the same machine that needs a different, incompatible version of that same package, since there'd only be one shared, global installation. Virtual environments avoid this by letting each project pin exactly the dependency versions it needs, independently of everything else on the machine.

Related Functions

import-statementwith-statementfrom-import