Computer >> Computer tutorials >  >> Programming >> Python

How to write a python module?


A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py

 create helloworld.py then write the following function as its content:

 def hello_world():
    print "Hello world"

 Now create another file in the same directory called script.py. Write the following as its contents:

 import helloworld
helloworld.hello_world()

 We created the helloworld module and imported it in script.py. Then we used the function from helloworld.py. Creating many modules in the working directory can create a lot of clutter. You can use packages to neatly group multiple modules together. You can read about how packages are created and used in detail here: https://docs.python.org/2/tutorial/modules.html#packages