Skip to content

pip

pip is the package installer for Python. It allows you to install and manage additional libraries and dependencies that are not included in the Python standard library.


Installation and Basic Usage

  • Install a Package:

    pip install requests
    
  • Uninstall a Package:

    pip uninstall requests
    
  • Check Installed Packages:

    pip list
    
  • Check for Package Updates:

    pip list --outdated
    

Managing Dependencies

  • Install from requirements.txt: Create a requirements.txt file with a list of packages and versions:

    requests==2.28.1
    numpy==1.24.2
    

    Install dependencies:

    pip install -r requirements.txt
    
  • Freeze Installed Packages: Generate a requirements.txt file from the current environment:

    pip freeze > requirements.txt
    
  • Upgrade a Package:

    pip install --upgrade requests
    

Environment and Compatibility

  • Create a Virtual Environment: Although pip itself doesn’t create virtual environments, it works seamlessly with venv or virtualenv.

    python -m venv myenv
    
  • Activate the Virtual Environment:

    • On Windows:

      myenv\Scripts\activate
      
    • On macOS/Linux:

      source myenv/bin/activate
      
  • Check pip Version:

    pip --version
    
  • Upgrade pip:

    pip install --upgrade pip
    

Scripting and Automation

  • Install Multiple Packages in a Single Command:

    pip install requests numpy pandas
    
  • Automate Package Installation: Use shell scripts or batch files to automate installation in multiple environments.


Other

In pip, the concept of "development" dependencies (i.e., packages needed only for development, not for production) is not directly supported in the same way as some other tools like Poetry or Pipenv. However, you can manage development dependencies in a few ways using pip:

  1. Using requirements.txt with Separate Files

    You can maintain separate requirements.txt files for different environments, such as one for production and one for development.

    Production requirements.txt:
    • List only the packages required for your application to run in production

      requests==2.28.1
      numpy==1.24.2
      
    Development requirements-dev.txt:
    • List the additional packages needed for development, such as testing and linting tools.

      pytest==7.4.0
      flake8==6.0.0
      

    To install production dependencies:

    pip install -r requirements.txt
    

    To install development dependencies:

    pip install -r requirements-dev.txt
    

    Alternatively, you can include development dependencies in a single file with markers or comments, and install them conditionally.