Working With Python Modules

in STEMGeeks25 days ago

Welcome back to our next article in the series we have put together introducing the Python programming language to users. Last week we discussed implementing functions as part of our Python program, and this week we are gong to take things a little further by discussing how we can move our Functions out of our single program and turn them into a Module.

In our previous article we started to organise our code in Functions. This went part the way in making our code a little more easy to read but as you might be thinking, as our programs increase in size, it's going to be a lot more difficult to read. This is where Modules can help.

In the simplist of terms, a Module is basically another Python program, that you are referencing and using in your current program. This means we can start to break up our program based on different functionality and features.

For example, in our sample code which we will be working on shortly, we could separate all the Functions into a different file, and then import this file as a Module into the rest of our program. This also means that this same file can be used by other programs or users if needed.

A Really Simple Module Example


As we've always seen, the best way to explain how to use Modules is by using a simply example. Below, we are going to create a basic Function that does a simple print statement.

Create a new Python program called simple_module.py:

touch simple_module.py

Open the file with your text editor and add in the following code

#!/usr/bin/env python

def simple_module_function():
  print("This is the code from the module")

simple_module_function()

Save the code and perform a test on the code to make sure all the Python code is correct. You should simply get the output of the print statement.

To demonstrate how we can now use this Module in our Python programs, we could create a new program, but instead it will be a little quicker to open the interactive shell and we will be able to test this way.

There are a few ways in which we can now use the Module in our Python code. The first way is to simply use the import statement and include the name of the Module. Remember not to include the .py when using importing the module like the example below:

import module_name

In the interactive shell, we can perform the following to first import the Module. We then use the dot(.) notation to use the Function in the code. For example, we use the <module_name>.<function_name> to use the Function, in our code "simple_module.simple_module_function()". This means we can have multiple function in the one Module and simple use its name to differentiate between them.

>>> import simple_module
>>> simple_module.simple_module_function()
This is the code from the module

In the code below, we have shown we can use "as" to rename the Module. This allows you to reduce the size of the code if you want to.

import module_name as renamed_module

Using our example Module, we can rename the Module to "sm", so we can then use the Function in the Module as "sm.simple_module_function()":

>>> import simple_module as sm
>>> sm.simple_module_function()
This is the code from the module

The next way to import a Module, is by simply importing the Functions you need from a Module using the "from" statement:

from module_name import function1, function2

In our example, we can use the "from" statement and simply import the one Function we have in our Module.

>>> from simple_module import simple_module_function
>>> simple_module_function()
This is the code from the module

Finally, the last way we can import Modules us by using an asterisk(*), as you can see below:

from module_name import *

We have done this again below, but as you can see, we no longer need to include the Module name when using the Function.

>>> from simple_module import *
>>> simple_module_function()
This is the code from the module

Although our Module is pretty small, if we had a large Module, using an asterisk(*) might actually slow our code down a little due to the number of Functions needing to be imported.

Python Built In Modules


Python comes with a lot of built in Modules which are installed as part of the Python installation, but are not loaded when Python is started up. They add a lot of functionality to your code, but the list of Modules is kinda large and something we may need to cover in another article.

The latest list of Built In Modules are here:
https://docs.python.org/3/py-modindex.html

If you access the interactive shell for Python, you can see all of the Modules that are included as part of your installation by running the help('modules') function. You should see a large list of modules provided with our output, limited for ease of reading:

>>> help('modules')

Please wait a moment while I gather a list of all available modules...

__future__          _xxsubinterpreters  heapq               select
_abc                abc                 hmac                selectors
_ast                aifc                html                setuptools

If you import a Module, you can also use the help() Function to see details and documentation on how to use the Module. In the example below, we have imported the os Module and then used the help() Function to provide details on how to use it:

>>> import os
>>> help('os')

Help on module os:

NAME
    os - OS routines for NT or Posix depending on what system we're on.

MODULE REFERENCE
    https://docs.python.org/3.8/library/os

    The following documentation is automatically generated from the Python
    source files.  It may be incomplete, incorrect or include features that

If you need to see all of the Functions available on the Module, you can use the dir() function as we have below:

