Make your own module
Creating your own module is very simple and you've been doing it! Each Python program is also a module. You have ensured that it has a. py extension. The following example will make it clearer.
Create your own module
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Example 8.3 How to create your own module
# !/usr/bin/python # Filename:mymodule.py def Sayhi (): Print ' Hi, this is MyModule speaking ' version='0.1'#End of mymodule.py
Above is an example of a module. As you've seen, it's nothing special compared to our normal Python program. We'll look at how to use this module in our other Python programs.
Remember that this module should be placed in the same directory as the program we entered it, or in one of the directories listed in Sys.path.
# !/usr/bin/python # Filename:mymodule_demo.py Import Mymodulemymodule.sayhi () Print ' version ', mymodule.version
Output
$ python mymodule_demo.py
Hi, this is MyModule speaking.
Version 0.1
How it works
Note that we use the same dot number to use the members of the module. Python is good at reusing the same tokens, so that we python programmers don't need to learn new methods constantly.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From.. Import
Here is a use from: The version of the import syntax.
# !/usr/bin/python # Filename:mymodule_demo2.py from Import Sayhi,versionsayhi () Print ' Version ', version
The output of the mymodule_demo2.py is exactly the same as the mymodule_demo.py.
Module's Custom module