Linux Python Package Installing

LinuxLinuxBeginner
Practice Now

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.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/PackagesandSoftwaresGroup(["Packages and Softwares"]) linux(("Linux")) -.-> linux/VersionControlandTextEditorsGroup(["Version Control and Text Editors"]) linux(("Linux")) -.-> linux/BasicSystemCommandsGroup(["Basic System Commands"]) linux(("Linux")) -.-> linux/BasicFileOperationsGroup(["Basic File Operations"]) linux(("Linux")) -.-> linux/FileandDirectoryManagementGroup(["File and Directory Management"]) linux(("Linux")) -.-> linux/UserandGroupManagementGroup(["User and Group Management"]) linux/BasicSystemCommandsGroup -.-> linux/source("Script Executing") linux/BasicFileOperationsGroup -.-> linux/touch("File Creating/Updating") linux/FileandDirectoryManagementGroup -.-> linux/cd("Directory Changing") linux/UserandGroupManagementGroup -.-> linux/env("Environment Managing") linux/PackagesandSoftwaresGroup -.-> linux/apt("Package Handling") linux/PackagesandSoftwaresGroup -.-> linux/pip("Python Package Installing") linux/VersionControlandTextEditorsGroup -.-> linux/nano("Simple Text Editing") subgraph Lab Skills linux/source -.-> lab-271355{{"Linux Python Package Installing"}} linux/touch -.-> lab-271355{{"Linux Python Package Installing"}} linux/cd -.-> lab-271355{{"Linux Python Package Installing"}} linux/env -.-> lab-271355{{"Linux Python Package Installing"}} linux/apt -.-> lab-271355{{"Linux Python Package Installing"}} linux/pip -.-> lab-271355{{"Linux Python Package Installing"}} linux/nano -.-> lab-271355{{"Linux Python Package Installing"}} end

Setting up Your Python Environment

In this step, you will prepare your working environment for Python package management. First, let's understand what we need:

  • Python: The programming language itself
  • pip: The Python Package Installer, a tool for installing and managing Python packages

Checking Python Installation

Let's start by checking if Python is already installed on your system:

python3 --version

You should see output similar to:

Python 3.10.x

If Python is not installed, you would use the following command to install it (you won't need to run this as Python is already installed in the lab environment):

sudo apt update && sudo apt install python3

Installing pip

Now, let's install pip, which is the standard package manager for Python:

sudo apt install python3-pip

After installation completes, verify that pip is correctly installed by checking its version:

pip3 --version

You should see output showing the pip version, similar to:

pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)

Creating a Requirements File

Package management best practices include maintaining a list of required packages in a file called requirements.txt. This allows for consistent installations across different environments.

Create this file in your project directory:

cd ~/project
touch requirements.txt

Open the file with the nano text editor:

nano requirements.txt

For now, leave this file empty. We will add packages in the next step. To exit nano, press Ctrl+X, then press N since we don't need to save any changes yet.

Installing Python Packages with pip

In this step, you will learn how to install Python packages using pip. There are two main ways to install packages:

  1. Installing individual packages directly
  2. Installing multiple packages from a requirements file

Understanding Python Packages

Python packages are collections of modules that extend Python's functionality. Popular packages include:

  • requests: For making HTTP requests
  • numpy: For numerical computing
  • pandas: For data analysis
  • matplotlib: For data visualization

Installing Individual Packages

To install a single package, use the following command structure:

pip3 install package_name

Let's install the requests package, which is commonly used for making HTTP requests:

pip3 install requests

You should see output showing the download and installation progress, ending with a successful installation message.

Creating a Requirements File

Now, let's prepare a requirements file to specify multiple packages and their versions. Open the requirements.txt file created earlier:

nano ~/project/requirements.txt

Add the following lines to the file:

requests==2.25.1
numpy==1.20.1

Save the file by pressing Ctrl+O, then Enter, and exit by pressing Ctrl+X.

