Installing Python modules

Reading time5 min

In brief

Article summary

In this article, we go into more details on the Python modules manager, pip. This tool is central in Python to install external modules, which are collections of files and functions to serve your purposes.

Main takeaways

  • A module is a collection of functions and variables, generally with a thematic coherency.

  • In Python, pip allows to simply install modules.

Article contents

1 — Packages and modules

In Python, an external library is called a “module”. A module is a file containing code (functions, variables), which you can reuse in your own code.

The Python language comes with a plethora of already defined functions regrouped into thematical modules.

Important

Never reinvent the wheel and favor the “official” functions whose correctness and efficiency have been already attested.

For instance, you may wonder: isn’t there already a function in Python that allows to calculate the square root of a number? It turns out there is such a function, called sqrt, provided by the math module from the Python standard library.

We will now learn how to use Python modules provided by the Python community, which have to be installed before use. They allow to accelerate your development time, by relying on work already done by the community.

Before we move on, let us introduce the concept of a Python “package”. A package is simply a collection of modules, that can be installed as a whole. It also provides you with the ability to use dotted module names, i.e., sub-parts of a larger module.

In this lesson, we will learn how to install and use such Python packages.

2 — Installing Python packages

2.1 — A simple example

Suppose we have to multiply two matrices. As you can guess, this is a recurrent problem, hence there is a high probability that Python packages for doing this already exist. One such package for scientific computing is NumPy. Let us use NumPy to compute the result of a matrix product.

# Needed imports
import numpy as np

# Define matrices A and B
matrix_a = np.array([1,2,3])
matrix_b = np.array([1,1,-1])

# Compute the matrix product of A and B
product = np.matmul(matrix_a, matrix_b)

# Print the matrix product of A and B
print(product)

Notice that we use the following syntax to rename the package in our code:

import packageName as newName

This is usually done to shorten a long package name, but it can also be done to disambiguate between packages. In our case, we simply rename numpy to the shorter np.

Save the above script as matrix_multiplication.py, and execute it. If you never installed numpy before, you should get the following output:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'

This error message informs you that the numpy package is absent. In other words, NumPy is not installed, so we have to install it before using it.

Information

You could also have checked if it is installed using the following command:

python -m pip show numpy
pip show numpy
pip show numpy

If installed, you would get the package information as an output. Otherwise, this command will tell you that it is not installed.

2.2 — Using Pip to install packages

By default, many modules have been already installed: they constitute the Python Standard Library. However, depending on your domain (e.g., signal processing, data science), the standard modules may not provide some specific functionnalities. That’s why external modules or packages exist, to provide additional functionalities to the developer.

The easiest way to install a package in Python is by using a “package installer”. The Python package installer is called pip, a recursive acronym for “Pip Installs Packages”. Pip for Python3 is called pip3.

2.2.1 — Installing Pip

Normally, this tool has been installed along with the Python interpreter. You can check that by running the following command in a terminal:

python -m pip --version
pip --version
pip --version

If pip is not installed, check the Python official website to install it.

2.2.2 — Installing an external package with Pip

Now, you have the tool (pip) to install external packages, but where can you and pip find such packages?

The Python Package Index (PyPI) is a repository of software for the Python programming language. PyPI helps you find and install software developed and shared by the Python community. The pip installer uses the PyPI repository to install the packages you specify as described here.

Basically, it consists in running pip and specifying the package you want to install (here the matplotlib package).

python -m pip install matplotlib
pip install matplotlib
pip install matplotlib

Before installing a package, check the contents of the project description page in the PyPI repository as you may find relevant information about the package (functionalities) and about to get things done correctly (e.g., compatible versions of the Python interpreter, specific dependencies or constraints, additionnal external librairies to install to get extra functionnalities).

Information

In addition to external packages, Pip can also install packages from multiple places:

2.2.3 — Controlling the package version

Sometimes, for compatibility reasons, you may need to install a specific version of a package. Discover the different versions of a package available for download on PyPI using the following command (change <package_name> with the mudole name):

python -m pip index versions <package_name>
pip index versions <package_name>
pip index versions <package_name>

Then, to install a specific package version, you may use the following syntax (change <version_number> with the version number, e.g., 1.2.5):

python -m pip install <package_name>==<version_number>
pip install <package_name>==<version_number>
pip install <package_name>==<version_number>

To upgrade a package using Pip, simply use:

python -m pip install --upgrade <package_name>
pip install --upgrade <package_name>
pip install --upgrade <package_name>

If you have several versions of Python installed simultaneously (e.g., 3.7, 3.11) the command pip3 install <package_name> becomes ambiguous, as it is not clear for which version of Python the installation is being done. To disambiguate this case, we can call the pip of a specific python version using the same syntax as for Windows, even for Linux and MacOS. In that case, you just need to precise the Python executable you want to use (e.g., python3.7, python3.11).

2.3 — Back to the example

Going back to our matrix multiplication example, let’s install numpy:

python -m pip install numpy
pip install numpy
pip install numpy

Congratulations! You now know how to find, install, and import a Python package.

To go further

Important

The content of this section is optional. It contains additional material for you to consolidate your understanding of the current topic.

