pip Package Manager (Python)
pip - Package installer for Python.
Basic Commands
1# Install package
2pip install package-name
3
4# Install specific version
5pip install package-name==1.0.0
6
7# Install minimum version
8pip install 'package-name>=1.0.0'
9
10# Install version range
11pip install 'package-name>=1.0.0,<2.0.0'
12
13# Install from requirements file
14pip install -r requirements.txt
15
16# Uninstall package
17pip uninstall package-name
18
19# Uninstall without confirmation
20pip uninstall -y package-name
21
22# Upgrade package
23pip install --upgrade package-name
24pip install -U package-name
25
26# Upgrade pip itself
27python -m pip install --upgrade pip
List and Search
1# List installed packages
2pip list
3
4# List outdated packages
5pip list --outdated
6
7# Show package info
8pip show package-name
9
10# Show package files
11pip show --files package-name
12
13# Search packages (deprecated, use https://pypi.org)
14# pip search package-name # No longer works
Requirements Files
1# Generate requirements.txt
2pip freeze > requirements.txt
3
4# Generate with versions
5pip freeze --all > requirements.txt
6
7# Install from requirements
8pip install -r requirements.txt
9
10# Upgrade all packages in requirements
11pip install -r requirements.txt --upgrade
requirements.txt Format
1# requirements.txt
2# Exact version
3requests==2.31.0
4
5# Minimum version
6flask>=2.0.0
7
8# Version range
9django>=4.0.0,<5.0.0
10
11# From Git
12git+https://github.com/user/repo.git@v1.0.0
13
14# From Git branch
15git+https://github.com/user/repo.git@main
16
17# Editable install (development)
18-e .
19-e ./path/to/package
20
21# Include another requirements file
22-r base.txt
23
24# Comments
25# This is a comment
Virtual Environments
1# Create virtual environment
2python -m venv venv
3
4# Activate (Linux/macOS)
5source venv/bin/activate
6
7# Activate (Windows)
8venv\Scripts\activate
9
10# Deactivate
11deactivate
12
13# Install packages in venv
14pip install package-name
15
16# Generate requirements from venv
17pip freeze > requirements.txt
Install from Source
1# Install from local directory
2pip install /path/to/package
3
4# Install in editable mode (development)
5pip install -e /path/to/package
6pip install -e .
7
8# Install from Git
9pip install git+https://github.com/user/repo.git
10
11# Install from specific branch
12pip install git+https://github.com/user/repo.git@branch-name
13
14# Install from specific tag
15pip install git+https://github.com/user/repo.git@v1.0.0
16
17# Install from specific commit
18pip install git+https://github.com/user/repo.git@abc123
19
20# Install from tarball
21pip install https://example.com/package.tar.gz
22
23# Install from wheel
24pip install package-name.whl
Configuration
1# Show pip config
2pip config list
3
4# Set config value
5pip config set global.index-url https://pypi.org/simple
6
7# Get config value
8pip config get global.index-url
9
10# Edit config file
11pip config edit
12
13# Config file locations:
14# Global: /etc/pip.conf (Linux), %APPDATA%\pip\pip.ini (Windows)
15# User: ~/.config/pip/pip.conf (Linux), %APPDATA%\pip\pip.ini (Windows)
16# Virtual env: $VIRTUAL_ENV/pip.conf
pip.conf Example
1[global]
2index-url = https://pypi.org/simple
3trusted-host = pypi.org
4timeout = 60
5
6[install]
7no-cache-dir = true
Alternative Package Indexes
1# Use alternative index
2pip install --index-url https://test.pypi.org/simple/ package-name
3
4# Use extra index
5pip install --extra-index-url https://pypi.example.com/simple package-name
6
7# Trust host
8pip install --trusted-host pypi.example.com package-name
9
10# Use local index
11pip install --index-url file:///path/to/packages package-name
Cache Management
1# Show cache location
2pip cache dir
3
4# List cached packages
5pip cache list
6
7# Show cache info
8pip cache info
9
10# Remove cache
11pip cache purge
12
13# Remove specific package cache
14pip cache remove package-name
15
16# Install without cache
17pip install --no-cache-dir package-name
Dependency Resolution
1# Show dependency tree
2pip install pipdeptree
3pipdeptree
4
5# Show dependencies for package
6pipdeptree -p package-name
7
8# Show reverse dependencies
9pipdeptree -r -p package-name
10
11# Check for conflicts
12pip check
13
14# Install with no dependencies
15pip install --no-deps package-name
Wheel and Build
1# Install wheel
2pip install wheel
3
4# Build wheel
5pip wheel .
6
7# Build wheel for package
8pip wheel package-name
9
10# Install from wheel
11pip install package-name.whl
12
13# Download packages (no install)
14pip download package-name
15
16# Download with dependencies
17pip download -r requirements.txt -d ./packages
Create Python Package
Project Structure
1mypackage/
2βββ mypackage/
3β βββ __init__.py
4β βββ module1.py
5β βββ module2.py
6βββ tests/
7β βββ __init__.py
8β βββ test_module1.py
9βββ setup.py
10βββ setup.cfg
11βββ pyproject.toml
12βββ README.md
13βββ LICENSE
14βββ MANIFEST.in
setup.py
1from setuptools import setup, find_packages
2
3with open("README.md", "r", encoding="utf-8") as fh:
4 long_description = fh.read()
5
6setup(
7 name="mypackage",
8 version="0.1.0",
9 author="Your Name",
10 author_email="your.email@example.com",
11 description="A short description",
12 long_description=long_description,
13 long_description_content_type="text/markdown",
14 url="https://github.com/yourusername/mypackage",
15 project_urls={
16 "Bug Tracker": "https://github.com/yourusername/mypackage/issues",
17 "Documentation": "https://mypackage.readthedocs.io",
18 "Source Code": "https://github.com/yourusername/mypackage",
19 },
20 packages=find_packages(exclude=["tests", "tests.*"]),
21 classifiers=[
22 "Development Status :: 3 - Alpha",
23 "Intended Audience :: Developers",
24 "License :: OSI Approved :: MIT License",
25 "Programming Language :: Python :: 3",
26 "Programming Language :: Python :: 3.8",
27 "Programming Language :: Python :: 3.9",
28 "Programming Language :: Python :: 3.10",
29 "Programming Language :: Python :: 3.11",
30 ],
31 python_requires=">=3.8",
32 install_requires=[
33 "requests>=2.25.0",
34 "click>=8.0.0",
35 ],
36 extras_require={
37 "dev": [
38 "pytest>=7.0.0",
39 "black>=22.0.0",
40 "flake8>=4.0.0",
41 ],
42 "docs": [
43 "sphinx>=4.0.0",
44 "sphinx-rtd-theme>=1.0.0",
45 ],
46 },
47 entry_points={
48 "console_scripts": [
49 "mypackage=mypackage.cli:main",
50 ],
51 },
52 include_package_data=True,
53 zip_safe=False,
54)
pyproject.toml (Modern)
1[build-system]
2requires = ["setuptools>=45", "wheel", "setuptools_scm>=6.2"]
3build-backend = "setuptools.build_meta"
4
5[project]
6name = "mypackage"
7version = "0.1.0"
8description = "A short description"
9readme = "README.md"
10requires-python = ">=3.8"
11license = {text = "MIT"}
12authors = [
13 {name = "Your Name", email = "your.email@example.com"}
14]
15keywords = ["example", "package"]
16classifiers = [
17 "Development Status :: 3 - Alpha",
18 "Intended Audience :: Developers",
19 "License :: OSI Approved :: MIT License",
20 "Programming Language :: Python :: 3",
21]
22dependencies = [
23 "requests>=2.25.0",
24 "click>=8.0.0",
25]
26
27[project.optional-dependencies]
28dev = [
29 "pytest>=7.0.0",
30 "black>=22.0.0",
31 "flake8>=4.0.0",
32]
33docs = [
34 "sphinx>=4.0.0",
35 "sphinx-rtd-theme>=1.0.0",
36]
37
38[project.urls]
39Homepage = "https://github.com/yourusername/mypackage"
40Documentation = "https://mypackage.readthedocs.io"
41Repository = "https://github.com/yourusername/mypackage"
42"Bug Tracker" = "https://github.com/yourusername/mypackage/issues"
43
44[project.scripts]
45mypackage = "mypackage.cli:main"
46
47[tool.setuptools]
48packages = ["mypackage"]
49
50[tool.setuptools.package-data]
51mypackage = ["data/*.json"]
Build Package
1# Install build tools
2pip install build twine
3
4# Build package
5python -m build
6
7# This creates:
8# dist/mypackage-0.1.0.tar.gz (source distribution)
9# dist/mypackage-0.1.0-py3-none-any.whl (wheel)
10
11# Check package
12twine check dist/*
Publish to PyPI
1# Install twine
2pip install twine
3
4# Create PyPI account at https://pypi.org/account/register/
5
6# Create API token at https://pypi.org/manage/account/token/
7
8# Configure credentials
9# Create ~/.pypirc:
10cat > ~/.pypirc <<EOF
11[pypi]
12username = __token__
13password = pypi-YOUR-API-TOKEN-HERE
14EOF
15
16# Upload to Test PyPI (recommended first)
17twine upload --repository testpypi dist/*
18
19# Install from Test PyPI
20pip install --index-url https://test.pypi.org/simple/ mypackage
21
22# Upload to PyPI
23twine upload dist/*
24
25# Install from PyPI
26pip install mypackage
Private Package Repository
Using PyPI Server
1# Install pypiserver
2pip install pypiserver
3
4# Create packages directory
5mkdir ~/packages
6
7# Run server
8pypiserver run -p 8080 ~/packages
9
10# Upload package
11twine upload --repository-url http://localhost:8080 dist/*
12
13# Install from private server
14pip install --index-url http://localhost:8080/simple/ mypackage
Using Artifactory/Nexus
1# Configure pip to use private repository
2pip config set global.index-url https://artifactory.example.com/api/pypi/pypi/simple
3
4# Or use in command
5pip install --index-url https://artifactory.example.com/api/pypi/pypi/simple mypackage
6
7# Upload with twine
8twine upload --repository-url https://artifactory.example.com/api/pypi/pypi dist/*
Using Git Repository
1# Install directly from Git
2pip install git+https://github.com/user/private-repo.git
3
4# With authentication
5pip install git+https://username:token@github.com/user/private-repo.git
6
7# In requirements.txt
8git+https://github.com/user/private-repo.git@v1.0.0
Troubleshooting
1# Verbose output
2pip install -v package-name
3
4# Very verbose
5pip install -vv package-name
6
7# Debug output
8pip install -vvv package-name
9
10# Check for issues
11pip check
12
13# Fix broken installation
14pip install --force-reinstall package-name
15
16# Clear cache and reinstall
17pip cache purge
18pip install --no-cache-dir package-name
19
20# SSL issues
21pip install --trusted-host pypi.org --trusted-host files.pythonhosted.org package-name
Best Practices
- Always use virtual environments
- Pin versions in production (
==not>=) - Use requirements.txt for reproducibility
- Separate dev/prod dependencies
- Keep pip updated
- Use pip-tools for dependency management
- Check for vulnerabilities (use
pip-audit)
Related Snippets
- APT Package Manager (Debian/Ubuntu)
APT (Advanced Package Tool) for Debian and Ubuntu-based systems. Basic Commands β¦ - Bazel Build System
Bazel - Fast, scalable, multi-language build system from Google. Installation 1# β¦ - dpkg Package Manager
dpkg - Low-level package manager for Debian-based systems. Basic Commands 1# β¦ - emerge Package Manager (Gentoo)
emerge (Portage) - Source-based package manager for Gentoo Linux. Basic Commands β¦ - Go Modules & Workspaces
Go modules and workspaces for dependency management. Essential commands for Go β¦ - npm - Node Package Manager
Essential npm commands for Node.js package management. Quick reference for daily β¦ - pacman Package Manager (Arch Linux)
pacman - Package manager for Arch Linux and derivatives (Manjaro, EndeavourOS). β¦ - pkg Package Manager (FreeBSD)
pkg - Binary package manager for FreeBSD. Basic Commands 1# Update repository β¦ - Scoop Package Manager (Windows)
Scoop - Command-line installer for Windows. Installation 1# Set execution policy β¦ - Yarn Package Manager
Yarn package manager for JavaScript/Node.js projects. Installation 1# Via npm β¦ - yum/dnf Package Manager (RHEL/Fedora)
yum (Yellowdog Updater Modified) and dnf (Dandified Yum) for RHEL, Fedora, β¦