-
Step 1
Decide on a short and descriptive module name, taking into account that users of your module (including yourself) will have to type this name often. A module name follows the same guidelines as any other identifier in the Python language. It must start with a letter and contain only letters, numbers and the underscore character.
-
Step 2
Create a new script file. This script file will hold the code for this module. It should be named "module_name.py". For example if you had a module for dealing with temperature conversions called "temperature", you would create a file called "temperature.py".
-
Step 3
Create a file header. In a series of comments, describe what the module is, what functions and classes it contains and how to use them. Include as much information as you can, these module files are often forgotten about even though you may use them in many of your programs.
-
Step 4
Write the code. There are no special keywords or statements to define the module, the filename alone is enough. Any code in this file will be part of that module. If you've already written the code in another file, you can cut and paste. Add comments before non-trivial functions describing their function and usage, making as much comment as possible.
-
Step 1
Import the module into your program prior to using it. The module file has to be either in the same directory or one of your configured module directories. Use the following command:
"import temperature" -
Step 2
Begin using functions and classes from the module once it is imported.
"celsius = temperature.fah_to_cel(fahrenheit)" -
Step 3
Import some or all of the module into your current namespace. Typing the module name all the time can be inconvenient. If you use the module name often or it is long or difficult to type, you can import some or all of the functions or classes in the module into your current namespace.
"# Import the fah_to_cel function
from temperature import fah_to_cel
# Import everything from the temperature module
from temperature import *"












