Introduction
Python packages extend the functionality of Python by providing specialized tools and libraries for various tasks. Installing and managing these packages efficiently is an essential skill for Python developers.
In this lab, you will learn how to use pip, the standard Python package installer, on a Linux system. You will set up your environment, install packages, manage dependencies, and explore different installation options. These skills are fundamental for any Python developer working in Linux environments.
Setting up Your Python Environment
In this step, you'll prepare a clean Python environment for package management. You'll learn to use virtual environments, which are essential for managing project dependencies without conflicts.
Verifying Python and pip
First, let's verify that Python and pip are available:
python3 --version
pip3 --version
You should see Python 3.10.x and pip version information. If pip is not installed, install it:
sudo apt update
sudo apt install python3.10-venv -y
sudo apt upgrade python3-pip -y
Creating a Virtual Environment
Virtual environments create isolated Python spaces for each project. This prevents package conflicts between different projects:
cd ~/project
python3 -m venv myproject_env
source myproject_env/bin/activate
Your prompt should now show (myproject_env) indicating you're in the virtual environment. Inside a virtual environment, you can use pip instead of pip3.
Creating a Requirements File
Create a requirements.txt file to track your project's dependencies:
touch requirements.txt
This file will help you recreate the same environment on different systems.
Installing Python Packages
In this step, you'll learn to install Python packages both individually and using a requirements file. Ensure you're in your virtual environment (you should see (myproject_env) in your prompt).
Installing Your First Package
Let's install the requests package, which is commonly used for making HTTP requests:
pip install requests
You'll see the download and installation progress. Verify the installation:
pip list
You should see requests in the list of installed packages.
Using a Requirements File
Now let's specify multiple packages with exact versions in your requirements file. Open it:
nano requirements.txt
Add the following content:
requests==2.31.0
numpy==1.24.3
pandas==2.0.3
Save with Ctrl+O, Enter, then exit with Ctrl+X.
This approach ensures consistent environments across different systems by specifying exact package versions.
Installing from Requirements File
Install all packages from your requirements file:
pip install -r requirements.txt
Since requests is already installed, pip will either keep it or update it to match the specified version. The new packages (numpy and pandas) will be installed fresh.
Verify all packages are installed:
pip list | grep -E "(requests|numpy|pandas)"
You should see all three packages with their specified versions.
Managing Package Versions
In this step, you'll learn essential package management operations: checking package information, updating packages, and installing specific versions.
Viewing Package Information
Check detailed information about an installed package:
pip show requests
This displays version, dependencies, license, and installation location information.
Installing Specific Versions
Sometimes you need a different version for compatibility. Install a specific version of numpy:
pip install numpy==1.23.5
Verify the version change:
pip show numpy | grep Version
You should see version 1.23.5 instead of 1.24.3.
Updating Packages
Update a package to its latest version:
pip install --upgrade pandas
This updates pandas to the newest available version.
Freezing Your Environment
Generate a complete list of your current packages and versions:
pip freeze > current_env.txt
View the generated file:
cat current_env.txt
This file can be used to recreate the exact same environment elsewhere using pip install -r current_env.txt.
Working with Virtual Environments and Dependencies
In this final step, you'll learn virtual environment best practices and how to manage project dependencies effectively.
Deactivating and Reactivating
Practice deactivating your virtual environment:
deactivate
Your prompt should return to normal (no (myproject_env)). Try to run pip list:
pip list
You'll see only system-installed packages. Now reactivate your environment:
source ~/project/myproject_env/bin/activate
Notice how your project packages return when you reactivate.
Understanding Dependencies
Check which packages depend on others:
pip show pandas
Look at the "Requires" line to see pandas' dependencies. This is why managing environments is crucial - installing one package often installs many others.
Cleaning Up and Best Practices
Create a clean requirements file with only your direct dependencies:
nano ~/project/requirements.txt
Replace the content with just the packages you explicitly need (not their dependencies):
requests==2.31.0
numpy==1.23.5
pandas==2.0.3
Save and exit. When someone else installs from this file, pip will automatically handle the dependencies.
Sharing Your Environment
Your environment is now ready to be shared. Anyone can recreate it using:
pip install -r requirements.txt
This is the foundation of reproducible Python development.
Summary
In this lab, you've mastered the fundamentals of Python package management on Linux:
- Environment Setup: Created and managed Python virtual environments for project isolation
- Package Installation: Installed packages individually and using requirements files with specific versions
- Version Management: Updated packages, installed specific versions, and generated environment snapshots
- Best Practices: Learned to maintain clean dependency lists and share reproducible environments
These skills are essential for any Python developer. Virtual environments prevent conflicts between projects, while proper dependency management ensures your code works consistently across different systems.
You're now ready to manage Python packages confidently in any Linux development environment!



