To have truly reusable code, we need to access functions, variables, and objects that have already been written. Thus we need to have a way to share our code. This is where modules and packages are useful.

Python Module:

In this tutorial, we demonstrate how to create our first Python module and access its contents from a different Python program.

What Is a Module?

Working with Python it’s very easy to define new functions and assign values to variables that we would like to use multiple times. It would be great if we could write these useful pieces of code once and then use them whenever we need them. Thankfully, we can do just that because of modules.

In Python, a module is just a Python file. This means that we can use modules to divide our code into logical groupings by putting them into separate modules and then pulling those modules into our scripts or applications when we need them.

Creating Python Module:

To demonstrate how to create and use modules, let’s create a new directory called using_modules. Within it, we’ll define our first module by creating the using_modules/helpers.py file.

$ mkdir ~/using_modules
$ cd ~/using_modules
$ touch helpers.py

Within helpers.py, we’re placing some functions that we think will be generally useful and likely to be used in other files. Let’s write a few functions that can manipulate strings.

~/using_modules/helpers.py
def extract_upper(phrase):
    return list(filter(str.isupper, phrase))

def extract_lower(phrase):
    return list(filter(str.islower, phrase))

Now we have two functions defined and we’d like to use them in other scripts and modules.

Using Module from Another Script:

For this section, we’re going to be putting our example code into a script called main.py. Let’s create that script now and look at what we can do to pull in these functions so that we can use them.

The key to working with modules is the import statement. We’re going to dig deeper into all that we can do while importing modules in the next tutorials. But for now, we’re going to leverage the fact that we can import modules in the same directory as our script by referencing them by their file name minus the extension. In our case, these will be helpers.

~/using_modules/main.py
import helpers

Before we use our functions, let’s make sure that this file is valid by running it.

Terminal
$ python main.py
$

No output is a good sign. To utilize the functions defined in our module, we’ll add a period to the end of our module name (i.e. the file name) and then type the name of our function to call it as we otherwise would.

~/using_modules/main.py
import helpers

name = "Chandra Shekhar"
print(f"Lowercase letters: {helpers.extract_lower(name)}")
print(f"Uppercase letters: {helpers.extract_upper(name)}")

Let’s run this and verify it works as expected.

Terminal
$ python main.py
Lowercase letters: ['h', 'a', 'n', 'd', 'r', 'a', 'h', 'e', 'k', 'h', 'a', 'r']
Uppercase letters: ['C', 'S']

Perfect! Now we know the simplest way to define and use modules. In the next tutorials, we’ll dig deeper into the various ways and places that we can import modules.

References:

Happy Learning 🙂

Leave A Comment