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
is the standard package manager for Python, allowing you to install and manage Python packages from the Python Package Index (PyPI).
Ensure pip is installed and check its version:
pip --version
Install a specific package from PyPI:
pip install package_name
View all installed packages:
pip list
Update a package to the latest version:
pip install --upgrade package_name
Remove a package:
pip uninstall package_name
Search for packages (requires pip 10+):
pip search package_name
Install all dependencies listed in a requirements.txt
file:
pip install -r requirements.txt
Generate a requirements.txt
file with all installed packages:
pip freeze > requirements.txt
conda
is a package manager designed for Python and non-Python dependencies. It is particularly popular in data science and machine learning environments.
Conda is included with Anaconda and Miniconda. Install one of these distributions to use conda.
Verify the installation and version:
conda --version
Create isolated environments for different projects:
conda create --name env_name
Switch to a specific environment:
conda activate env_name
Install a package within an environment:
conda install package_name
View all packages in the active environment:
conda list
Upgrade a package:
conda update package_name
Remove a package:
conda remove package_name
Save the environment configuration:
conda env export > environment.yml
Recreate an environment from a file:
conda env create -f environment.yml
Packages installed globally are available to all projects but can cause version conflicts. To install globally:
pip install package_name --user
Use a virtual environment to avoid conflicts between projects.
python -m venv venv_name
On Windows:
venv_name\Scripts\activate
On macOS/Linux:
source venv_name/bin/activate
deactivate
poetry:
Simplifies dependency management and project structure.
Install poetry:
pip install poetry
pipenv:
Combines pip
and virtualenv
for better dependency management.
Install pipenv:
pip install pipenv
wheel:
Helps build and distribute Python packages in the .whl
format.
Install wheel:
pip install wheel
Pyenv:
Manages multiple Python versions.
Install Pyenv (Linux/macOS):
curl https://pyenv.run | bash
Objective: Practice managing Python packages and environments using popular tools like pip
, conda
, and pipenv
.
Install a Package:
requests
library using pip
, and verify the installation by importing it in a Python script.pip install requests
import requests
print(requests.__version__)
Create a Virtual Environment:
venv
module to create a virtual environment.numpy
and pandas
.python -m venv myenv
source myenv/bin/activate # On Windows: myenv\Scripts\activate
pip install numpy pandas
Use conda for Environment Management:
data_env
and install the matplotlib
package.conda create --name data_env matplotlib
conda activate data_env
python -c "import matplotlib; print(matplotlib.__version__)"
Freeze and Restore Dependencies:
pip freeze
to generate a requirements.txt
file, and restore it in another environment.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
Experiment with pipenv:
pipenv
and create a virtual environment with specific dependencies (flask
and sqlalchemy
).Pipfile.lock
and install dependencies from it.pip install pipenv
pipenv install flask sqlalchemy
pipenv lock
pipenv install --ignore-pipfile
Switch Python Versions with pyenv:
pyenv
and use it to manage different Python versions (e.g., 3.8 and 3.11).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
Bonus: Check for Outdated Packages:
pip list --outdated
to identify outdated packages and update one of them.pip list --outdated
pip install --upgrade <package_name>