Learning Resources

A module name and custom modules

A module's __name__

Every module has a name and statements in a module can find out the name of their module. This is handy for the particular purpose of figuring out whether the module is being run standalone or being imported. As mentioned previously, when a module is imported for the first time, the code it contains gets executed. We can use this to make the module behave in different ways depending on whether it is being used by itself or being imported from another module. This can be achieved using the __name__ attribute of the module.


# Filename: using_name.py
if __name__ == '__main__':
    print('This program is being run by itself')
    print('I am being imported from another module')


   $ python using_name.py
   This program is being run by itself
   $ python
   >>> import using_name
   I am being imported from another module

How It Works:

Every Python module has its __name__ defined. If this is '__main__', that implies that the module is being run standalone by the user and we can take appropriate actions.

Making Your Own Modules

Creating your own modules is easy, you've been doing it all along! This is because every Python program is also a module. You just have to make sure it has a .py extension. The following example should make it clear.


# Filename: mymodule.py
def sayhi():
    print('Hi, this is mymodule speaking.')
__version__ = '0.1'
# End of mymodule.py

The above was a sample module. As you can see, there is nothing particularly special about it compared to our usual Python program. We will next see how to use this module in our other Python programs.

Remember that the module should be placed either in the same directory as the program from which we import it, or in one of the directories listed in sys.path.

# Filename: mymodule_demo.py
import mymodule
print ('Version', mymodule.__version__)


   $ python mymodule_demo.py
   Hi, this is mymodule speaking.
   Version 0.1

How It Works:

Notice that we use the same dotted notation to access members of the module. Python makes good reuse of the same notation to give the distinctive 'Pythonic' feel to it so that we don't have to keep learning new ways to do things.

Here is a version utilising the from..import syntax:

# Filename: mymodule_demo2.py
from mymodule import sayhi, __version__
print('Version', __version__)

The output of mymodule_demo2.py is same as the output of mymodule_demo.py.

Notice that if there was already a __version__ name declared in the module that imports mymodule, there would be a clash. This is also likely because it is common practice for each module to declare it's version number using this name. Hence, it is always recommended to prefer the import statement even though it might make your program a little longer.

You could also use:

from mymodule import *

This will import all public names such as sayhi but would not import __version__ because it starts with double underscores.

Zen of Python -One of Python's guiding principles is that "Explicit is better than Implicit".