Python Modules

okoth kongo
4 min readOct 22, 2018

The power of language lies in it’s libraries .This is what makes python which has simple syntax and being loosely typed, one of the most powerful languages on earth.

By the end of this article I will try to answer these questions about module:

1.what they are

2.why need them

3 how we get them

What are Modules

Modules are a collection of pre-written python scripts that allows you to solve various tasks.Related modules are ussually under one namespace in a package. Package, like real life libraries,houses modules which we can equate to books. So Generally am going to talk about this python book.

Types of modules in Python

Well there are three types of python modules. They are classified according to how a user(programmer) gets hold of them. These modules are:

1.standard

2.third party

3.user created

Standard Modules

These are inbuilt modules which come along with python upon installation for example os, sys, math, re. Check the docs at python docs on the same.

I will dwell on standard modules and show you the general syntax when using py modules. This is generally the same across board

general syntax

When you get a book from library, we use the word “borrow”, well in python this keyword is import .

import (module name).

import math

We are now in the module called math, if i want to solve a problem called square-root we simply turn to a book(function) called square root:

import math
math.sqrt(4)
2.0

You can access specific function of module using from module import function i.e. from math import sqrt. This allows us to access square root function directly from math the module

from math import sqrt
sqrt(4)
2.0

from module import * works tough it is frowned upon because the pythonic way states that explicit is better than simple(try import this)for more this also checkout pep for guidelines on how to be pythonic.

Asterisks are used call all the functions in a module at once but this generally slow the program down

from math import*

Using aliases while importing

Well you can nickname your module as you wish but this should not make the module lose its meaning , codes are read more than they are written.

Some module with long name encourage use of aliases e.g. matplotlib.pyplot is always plt even in the documentation

This is done using as keyword ,for instance import module as alias

e.g import math as podii. will be able to access math functions.

import math as podii
podii.sqrt(4)
2.0
dir(podii) == dir(math)
True

if you test the equality of podii and math it will return True

Incase you wondering what podii is,it consist of best minds I have ever met trying to make world a better place using technology just check them out

Third party modules

These are modules outside python inbuilt library written by good hearted people trying to make our life easy.

They are some times referred to as packages. Packaging just like in real context is putting things together. Generally putting modules together in a way that other users can install and use them. I will not go into details of packaging, that’s another day’s problem. Some examples of third party modules are numpy ,pylab,e.t.c.

They are not inbuilt so how do we get them? Well if you are working with python, you should have virtual environment installed. If you haven’t, lets do it. Assuming that all of you have python3 but its also possible in python2 just remove 3 whenever you see it e.g pip3,python3 e.t.c and replace it with 2

To manage software packages for Python, let’s install pip for more what is pip and what it does

sudo apt-get install -y python3-pip

Installing virtual environment

Virtual environments enable you to have an isolated space on your computer for Python projects, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects.
1.We need to first install the venv module, part of the standard Python 3 library, so that we can create virtual environments. Let’s install venv by copying this into the terminal:

sudo apt-get install -y python3-venv

2.Create a directory where your will be doing your python project

mkdir python_environment

(choose whatever name you wish).
3.Change working directory to python_environment i.e

cd python_environment

4. Once you are in the directory where you would like the environments to live, you can create an environment by running the following command:

python3 -m venv my_env

Note that my_env is a name of choosing and you can use that which you dim fit

Try if it is correctly setup by typing:

ls my_env

bin include lib lib64 pyvenv.cfg share

That’s the output

5. Activate virtual environment by:

source my_env/bin/activate

You can always deactivate by simple command by using

deactivate

Now that you have everything setup lets install some packages

To install packages(the third party library) use command:

pip3 install package_name

For instance

pip3 install numpy

kindly note all the installations are to be via linux terminal

--

--