>>> dir(os)
['DirEntry', 'F_OK', 'MutableMapping', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'PathLike', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'W_OK', 'X_OK', '_AddedDllDirectory', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_che

Updating Our Python Project


We've been working on our simply Python program in the past few articles, and we will also be able to make a similar change to remove the Functions into a Module. First create a new Python file called savings_function.py:

touch savings_function.py

Open the file with your text editor and add in the check_savings and add_savings Functions we had from our previous code:

  1 #!/usr/bin/env python
  2
  3 # Set up the functions our program will use
  4 def check_savings():
  5   total = 0
  6   f = open("savings.txt", "r")
  7   savings_from_file = f.readlines()
  8   f.close()
  9
 10   for i in savings_from_file:
 11     total += int(i)
 12   print("\nI have saved ${} so far this year\n".format(total))
 13
 14 def add_savings(amount):
 15   f = open("savings.txt","a")
 16   f.write("\n{}".format(amount))
 17   f.close()
 18   check_savings()

If we run the program from the command line, it should imply not provide any details, but should not error either. If we open the interactive shell again, we can see if we can successfully import the new Module we created and list all the Functions available on the Module using the dir() function:

>>> import savings_functions
>>> dir(savings_functions)
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', 
'__package__', '__spec__', 'add_savings', 'check_savings']

Now open the python_savings.py file with your text editor. The code below now has the functions removed and instead has an import statement at line 4. Line 21 now calls the add_savings() Function and line line 23 now calls the check_savings() Function:

  1 #!/usr/bin/env python
  2
  3 # Set up the functions our program will use
  4 import savings_functions
  5
  6 # Welcome the user and provide a menu
  7 while True:
  8   print("Welcome to Python Savings.")
  9   print("Choose from one of the tasks below.")
 10   print("1. Add Savings.")
 11   print("2. View Savings.")
 12   print("3. Quit Program.")
 13
 14   choice = input("From the items above make a select from 1 - 3: ")
 15
 16   if (int(choice) == 3):
 17     break
 18   elif (int(choice) < 3):
 19     if (int(choice) == 1):
 20       savings = input("\nHow much money did you save today? ")
 21       savings_functions.add_savings(savings)
 22     else:
 23       savings_functions.check_savings()
 24   else:
 25     print("\nYou have made an invalid choice, please try again\n")
 26

All the code is available on our GitHub repository that we have created with most of the example code we have placed in these articles: https://github.com/vincesesto/python_study/tree/main/article_10

If you now run the code, it all runs pretty smoothly with the Functions now put in their own file as a Module:

python .\python_savings.py
Welcome to Python Savings.
Choose from one of the tasks below.
1. Add Savings.
2. View Savings.
3. Quit Program.
From the items above make a select from 1 - 3: 2

I have saved $1040 so far this year

Welcome to Python Savings.
Choose from one of the tasks below.
1. Add Savings.
2. View Savings.
3. Quit Program.
From the items above make a select from 1 - 3: 1

How much money did you save today? 40

I have saved $1080 so far this year

Welcome to Python Savings.
Choose from one of the tasks below.
1. Add Savings.
2. View Savings.
3. Quit Program.
From the items above make a select from 1 - 3: 3

I hope that has clarified things about Modules and we'll have more for you shortly.

Found this post useful? Kindly tap the up vote button below! :)

About The Author

I am a DevOps Engineer, Endurance Athlete and Author. As a DevOps Engineer I specialize in Linux and Open Source Applications. Particularly interested in Search Marketing and Analytic’s, and is currently developing my skills in devops, continuous integration, security, and development(Python).

Posted with STEMGeeks

Sort:  

All the modules/libraries available for Python really make it powerful. Code reuse is a wonderful thing when you can do it, but you have to write with that in mind.

One other issue with using the * operator on import is there is a danger of losing track of which functions you are actually using. If you use short aliases then it is not big deal to make the module obvious.

I am writing Python most days for work and I enjoy it more than many other languages I have used.

!LUV

Thats a good point you bring up there...I don't think I've ever made a Python program that big to worry about but will need to keep an eye on that now.

Hi @run.vince.run, you were just shared some LUV thanks to @steevc. Having at least 5 LUV in your wallet enables you to give up to 3 LUV per day, for free. See the LUV tokens in your wallet at https://hive-engine.com or learn about LUV at https://peakd.com/@luvshares