Python Modules#

In Python, Modules are files with the .py extension that can be imported inside another Python file to reuse code between applications or break down a big application into smaller files for easier organization and more manageable code base. These modules contain constants, variables, functions and classes for example.

Creating and using a module#

As modules in Python are files, they can be created and used in the same way as any other file. The only difference is that the module must be imported to be used. A module can also be a collection of files in a directory.

Create a file called mymodule.py with the following

Example module called mymodule.py#
1def greeting(name):
2    print("Hello, " + name)

In our main program we can import the module and can call the function defined in the module

Example program that imports module mymodule.py#
 1#!/usr/bin/env python3
 2
 3import mymodule
 4
 5def main():
 6    mymodule.greeting("John")
 7
 8
 9if __name__ == "__main__":
10    main()

Using variable in modules#

A module can also contain functions and classes, but also variables of all types. If we extend the module with a variable, we can use it in our main program. As in the example below, we extend the module with a dictionary called person.

Example of variable in module mymodule.py#
4person = {
5    "name": "John",
6    "age": 42,
7    "country": "Ireland"
8}

We can now access the variable person and use age defined within person. The import of the module creates a namespace for the module, so we can access the variable via mymodule.person. This is also the reason why we can use the variable person in our main program or can be defined in another modules.

Example of accessing variable in module mymodule.py#
 1#!/usr/bin/env python3
 2
 3import mymodule
 4
 5def main():
 6    age = mymodule.person["age"]
 7    print(age)
 8
 9
10if __name__ == "__main__":
11    main()

Running the program that imports mymodule.py and uses the variable person shows that age is 42.

Output of printing variable age in module mymodule.py#
42

Import from a module#

You can also import a certain section from a module

Example of importing a specified section from a module#
 1#!/usr/bin/env python3
 2
 3from mymodule import person
 4
 5def main():
 6    print(person["age"])
 7
 8
 9if __name__ == "__main__":
10    main()

Rename a module#

We can rename a module during import

Example of renaming a module during import#
 1#!/usr/bin/env python3
 2
 3import mymodule as abc
 4
 5def main():
 6    age = abc.person["age"]
 7    print(age)
 8
 9
10if __name__ == "__main__":
11    main()

Built-in modules#

Example of importing a built-in module#
 1#!/usr/bin/env python3
 2
 3import platform
 4
 5def main():
 6    x = platform.system()
 7    print(x)
 8
 9
10if __name__ == "__main__":
11    main()
Example to show the content of a module#
 1#!/usr/bin/env python3
 2
 3import platform
 4
 5def main():
 6    x = dir(platform)
 7    print(x)
 8
 9
10if __name__ == "__main__":
11    main()
Output of dir(platform)#
['DEV_NULL', '_UNIXCONFDIR', '_WIN32_CLIENT_RELEASES', '_WIN32_SERVER_RELEASES', '__builtins__', '__cached__', '__copyright__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '__version__', '_comparable_version', '_component_re', '_default_architecture', '_dist_try_harder', '_follow_symlinks', '_ironpython26_sys_version_parser', '_ironpython_sys_version_parser', '_java_getprop', '_libc_search', '_linux_distribution', '_lsb_release_version', '_mac_ver_xml', '_node', '_norm_version', '_parse_release_file', '_platform', '_platform_cache', '_pypy_sys_version_parser', '_release_filename', '_release_version', '_supported_dists', '_sys_version', '_sys_version_cache', '_sys_version_parser', '_syscmd_file', '_syscmd_uname', '_syscmd_ver', '_uname_cache', '_ver_output', '_ver_stages', 'architecture', 'collections', 'dist', 'java_ver', 'libc_ver', 'linux_distribution', 'mac_ver', 'machine', 'node', 'os', 'platform', 'popen', 'processor', 'python_branch', 'python_build', 'python_compiler', 'python_implementation', 'python_revision', 'python_version', 'python_version_tuple', 're', 'release', 'subprocess', 'sys', 'system', 'system_alias', 'uname', 'uname_result', 'version', 'warnings', 'win32_ver']