3 — Some useful packages to play with

Let’s install some packages that may be useful to you. Obviously, this list is not comprehensive at all. Feel free to browse the repository, find packages of interest and maybe improve them later through your contributions.

  • Matplotlib – Visualization with Python.

    Example

    Here is an example script using Matplotlib to create a simple plot:

    # Import the package
    # We rename the library matplotlib.pyplot to "plt" for shorter code in the script
    import matplotlib.pyplot as plt
    
    # Make a simple plot
    plt.plot([1, 2, 3, 4], [1, 4, 9, 16])
    plt.xlabel("$n$")
    plt.ylabel("$n^2$")
    plt.title("Square of some values")
    plt.show()
    // Import the necessary packages
    // We use the JFreeChart library for creating charts
    import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartPanel;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.data.category.DefaultCategoryDataset;
    import javax.swing.JFrame;
    
    
    /**
    * In Java, the entry point of a program is the 'main' function, in the 'Main' class, in a file named 'Main.java'.
    * To enable asserts, you should compile your program with `javac Main.java`.
    * Then, run it with the command `java -ea Main`.
    */
    public class Main
    {
    
       /**
       * This is the entry point of your program.
       * It contains the first codes that are going to be executed.
       *
       * @param args Command line arguments received.
       */
       public static void main (String[] args)
       {
           // Create a dataset
           DefaultCategoryDataset dataset = new DefaultCategoryDataset();
           dataset.addValue(1, "Values", "1");
           dataset.addValue(4, "Values", "2");
           dataset.addValue(9, "Values", "3");
           dataset.addValue(16, "Values", "4");
    
           // Create a chart
           JFreeChart lineChart = ChartFactory.createLineChart(
               "Square of some values", // Title
               "$n$", // X-axis Label
               "$n^2$", // Y-axis Label
               dataset, // Dataset
               PlotOrientation.VERTICAL,
               false, // Include legend
               true,
               false
           );
    
           // Display the chart in a window
           JFrame chartFrame = new JFrame("Line Chart Example");
           ChartPanel chartPanel = new ChartPanel(lineChart);
           chartFrame.setContentPane(chartPanel);
           chartFrame.setSize(800, 600);
           chartFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           chartFrame.setVisible(true);
       }
    
    }

    Running this script should open a new window containing the following plot:

  • SciPy – Fundamental algorithms for scientific computing in Python.

    Example

    Here is an example script using Scipy to compute such an expression:

    $​I(a,b)=\int_{0}^{1} ax^2+b , dx$

    # Import the needed elements from the package
    from scipy.integrate import quad
    
    # Define function f for which to compute the integral
    def f (x, a: float, b: float):
        return a * x**2 + b
    
    # Compute the integral for some values of a and b
    lower_bound = 0
    upper_bound = 1
    a = 2
    b = 1
    result = quad(f, lower_bound, upper_bound, args=(a, b))
    print(result)
    // Import the needed elements from the package
    import org.apache.commons.math3.analysis.integration.SimpsonIntegrator;
    import org.apache.commons.math3.analysis.integration.BaseAbstractUnivariateIntegrator;
    import org.apache.commons.math3.analysis.UnivariateFunction;
    
    
    /**
    * Define function f for which to compute the integral.
    * This should be done in a separate class
    */
    /**
    * Define function f for which to compute the integral.
    * This should be done in a separate class
    */
    public static class FunctionToIntegrate implements UnivariateFunction
    {
    
        /**
         * Arguments taken by the function.
         */
        private final double a;
        private final double b;
    
    
    
        /**
         * Constructor of the class.
         *
         * @param a: First argument of the function.
         * @param b: Second argument of the function.
         */
        public FunctionToIntegrate (double a, double b)
        {
            this.a = a;
            this.b = b;
        }
    
    
        /**
         * Function to integrate.
         *
         * @param x: Variable to use for integration.
         * @return   The integral of f.
         */
        public double value (double x)
        {
            return a * x * x + b;
        }
    
    }
    
    
    /**
    * In Java, the entry point of a program is the 'main' function, in the 'Main' class, in a file named 'Main.java'.
    * To enable asserts, you should compile your program with `javac Main.java`.
    * Then, run it with the command `java -ea Main`.
    */
    public class Main
    {
    
       /**
       * This is the entry point of your program.
       * It contains the first codes that are going to be executed.
       *
       * @param args Command line arguments received.
       */
       public static void main (String[] args)
       {
           // Compute the integral for some values of a and b
           double lowerBound = 0;
           double upperBound = 1;
           double a = 2;
           double b = 1;
    
           // Create an instance of the function
           UnivariateFunction function = new FunctionToIntegrate(a, b);
    
           // Use Simpson's rule for integration
           BaseAbstractUnivariateIntegrator integrator = new SimpsonIntegrator();
           double result = integrator.integrate(1000, function, lowerBound, upperBound);
    
           // Print the result
           System.out.println("Integral result: " + result);
       }
    
    }

    Running this script should produce the following output:

    (1.6666666666666667, 1.8503717077085944e-14)

To go beyond

Important

The content of this section is very optional. We suggest you directions to explore if you wish to go deeper in the current topic.