3 Key Skills for Handling Modules in Python

Manage and use Python modules elegantly

Yang Zhou
Yang Zhou
Mar 13 · 5 min read

In Python, a module is a file containing Python code and its name has the suffix — .py. Generally speaking, putting related classes or functions into a module and separating the whole program into different modules are good programming practice.

However, when a project becomes larger, handling more and more modules would become difficult.

Fortunately, Python is very flexible and there are many good tricks and practice that can help us manage and use modules elegantly. This article will summary them into three key skills. After reading, handling modules would just be a piece of cake for you. 🍰

1. Import Python Modules Skilfully

When we writing programs, using other existed modules is nearly inevitable. Therefore, knowing how to import modules properly is very important. Basically, we all know the template of importing modules in Python:

from A import B

And the from A is optional.

It looks simple, but don’t underestimate it. Here are 3 tips that can help us use it more elegantly and avoid potential bugs.

(1) Don’t use an asterisk to import all things at once

There is a simple way to import everything in a module at once with the help of one asterisk:

from module_a import *

Although the asterisk is a very helpful symbol in Python, using it this way is not recommended. Because it’s not clear what will be imported, some potential bugs could appear:

  • The names in the current file may conflict with the imported names.
  • If we add something new in the imported module, all new names will be imported as well. It will increase the possibility of name conflicts.

Therefore, we should never import all names at once by the asterisk. Only importing what you need is a really good practice.

(2) Give it a nickname for convenience

If you are a data scientist, the following lines of code must be your friends:

import numpy as npimport pandas as pdimport matplotlib.pyplot as plt

With the help of as, we can give a long-name module a shorter and convenient nickname. Instead of texting the long name again and again, we can just use their nicknames in our following code. This small trick brings great convenience.

(3) Use relative imports smartly

With the help of dots, we have a succinct way to import the relative modules of the current file as follows:

from . import module_a
from .. import module_b
  • A single dot means that the module_a is imported from the same directory as the current file’s location (it’s siblings).
  • Two dots mean that the module_b is imported from the above directory of the current file’s location (it’s parents).

These tricks are called relative imports. It can give us a bit more convenience.

2. Execute Python Modules As Scripts

Besides importing a module, we can also run a module independently as a script.

Let’s take a simple example:

Here is a module called test.py. It contains one line of code:

print("Yang Zhou")

Now, if we want to run the module on a terminal, just enter the commands as follows:

Execute Python Modules As Scripts
Execute Python Modules As Scripts

As the above result shows, we can execute a Python module in the terminal directly. The syntax is simple, we just need to enter the python following the module’s name.

Furthermore, if the text.py contains functions or classes, rather than some code that can be executed directly, how to run it as a script?

The trick here is to write a “main” function for our module.

Now, our test.py module has one function. In the meanwhile, we add a “main” function to it as follows.

We can execute the above module in the terminal:

Execute Python module in the terminal
Execute Python module in the terminal

You may confuse about how this wired code if __name__ == "__main__" works?

When we run a module directly as a script, the module itself is the “main” function. All the code within the if __name__ == "__main__" block will be executed.

On the other hand, if the module is imported into other modules, it’s not the “main” function, and the code within the if __name__ == "__main__" block won’t be executed.

Therefore, this mechanism is very helpful and give us flexibility. If some code shouldn’t run when importing the module, but it’s necessary when the module executing as a script, the if __name__ == "__main__" trick is important.

3. Manage Python Modules by Packages

The packages in Python are used to group different modules. It’s a good habit to put some logically related modules into the same package.

For example, the following image shows the file structure of a simple Django project called “mysite”:

The file structure of a Django project
The file structure of a Django project

In this project, the “blog” and “mysite” are two Python packages and the “migrations” is a sub package of the “blog”. Obviously, using packages to manage modules makes logical structures of the project clear.

Conclusion

Modules and packages are basic components of a Python project. Being familiar with the key skills for managing them is important. After all, it will save us lots of time if the structure of all files in a project is clear and neat.

Thanks for reading. If you like it, don’t forget to follow me to get more great articles about programming and technologies!

Relative article:

TechToFreedom

Dive into technology, investment and entrepreneurship.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store