This file specifies that we want to install:

  • requests version 2.25.1
  • numpy version 1.20.1

Specifying versions ensures consistency across different environments.

Installing from a Requirements File

Now, install the packages specified in the requirements file:

pip3 install -r ~/project/requirements.txt

You should see the packages being downloaded and installed. Note that if requests is already installed but with a different version, pip will update or downgrade it to match the version in requirements.txt.

Verifying Installed Packages

After installation, verify that the packages were installed correctly:

pip3 list

This command displays all installed Python packages. Look for requests and numpy in the list, which should show the exact versions specified in your requirements file:

Package    Version
---------- -------
...
numpy      1.20.1
...
requests   2.25.1
...

Managing Python Package Versions

In this step, you'll learn how to update packages, install specific versions, and uninstall packages when needed.

Checking Package Information

Before updating or changing a package, it's useful to check its current information:

pip3 show requests

This will display details about the requests package, including:

  • Version
  • Summary
  • Author
  • License
  • Location on your system
  • Dependencies

Updating a Package

To update a package to its latest version, use the --upgrade flag:

pip3 install --upgrade requests

This will update the requests package to the latest available version. Note that this might override the version specified in your requirements.txt file.

Installing a Different Version

Sometimes you might need to install a specific version of a package, for compatibility reasons:

pip3 install numpy==1.19.5

This will downgrade numpy to version 1.19.5. You can verify the change with:

pip3 show numpy

The output should now show version 1.19.5 instead of 1.20.1.

Uninstalling Packages

To remove a package that you no longer need, use:

pip3 uninstall -y numpy

The -y flag automatically confirms the uninstallation without prompting. You can verify the package is gone:

pip3 list | grep numpy

This should return no results, indicating numpy is no longer installed.

Reinstalling Required Packages

Now let's reinstall the packages according to our requirements.txt:

pip3 install -r ~/project/requirements.txt

This will reinstall the packages with the versions specified in the file. Verify with:

pip3 list | grep numpy

You should see numpy version 1.20.1 has been reinstalled.

Working with Python Virtual Environments

Virtual environments are isolated Python environments that allow you to install packages without affecting the system-wide Python installation. This is particularly useful when working on multiple projects with different dependencies.

Understanding Virtual Environments

A virtual environment:

  • Creates an isolated space for Python projects
  • Allows each project to have its own dependencies
  • Prevents conflicts between project requirements

Creating a Virtual Environment

Python comes with the venv module for creating virtual environments. Let's create one:

python3 -m venv ~/project/myenv

This command creates a new virtual environment named myenv in your project directory.

Activating the Virtual Environment

To use the virtual environment, you need to activate it:

source ~/project/myenv/bin/activate

After activation, your command prompt should change to indicate you're now working within the virtual environment. It will look something like:

(myenv) labex@hostname:~/project$

Installing Packages in the Virtual Environment

Now, packages you install will be isolated to this environment. Let's install a new package:

pip install matplotlib

Note that inside the virtual environment, you can use pip instead of pip3.

Verify the installation:

pip list

You'll see matplotlib is installed, but only in this virtual environment.

Deactivating the Virtual Environment

When you're done working in the virtual environment, you can deactivate it:

deactivate

Your prompt will return to normal, indicating you're back in the system-wide Python environment.

Comparing Environments

Now that you're back in the global environment, check if matplotlib is installed:

pip3 list | grep matplotlib

You should see no results, confirming that packages installed in the virtual environment are isolated from the global environment.

Summary

In this lab, you have learned essential skills for managing Python packages on Linux systems:

  • Setting up your Python environment with pip
  • Installing packages individually and from a requirements.txt file
  • Managing package versions (updating, downgrading, uninstalling)
  • Working with virtual environments for isolated project dependencies

These skills form a fundamental part of Python development workflow. Proper package management ensures your projects have the correct dependencies, making your development process more reliable and reproducible.

As you continue your Python journey, you'll find that these package management skills are essential for all types of Python development, from web applications to data science and beyond.