Installing Packages in Virtual Environments (VENV)

Python’s package management system allows you to install packages locally within a virtual environment (VENV) or globally in the system-wide Python installation. This guide will show you how to install and manage packages both locally and globally using pip.


Installing Packages Locally in a Virtual Environment

When you create a virtual environment, it is isolated from the global Python environment. This allows you to install project-specific dependencies without affecting the system-wide packages.

Steps for Installing Locally

  1. Create and Activate the Virtual Environment:

    • First, create a virtual environment if you haven’t already:
    python -m venv myenv
    
    • Activate the virtual environment:

    • Windows:

    myenv\Scripts\activate
    
    • macOS/Linux:
    source myenv/bin/activate
    
  2. Install Packages Locally: After activating the virtual environment, you can install packages using pip. These packages will be installed in the virtual environment and won't affect the global Python environment.

    pip install package_name
    

    Example: Install requests:

    pip install requests
    
  3. Check Installed Packages: To view installed packages in the current virtual environment, use:

    pip list
    
  4. Save Dependencies: To keep track of the installed packages, you can save them into a requirements.txt file:

    pip freeze > requirements.txt
    

    You can later use this file to recreate the environment on another system or share it with collaborators.


Installing Packages Globally

Global installation means the package is available to all Python projects and scripts, regardless of the virtual environment. This method should be used for system-wide tools and utilities.

Steps for Installing Globally

  1. Install Packages Globally: If you are not using a virtual environment (or deactivate it), you can install packages globally. Use sudo (on macOS/Linux) if you need administrative rights.

    pip install package_name
    

    Example: Install requests globally:

    pip install requests
    
    Info

    You may need to use sudo to install packages globally on some systems, especially on macOS/Linux, where administrative privileges are required for system-wide changes.

    sudo pip install requests
    
  2. Check Installed Global Packages: To see the packages installed globally (system-wide), you can use:

    pip list
    

    To check if a package is installed globally, you can use the pip show command:

    pip show requests
    
  3. Uninstalling Globally Installed Packages: If you need to uninstall a package globally, use:

    pip uninstall package_name
    

    Example:

    pip uninstall requests
    

Difference Between Local and Global Installation

FeatureLocal Installation (VENV)Global Installation
ScopeAvailable only inside the virtual environmentAvailable to all Python projects
IsolationDoes not affect the global Python environmentCan cause conflicts with system-wide packages
Use CaseProject-specific dependenciesTools that need to be accessed globally (e.g., awscli, flask)

Upgrading Packages in Virtual Environments

You can also upgrade installed packages inside a virtual environment to the latest version.

  1. Upgrade a Single Package:

    pip install --upgrade package_name
    

    Example:

    pip install --upgrade requests
    
  2. Upgrade All Installed Packages: Use pip-review (a third-party tool) to upgrade all installed packages:

    pip install pip-review
    pip-review --auto
    

Uninstalling Packages from VENV

To remove a package from your virtual environment, use:

pip uninstall package_name

This will uninstall the package from the current virtual environment but not affect the global Python installation.


Best Practices

  • Use Virtual Environments: Always use virtual environments for each Python project to avoid conflicts between packages.

  • Requirements File: Use a requirements.txt file to document your project’s dependencies. This helps with reproducibility and collaboration.

    Save installed packages with:

    pip freeze > requirements.txt
    

    Install packages from the requirements.txt file with:

    pip install -r requirements.txt
    
  • Global Tools: Use global installations for tools that should be available across all projects, like command-line tools (e.g., awscli, django).

Task

Practice: Installing and Managing Packages in VENV

Objective: Practice managing packages inside a virtual environment, including installation, saving dependencies, and uninstalling packages.

  1. Create and Activate a Virtual Environment:

    • Create a virtual environment named env and activate it.
    python -m venv env
    

    On Windows:

    env\Scripts\activate
    

    On macOS/Linux:

    source env/bin/activate
    
  2. Install Packages Locally:

    • Inside the virtual environment, install the requests and numpy packages.
    pip install requests numpy
    
  3. Save Dependencies:

    • Save the list of installed packages to a requirements.txt file.
    pip freeze > requirements.txt
    
  4. Recreate Environment:

    • Deactivate the virtual environment and create a new one.
    • Use pip install -r requirements.txt to install the dependencies from the requirements.txt file.
    deactivate
    python -m venv new_env
    source new_env/bin/activate  # On Windows: new_env\Scripts\activate
    pip install -r requirements.txt
    
  5. Uninstall a Package:

    • Uninstall requests from the virtual environment.
    pip uninstall requests
    
  6. Global Installation:

    • Install the requests package globally (outside the virtual environment).
    pip install requests
    

With pip, you have full control over how packages are installed in your Python environment. By using virtual environments for project dependencies and global installations for system-wide tools, you can maintain a clean and conflict-free environment for your development workflow.

Copyright © 2025 Devship. All rights reserved.

Made by imParth