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
.
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.
Create and Activate the Virtual Environment:
python -m venv myenv
Activate the virtual environment:
Windows:
myenv\Scripts\activate
source myenv/bin/activate
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
Check Installed Packages: To view installed packages in the current virtual environment, use:
pip list
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.
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.
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
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
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
Uninstalling Globally Installed Packages: If you need to uninstall a package globally, use:
pip uninstall package_name
Example:
pip uninstall requests
Feature | Local Installation (VENV) | Global Installation |
---|---|---|
Scope | Available only inside the virtual environment | Available to all Python projects |
Isolation | Does not affect the global Python environment | Can cause conflicts with system-wide packages |
Use Case | Project-specific dependencies | Tools that need to be accessed globally (e.g., awscli , flask ) |
You can also upgrade installed packages inside a virtual environment to the latest version.
Upgrade a Single Package:
pip install --upgrade package_name
Example:
pip install --upgrade requests
Upgrade All Installed Packages:
Use pip-review
(a third-party tool) to upgrade all installed packages:
pip install pip-review
pip-review --auto
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.
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
).
Objective: Practice managing packages inside a virtual environment, including installation, saving dependencies, and uninstalling packages.
Create and Activate a Virtual Environment:
env
and activate it.python -m venv env
On Windows:
env\Scripts\activate
On macOS/Linux:
source env/bin/activate
Install Packages Locally:
requests
and numpy
packages.pip install requests numpy
Save Dependencies:
requirements.txt
file.pip freeze > requirements.txt
Recreate Environment:
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
Uninstall a Package:
requests
from the virtual environment.pip uninstall requests
Global Installation:
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.