`pip` and packaging
pip is the standard tool that installs Python packages from PyPI or a local project into the current interpreter. Packaging is the work of declaring dependencies and shipping your own code as an installable distribution.
Detailed Explanation
A package on PyPI is a versioned archive with a name, metadata, and files to drop into site-packages. python -m pip install requests finds the latest version that matches your interpreter, downloads a wheel when one exists, and installs it into the environment you are using. python -m pip is the safe invocation: it uses the pip that belongs to that python, not some other pip on PATH.
Install into a virtual environment. A global install is how two projects pin different versions and then break each other. Pin what you deploy. pip freeze > requirements.txt captures everything currently installed, including transitive dependencies. For an application, a lock file (pip-tools, uv lock, poetry.lock) is the reproducible form. For a library, declare ranges in pyproject.toml and let the application lock them.
pyproject.toml is the current front door. [project] holds the name, version, runtime dependencies, and optional extras. [build-system] names the build backend (setuptools, hatchling, flit). setup.py still exists for older projects; new projects should not start there.
A wheel (.whl) is a built package. An sdist (.tar.gz) is source that must be built on the installer's machine. Wheels install faster and avoid a compiler. If pip starts compiling, you are missing a wheel for that platform or Python version.
Editable installs (pip install -e .) link your project into the environment so edits apply without reinstalling. That is the usual development setup. Production images should install a pinned wheel or sdist, not an editable tree.
pip install can also take a Git URL, a local path, or a constraints file. pip uninstall, pip list, and pip show inspect the environment. pip index and pip download help in offline or audited setups.
Do not run pip install as root into a system Python on Linux. Use a venv, or the distribution's package manager for system tools.
Code Examples
python -m pip install -U pip
python -m pip install requests==2.32.3
python -m pip show requests
python -m pip freeze | grep -i requests# pyproject.toml (library or app metadata)
[project]
name = "shop"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = [
"requests>=2.32,<3",
]import importlib.metadata as md
print(md.version("pip"))
print(md.requires("pip")[:2])Common Gotchas
`pip` ≠ this `python`
`pip install` can write into a different interpreter than `python` runs. Always `python -m pip`.
Unpinned `pip install pkg` is not reproducible
today's latest is not last month's. Lock applications.
Mixing conda and pip without a plan
pip can overwrite conda-managed files. Prefer one installer per environment, or install pip packages only after conda and record both.
A broken wheel tag looks like a missing package
`pip` may try to build from source and fail on a compiler. Read the last error, do not retry the same command blindly.