Creating your own modules is easy, you've been doing it all along! 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.
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
The above was a sample module. As you can see, there is nothing particularly special about 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 in the same directory as the program that
we import it in, or the module should be in one of the directories listed in
sys.path
.
#!/usr/bin/python # Filename: mymodule_demo.py import mymodule mymodule.sayhi() print 'Version', mymodule.version