- Press Ctrl+D to exit the Python interactive shell.
- If you want to power down your Raspberry Pi now, type sudo poweroff and
press the Enter key.
Understanding how to look at the various modules and their functions will be useful for you in the
years to come. This will be especially true as new modules become part of the standard library. And
didn’t you have fun seeing an Easter egg?
Creating Custom Modules
When you have created several functions, you might want to reuse them in your Python scripts. It can
be helpful to create a custom module to house your functions. Generally speaking, it takes about seven
steps to create a custom module:
- Create or gather functions that it makes sense to put together.
- Determine a module name.
- Create the custom module in a test directory.
- Test the custom module.
- Move the module to a production directory.
- Check the path and modify it if needed.
- Test the production custom module.
A few of these steps require more work than they might seem like they’d need. The following sections
examine all the steps and give you directions on successfully accomplishing them.
Creating or Gathering Functions That Go Together
What functions to gather into a module is relative. It comes down to what makes sense and will be the
most productive for you. However, keep in mind that you might want to distribute your functions to
others, so take some time here. A logical gathering of functions can serve the greater community.
As an example in the following sections, you will gather two of the functions created in Hour 12 to
create a module. They are dbl() and addem().
Determining a Module Name
Naming a module is not a trivial step. Python has a few rules for naming modules, and if you do not
follow them, your module won’t work. For example, a module name cannot be a Python keyword. In
Hour 4, the “Python Keywords” section explores this topic concerning variables. The same rules
apply to modules.
It is also a standard practice to name custom modules using very short names, with all the characters
in lowercase. Additional rules focus on the file name that holds the module’s functions:
Custom module file names must end in .py. If they don’t, Python cannot import the modules.
A file name cannot contain a dot (.) except right before the file name extension. For example,
my.module.py is not a legal module file name. However, mymodule.py is a legal module
file name because you need the last dot (.) to denote the file’s extension.
The file name must match the module name. Therefore, if the module’s name is ni, the file