View Single Post
Old 2019-12-21, 20:18   #13
Dylan14's Avatar
Mar 2017

25216 Posts
Default Modules and packages

So we have mentioned that functions and classes are the backbone of modules, which are used to extend the abilities of Python. So what exactly are modules, and how can we add some to Python?
A module is an object which is a organizational unit of Python code. Each of these have a namespace which contains other objects (such as functions, variables and other objects). These can be used within a Python program, but they first must be imported first. Related to modules are packages, which is actually a special type of module that can contain submodules, or subpackages (*). To use these, one must first download the package and then import the necessary modules within the package.

Getting and installing modules/packages:

To install a module or package, one must first ensure that pip is installed in your Python distribution. This shouldn't be a problem as the main installer of Python (see post 3, How to Get/Use Python) includes this. If you are using a distribution of Python, it may include a package manager which one can use to install packages. For example, the Anaconda distribution of Python has a utility called Anaconda navigator, which provides a GUI for package installation/upgrading. If you are using a Linux distro version of Python, you may need to install pip. This can be done in Ubuntu and similar flavors as follows (entering your password if needed):

sudo apt-get install python3-pip
or on Arch and derivatives:

sudo pacman -S python-pip
Once you have pip installed, you can then go to the Python Package Index ( and search for packages. When you have found a package, you can install it by typing the following on a command line:

python -m pip install NameofPackage
or on Linux (**)(***)

pip install NameofPackage

This will yield the following, for example:

dylan@ubuntu:~/Desktop$ python3 -m pip install numpy
Collecting numpy
  Downloading (20.0MB)
    100% |████████████████████████████████| 20.0MB 58kB/s 
Installing collected packages: numpy
  The scripts f2py, f2py3 and f2py3.7 are installed in '/home/dylan/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed numpy-1.17.4
If you want to upgrade a package, you can do so using the following:
pip install --upgrade NameofPackage
and you can also remove packages using the --uninstall option. Further information on pip syntax can be found at

Importing and using a module

Once you have downloaded a package, we can then import it into our program, so that the objects that are in the package/module within the package can be called(*****). There are three ways to do this

1. import ModuleName
2. import ModuleName as ShortName
3. from ModuleName import "comma separated list of functions, or *"

1. This is the simplest way to import a module, as it will give you all the functions in one go. The downside is that you have to call the full name of the module every time you want to use something from it (i.e. ModuleName."chain of submodules, period separated".function(args))
2. With this, you can shorten the length of calls to functions. You have to preface a call to a function within a module with "ShortName.".
3. With this, you can select the functions that you specifically want, or you can use the asterisk to import everything in the module. The upside is that you don't need to preface a function call with anything before calling the function. The downside is that you may have conflicting function definitions. Think of this like the struct "using namespace std" in C/C++.
Note that the as function can also be used to rename functions when importing, provided you are doing one function at a time (i.e. from Module import Function as FunctionName, in generic terms).

An example:

Let's try to compute the matrix inverse of the following matrix:
\begin{bmatrix}<br />
1 & 2\\<br />
3 & 4<br />

To do this, we can use the inv function in numpy.linalg(6). We'll show how to call this in the 3 ways we did above.

1. Using import only:
import numpy
a = numpy.array([[1.,2.],[3.,4.]])
ainv = numpy.linalg.inv(a)
2. Using import with as:
import numpy as np
a = np.array([[1.,2.],[3.,4.]])
ainv = np.linalg.inv(a)
3. Using the from command (with the as command as well):
import numpy as np
from numpy.linalg import inv
a = np.array([[1.,2.],[3.,4.]])
ainv = inv(a)
All of these yield the same thing:
array([[-2. ,  1. ],
       [ 1.5, -0.5]])
or in fancy LaTex:
\begin{bmatrix}<br />
  -2 & 1\\<br />
  1.5 & -0.5<br />

(*) Actually, a package is any module that has the __path__ attribute.
(**) Some packages may be available in the distro repository. Try, for example,
sudo apt-get install python3-numpy
to install numpy in Ubuntu or Debian or
sudo pacman -S python-numpy
on Arch (provided it is in the main repos. If it is in the AUR, it's a bit more complicated, so I would advise reading the Arch Wiki on the method to build packages manually or using a AUR helper like Yay, trizen or pikaur, which is written in Python.
(***) In some other distros packages might already be included when you install. An example is ArcoLinux, which by default on a fresh install has numpy installed.
(****) When you execute this command, pip may install additional packages that the package you selected needs in order to run. These are known as dependencies.
(*****) If the module that you want to use is on the list provided here, you don't need to download it as Python includes these by default. If not, you will need to download the package first before you import, otherwise you will get a ModuleNotFoundError.
(6) We'll cover numpy in more detail later.

Last fiddled with by Dylan14 on 2020-12-23 at 03:04 Reason: add instructions for Arch users
Dylan14 is offline