Python packages control using PIP: installation, updating and removal of libraries.

онлайн тренажер по питону
Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

A self-study guide for Python 3 compiled from the materials on this site. Primarily intended for those who want to learn the Python programming language from scratch.

What is PIP and why is it needed

PIP (Pip Installs Packages) is a standard package manager for the Python programming language that has revolutionized the way we work with external libraries. Developed in 2008 as an improved alternative to the easy_install tool, PIP has become an integral part of the Python ecosystem.

History and development of PIP

PIP was created by Ian Bicking and has gone a long way since then. Starting with Python 3.4, PIP is included in the standard delivery of the language, which makes it available to all developers without additional installations.

The main advantages of using PIP

1. Simplify dependency management

  • Automatic resolution of version conflicts
  • Installing package dependencies in the correct order
  • Tracking installed libraries

2. Access to PyPI (Python Package Index)

  • More than 400,000 packages in the official repository
  • The ability to publish your own packages
  • Automatic download and installation

3. Cross-platform

  • Unified interface for Windows, macOS and Linux
  • Automatic system architecture detection
  • Support for various package formats

4. Integration with development tools

  • Compatibility with IDE and code editors
  • CI/CD pipeline support
  • Integration with version control systems

PIP installation

Checking for PIP

Before installing PIP, check if it is already installed:

# For Python 3
pip3 --version

# For Python 2 (deprecated)
pip --version

# Alternative way
python -m pip --version

Installation on Windows

Method 1: Automatic installation with Python

  1. Download Python from official website
  2. During installation, make sure that the "Add Python to PATH" option is selected
  3. PIP will be installed automatically

Method 2: Manual installation

# Download get-pip.py
curl https://bootstrap.pypa.io/get-pip.py - o get-pip.py

# Perform
the python installation
get-pip.py 

Method 3: Via the Microsoft Store

  • Install Python from the Microsoft Store
  • PIP will be enabled automatically

Installation on macOS

Method 1: Via Homebrew

# Install Homebrew if it is not available
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

# Install Python and PIP
brew install python

Method 2: Through the official installer

  1. Download Python from python.org
  2. Install by following the instructions
  3. PIP will be installed automatically

Installation on Linux

Ubuntu/Debian:

#
Update sudo apt update packages

# Install PIP for Python 3
sudo apt install python3-pip

# For older versions of Python 2
sudo apt install python-pip

CentOS/RHEL/Fedora:

# For new versions of Fedora
sudo dnf install python3-pip

# For CentOS/RHEL
sudo yum install python3-pip

Arch Linux:

sudo pacman -S python-pip

Basic PIP commands

Package Installation

Basic installation:

pip install package_name

Installing a specific version:

# Exact version
of pip install package_name==1.2.3

# Minimum version
of pip install package_name>=1.2.0

#
pip install version range "package_name>=1.2.0,<2.0.0"

Installation from different sources:

# From the Git repository
pip install git+https://github.com/user/repo.git

# From the local
pip install file /path/to/package.tar.gz

# From
the pip install URL https://example.com/package.tar.gz

# From GitHub with a specific branch
pip install git+https://github.com/user/repo.git@branch_name

Updating packages

# Upgrade of one package
pip install --upgrade package_name

# Update PIP
pip install --upgrade pip

# Updating all packages (requires additional tools)
pip list --outdated --format=freeze | grep -v '^\-e' | cut -d = -f 1 | xargs -n1 pip install -U

Removing packages

# Removing one package
pip uninstall package_name

# Deletion with confirmation
pip uninstall package_name -y

# Removing all packages from requirements.txt
pip uninstall -r requirements.txt -y

Viewing information about packages

# List of all installed
pip list packages

# Outdated
pip list packages --outdated

# Information about a specific
pip package show package_name

# Detailed information with
pip show dependencies -v package_name

Package Search

# PyPI search (may be disabled)
pip search search_term

# Alternative browser search
# https://pypi.org/search/?q=search_term

Working with a file requirements.txt

Creating a file requirements.txt

# Create a file with all installed
pip freeze packages requirements.txt

# Create a file only with top-level packages
pip-tools compile requirements.in

File structure requirements.txt

# Main dependencies
requests==2.28.1
flask>=2.0.0
numpy>=1.21.0,<2.0.0

# Dependencies for development
pytest>=7.0.0
black==22.3.0
flake8>=4.0.0

# Dependencies from Git
git+https://github.com/user/repo.git@v1.0.0

# Local dependencies
-e./local_package

# Additional requirements files
-r requirements-dev.txt

Installation from requirements.txt

# Install all
pip install -r packages requirements.txt

# Installation with
pip install -r update requirements.txt --upgrade

# Installing only the missing
pip install -r packages requirements.txt --user

Virtual environments and PIP

Creating a virtual environment

Using venv (recommended):

# Creating
a python environment -m venv myproject_env

# Activation on Windows
myproject_env\Scripts\activate

