The PyPA recommended tool for installing Python packages.
91
Comprehensive package management operations including uninstallation, package listing, information queries, and dependency analysis.
Remove packages and their dependencies with confirmation and batch operations.
# Uninstall single package
pip uninstall package_name
# Uninstall multiple packages
pip uninstall package1 package2 package3
# Uninstall without confirmation
pip uninstall -y package_name
# Uninstall from requirements file
pip uninstall -r requirements.txt
# Uninstall from requirements file without confirmation
pip uninstall -y -r requirements.txtList installed packages with various filtering and formatting options.
# List all installed packages
pip list
# List outdated packages
pip list --outdated
# List up-to-date packages
pip list --uptodate
# List editable packages
pip list --editable
# List local packages (not from PyPI)
pip list --local
# List user-installed packages
pip list --user
# Format output
pip list --format=columns # Default format
pip list --format=freeze # Requirements format
pip list --format=json # JSON format
# Exclude specific packages
pip list --exclude package_name
# Include pre-release versions
pip list --preOutput formats:
# Columns format (default)
Package Version
---------- -------
requests 2.28.1
urllib3 1.26.12
# Freeze format
requests==2.28.1
urllib3==1.26.12
# JSON format
[{"name": "requests", "version": "2.28.1"}, {"name": "urllib3", "version": "1.26.12"}]Display detailed information about installed packages.
# Show package information
pip show package_name
# Show multiple packages
pip show package1 package2
# Show package files
pip show --files package_name
# Verbose output
pip show --verbose package_namePackage information includes:
Verify package dependencies and identify conflicts.
# Check for dependency conflicts
pip check
# Check specific packages
pip check package_nameCheck output shows:
Inspect the Python environment and pip configuration.
# Inspect environment
pip inspect
# Show detailed environment information
pip debugEnvironment inspection provides:
Note: pip search was disabled due to PyPI limitations. Use alternative methods.
# Search PyPI (deprecated - returns error)
pip search search_termAlternative search methods:
# Use PyPI website
# Visit https://pypi.org and search
# Use pip index (limited functionality)
pip index versions package_nameGenerate requirements files from installed packages.
# Generate requirements for all packages
pip freeze
# Save to requirements file
pip freeze > requirements.txt
# Exclude specific packages
pip freeze --exclude package_name
# Local packages only
pip freeze --local
# User packages only
pip freeze --user
# Include all packages (including pip itself)
pip freeze --allFreeze output format:
certifi==2022.9.24
charset-normalizer==2.1.1
idna==3.4
requests==2.28.1
urllib3==1.26.12Use subprocess for programmatic package management operations.
import subprocess
import sys
import json
def list_packages(format_type='json'):
"""List installed packages."""
cmd = [sys.executable, '-m', 'pip', 'list', f'--format={format_type}']
try:
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
if format_type == 'json':
return json.loads(result.stdout)
return result.stdout
except subprocess.CalledProcessError as e:
print(f"Failed to list packages: {e}")
return None
def get_package_info(package_name):
"""Get information about a specific package."""
cmd = [sys.executable, '-m', 'pip', 'show', package_name]
try:
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
return result.stdout
except subprocess.CalledProcessError as e:
print(f"Package {package_name} not found: {e}")
return None
def uninstall_package(package_name, confirm=False):
"""Uninstall a package."""
cmd = [sys.executable, '-m', 'pip', 'uninstall', package_name]
if not confirm:
cmd.append('-y')
try:
subprocess.check_call(cmd)
print(f"Successfully uninstalled {package_name}")
except subprocess.CalledProcessError as e:
print(f"Failed to uninstall {package_name}: {e}")
raise
def check_dependencies():
"""Check for dependency conflicts."""
cmd = [sys.executable, '-m', 'pip', 'check']
try:
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
print("No dependency conflicts found")
return True
except subprocess.CalledProcessError as e:
print(f"Dependency conflicts found:\n{e.stdout}")
return False
def freeze_requirements(output_file=None):
"""Generate requirements from installed packages."""
cmd = [sys.executable, '-m', 'pip', 'freeze']
try:
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
requirements = result.stdout
if output_file:
with open(output_file, 'w') as f:
f.write(requirements)
print(f"Requirements saved to {output_file}")
return requirements
except subprocess.CalledProcessError as e:
print(f"Failed to freeze requirements: {e}")
return None
# Usage examples
packages = list_packages()
print(f"Found {len(packages)} installed packages")
info = get_package_info('requests')
if info:
print(info)
check_dependencies()
freeze_requirements('requirements.txt')Perform operations on multiple packages efficiently.
import subprocess
import sys
def batch_uninstall(package_list, confirm=False):
"""Uninstall multiple packages."""
cmd = [sys.executable, '-m', 'pip', 'uninstall'] + package_list
if not confirm:
cmd.append('-y')
try:
subprocess.check_call(cmd)
print(f"Successfully uninstalled: {', '.join(package_list)}")
except subprocess.CalledProcessError as e:
print(f"Failed to uninstall packages: {e}")
raise
def get_outdated_packages():
"""Get list of outdated packages."""
cmd = [sys.executable, '-m', 'pip', 'list', '--outdated', '--format=json']
try:
result = subprocess.run(cmd, capture_output=True, text=True, check=True)
return json.loads(result.stdout)
except subprocess.CalledProcessError as e:
print(f"Failed to get outdated packages: {e}")
return []
def upgrade_all_outdated():
"""Upgrade all outdated packages."""
outdated = get_outdated_packages()
for package in outdated:
package_name = package['name']
cmd = [sys.executable, '-m', 'pip', 'install', '--upgrade', package_name]
try:
subprocess.check_call(cmd)
print(f"Upgraded {package_name}")
except subprocess.CalledProcessError as e:
print(f"Failed to upgrade {package_name}: {e}")
# Usage
outdated = get_outdated_packages()
print(f"Found {len(outdated)} outdated packages")
# Upgrade all (use with caution)
# upgrade_all_outdated()Install with Tessl CLI
npx tessl i tessl/pypi-pipevals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10