Python Package Managers

Python package managers help you install, update, and manage third-party libraries and tools efficiently. The most popular package managers include pip and conda, each with unique features and use cases.


pip: Python’s Default Package Manager

What is pip?

pip is the standard package manager for Python, allowing you to install and manage Python packages from the Python Package Index (PyPI).

Common pip Commands

1. Check pip Version

Ensure pip is installed and check its version:

pip --version

2. Install a Package

Install a specific package from PyPI:

pip install package_name

3. List Installed Packages

View all installed packages:

pip list

4. Upgrade a Package

Update a package to the latest version:

pip install --upgrade package_name

5. Uninstall a Package

Remove a package:

pip uninstall package_name

6. Search for a Package

Search for packages (requires pip 10+):

pip search package_name

7. Install from Requirements File

Install all dependencies listed in a requirements.txt file:

pip install -r requirements.txt

8. Freeze Installed Packages

Generate a requirements.txt file with all installed packages:

pip freeze > requirements.txt

conda: A Versatile Package Manager

What is conda?

conda is a package manager designed for Python and non-Python dependencies. It is particularly popular in data science and machine learning environments.

Installing conda

Conda is included with Anaconda and Miniconda. Install one of these distributions to use conda.

Common conda Commands

1. Check conda Version

Verify the installation and version:

conda --version

2. Create a New Environment

Create isolated environments for different projects:

conda create --name env_name

3. Activate an Environment

Switch to a specific environment:

conda activate env_name

4. Install a Package

Install a package within an environment:

conda install package_name

5. List Installed Packages

View all packages in the active environment:

conda list

6. Update a Package

Upgrade a package:

conda update package_name

7. Uninstall a Package

Remove a package:

conda remove package_name

8. Export Environment

Save the environment configuration:

conda env export > environment.yml

9. Restore Environment

Recreate an environment from a file:

conda env create -f environment.yml

Global vs Local Installations

Global Installations

Packages installed globally are available to all projects but can cause version conflicts. To install globally:

pip install package_name --user

Local (Virtual Environment) Installations

Use a virtual environment to avoid conflicts between projects.

Create a Virtual Environment

python -m venv venv_name

Activate the Environment

  • On Windows:

    venv_name\Scripts\activate
    
  • On macOS/Linux:

    source venv_name/bin/activate
    

Deactivate the Environment

deactivate

Other Package Management Tools

  1. poetry:

    • Simplifies dependency management and project structure.

    • Install poetry:

      pip install poetry
      
  2. pipenv:

    • Combines pip and virtualenv for better dependency management.

    • Install pipenv:

      pip install pipenv
      
  3. wheel:

    • Helps build and distribute Python packages in the .whl format.

    • Install wheel:

      pip install wheel
      
  4. Pyenv:

    • Manages multiple Python versions.

    • Install Pyenv (Linux/macOS):

      curl https://pyenv.run | bash
      
Task

Practice: Python Package Management

Objective: Practice managing Python packages and environments using popular tools like pip, conda, and pipenv.

  1. Install a Package:

    • Install the requests library using pip, and verify the installation by importing it in a Python script.
    pip install requests
    
    import requests
    print(requests.__version__)
    
  2. Create a Virtual Environment:

    • Use the venv module to create a virtual environment.
    • Activate the virtual environment and install numpy and pandas.
    python -m venv myenv
    source myenv/bin/activate  # On Windows: myenv\Scripts\activate
    pip install numpy pandas
    
  3. Use conda for Environment Management:

    • Install Miniconda or Anaconda.
    • Create a conda environment named data_env and install the matplotlib package.
    conda create --name data_env matplotlib
    conda activate data_env
    python -c "import matplotlib; print(matplotlib.__version__)"
    
  4. Freeze and Restore Dependencies:

    • Use pip freeze to generate a requirements.txt file, and restore it in another environment.
    • Alternatively, use conda env export to generate an environment file and recreate the environment.
    pip freeze > requirements.txt
    pip install -r requirements.txt
    
    # Conda approach:
    conda env export > environment.yml
    conda env create -f environment.yml
    
  5. Experiment with pipenv:

    • Install pipenv and create a virtual environment with specific dependencies (flask and sqlalchemy).
    • Generate a Pipfile.lock and install dependencies from it.
    pip install pipenv
    pipenv install flask sqlalchemy
    pipenv lock
    pipenv install --ignore-pipfile
    
  6. Switch Python Versions with pyenv:

    • Install pyenv and use it to manage different Python versions (e.g., 3.8 and 3.11).
    • Set a specific version globally and per project.
    pyenv install 3.8.10
    pyenv install 3.11.0
    pyenv global 3.11.0
    pyenv local 3.8.10  # Use this version for the current directory
    python --version
    
  7. Bonus: Check for Outdated Packages:

    • Use pip list --outdated to identify outdated packages and update one of them.
    pip list --outdated
    pip install --upgrade <package_name>
    

Copyright © 2025 Devship. All rights reserved.

Made by imParth