# Activation on macOS/Linux
source myproject_env/bin/activate

# Deactivate
deactivate

Using virtualenv:

# Installing virtualenv
pip install virtualenv

# Creating an environment
virtualenv myproject_env

# Activation is similar to venv

Working with packages in a virtual environment

# Checking the current environment
which python
which pip

# Installing packages in an active environment
pip install package_name

# Creation requirements.txt for
the pip freeze environment requirements.txt

# Creating an exact copy
of the pip freeze environment requirements.txt
pip install -r requirements.txt

Managing multiple environments

# Creating environments for different projects
python -m venv project1_env
python -m venv project2_env

# Quickly switch between environments
alias activate_project1="source project1_env/bin/activate"
alias activate_project2="source project2_env/bin/activate"

Advanced PIP features

PIP configuration

Configuration file pip.conf (Linux/macOS) or pip.ini (Windows):

[global]
index-url = https://pypi.org/simple/
trusted-host = pypi.org
timeout = 60
retries = 5

[install]
user = true
upgrade = true

Location of configuration files:

  • Linux/macOS: ~/.pip/pip.conf or ~/.config/pip/pip.conf
  • Windows: %APPDATA%\pip\pip.ini

Packet caching

# View information about
pip cache info

# Clearing
the pip cache purge

# Removing a specific package from
the pip cache cache remove package_name

Using package indexes

# Using the alternative
pip install -i index https://test.pypi.org/simple / package_name

# Using an additional
pip install --extra-index-url index https://private.pypi.org/simple / package_name

# Trusted host
pip install --trusted-host private.pypi.org package_name

Creating wheels

# Creating a wheel file
pip wheel package_name

# Install from
the pip wheel file install package_name.whl

# Creating a wheel for all
pip wheel -r dependencies requirements.txt

Problem solving and error correction

Common mistakes and their solutions

1. Access rights error

# Problem: Permission denied
# Solution: Using --user
pip install --user package_name

# Or installing
python -m venv myenv in a virtual environment
source myenv/bin/activate
pip install package_name

2. SSL certificates

# Problem: SSL certificate verify failed
# Solution: Updating
pip install --trusted-host certificates pypi.org --trusted-host pypi.python.org package_name

# Or pip update
python -m pip install --upgrade pip

3. Dependency conflicts

# Problem: Conflicting dependencies
# Solution: Using pip-tools
pip install pip-tools
pip-compile requirements.in
pip-sync requirements.txt

4. Compilation problems

# Problem: Failed building wheel
# Solution: Install ready-made wheel files
pip install --only-binary=all package_name

# Or installing system dependencies
# Ubuntu/Debian
sudo apt-get install python3-dev build-essential

# macOS
xcode-select --install

Troubleshooting

# Detailed error output
pip install -v package_name

# Debugging information
pip install --debug package_name

# Checking
pip check dependencies

# Information about
the pip debug system

Best practices and recommendations

Dependency Management

1. Use virtual environments

# Create a separate environment for each
python project -m venv project_env
source project_env/bin/activate

2. Pin package versions

# requirements.txt
requests==2.28.1 # Exact version
flask>=2.0.0,<3.0.0 # Version Range

3. Separate dependencies

# requirements.txt - main dependencies
of requests==2.28.1
flask==2.2.2

# requirements-dev.txt - development dependencies
pytest>=7.0.0
black==22.3.0
flake8>=4.0.0

Security

1. Update packages regularly

# Checking outdated packages
pip list --outdated

# Upgrade with caution
pip install --upgrade package_name

2. Use pip-audit to check for vulnerabilities

pip install pip-audit
pip-audit

3. Check the package sources

# Information about
the pip package show package_name

# Signature verification (if available)
pip install --verify package_name

Performance optimization

1. Use caching

# Configuring
the pip cache config set global.cache-dir /path/to/cache

2. Parallel installation

# Multi-thread installation
pip install --upgrade --force-reinstall --no-deps package_name

3. Optimization requirements.txt

# Use pip-tools to optimize
pip install pip-tools
pip-compile --upgrade requirements.in

Automation

1. Scripts for managing environments

#!/bin/bash
# setup_env.sh
python -m venv myenv
source myenv/bin/activate
pip install --upgrade pip
pip install -r requirements.txt

2. Makefile for Python projects

.PHONY: install update clean

install:
	pip install -r requirements.txt

update:
	pip install --upgrade -r requirements.txt

clean:
	pip uninstall -r requirements.txt -y

3. Using pre-commit hooks

# .pre-commit-config.yaml
repos:
  - repo: https://github.com/psf/black
    rev: 22.3.0
    hooks:
      - id: black


categories

  • Introduction to Python
  • Python Programming Basics
  • Control Structures
  • Data Structures
  • Functions and Modules
  • Exception Handling
  • Working with Files and Streams
  • File System
  • Object-Oriented Programming (OOP)
  • Regular Expressions
  • Additional Topics
  • General Python Base