- Python Modules
- Create a Module
- Example
- Use a Module
- Example
- Variables in Module
- Example
- Example
- Naming a Module
- Re-naming a Module
- Example
- Built-in Modules
- Example
- Using the dir() Function
- Example
- Import From Module
- Example
- Example
- Where to Store a Module?
- Example
- Example
- Importing Python Modules
- There are Many Ways to Import a Module #
- Which Way Should I Use? #
- What Does Python Do to Import a Module? #
- Import Gotchas #
- Using Modules as Scripts #
- Circular Imports #
Python Modules
A file containing a set of functions you want to include in your application.
Create a Module
To create a module just save the code you want in a file with the file extension .py :
Example
Save this code in a file named mymodule.py
Use a Module
Now we can use the module we just created, by using the import statement:
Example
Import the module named mymodule, and call the greeting function:
Note: When using a function from a module, use the syntax: module_name.function_name.
Variables in Module
The module can contain functions, as already described, but also variables of all types (arrays, dictionaries, objects etc):
Example
Save this code in the file mymodule.py
Example
Import the module named mymodule, and access the person1 dictionary:
Naming a Module
You can name the module file whatever you like, but it must have the file extension .py
Re-naming a Module
You can create an alias when you import a module, by using the as keyword:
Example
Create an alias for mymodule called mx :
Built-in Modules
There are several built-in modules in Python, which you can import whenever you like.
Example
Import and use the platform module:
Using the dir() Function
There is a built-in function to list all the function names (or variable names) in a module. The dir() function:
Example
List all the defined names belonging to the platform module:
Note: The dir() function can be used on all modules, also the ones you create yourself.
Import From Module
You can choose to import only parts from a module, by using the from keyword.
Example
The module named mymodule has one function and one dictionary:
def greeting(name):
print(«Hello, » + name)
Example
Import only the person1 dictionary from the module:
from mymodule import person1
Note: When importing using the from keyword, do not use the module name when referring to elements in the module. Example: person1[«age»] , not mymodule.person1[«age»]
Where to Store a Module?
You can store the modules in the same directory as the rest of your python files, or you can choose a different location.
Python will start by searching the current directory, then it will search in every location described in the PYTHONPATH variable.
To see which locations that are described in the PYTHONPATH variable, check the sys.path property:
Example
List all the locations described in the PYTHONPATH property:
To add locations to the PYTHONPATH variable use the set PYTHONPATH statement:
Example
Add a new location to the PYTHONPATH property:
Note: The dir() function can be used on all modules.
Importing Python Modules
The import and from-import statements are a constant cause of serious confusion for newcomers to Python. Luckily, once you’ve figured out what they really do, you’ll never have problems with them again.
This note tries to sort out some of the more common issues related to import and from-import and everything.
There are Many Ways to Import a Module #
Python provides at least three different ways to import modules. You can use the import statement, the from statement, or the builtin __import__ function. (There are more contrived ways to do this too, but that’s outside the scope for this small note.)
Anyway, here’s how these statements and functions work:
- import X imports the module X, and creates a reference to that module in the current namespace. Or in other words, after you’ve run this statement, you can use X.name to refer to things defined in module X.
- from X import * imports the module X, and creates references in the current namespace to all public objects defined by that module (that is, everything that doesn’t have a name starting with “_”). Or in other words, after you’ve run this statement, you can simply use a plain name to refer to things defined in module X. But X itself is not defined, so X.name doesn’t work. And if name was already defined, it is replaced by the new version. And if name in X is changed to point to some other object, your module won’t notice.
- from X import a, b, c imports the module X, and creates references in the current namespace to the given objects. Or in other words, you can now use a and b and c in your program.
- Finally, X = __import__(‘X’) works like import X, with the difference that you 1) pass the module name as a string, and 2) explicitly assign it to a variable in your current namespace.
Which Way Should I Use? #
Short answer: always use import.
As usual, there are a number of exceptions to this rule:
- The Module Documentation Tells You To Use from-import. The most common example in this category is Tkinter, which is carefully designed to add only the widget classes and related constants to your current namespace. Using import Tkinter only makes your program harder to read; something that is generally a bad idea.
- You’re Importing a Package Component. When you need a certain submodule from a package, it’s often much more convenient to write from io.drivers import zip than import io.drivers.zip, since the former lets you refer to the module simply as zip instead of its full name. In this case, the from-import statement acts pretty much like a plain import, and there’s not much risk for confusion.
- You Don’t Know the Module Name Before Execution. In this case, use __import__(module) where module is a Python string. Also see the next item.
- You Know Exactly What You’re Doing. If you think you do, just go ahead and use from-import. But think twice before you ask for help 😉
What Does Python Do to Import a Module? #
When Python imports a module, it first checks the module registry (sys.modules) to see if the module is already imported. If that’s the case, Python uses the existing module object as is.
Otherwise, Python does something like this:
- Create a new, empty module object (this is essentially a dictionary)
- Insert that module object in the sys.modules dictionary
- Load the module code object (if necessary, compile the module first)
- Execute the module code object in the new module’s namespace. All variables assigned by the code will be available via the module object.
This means that it’s fairly cheap to import an already imported module; Python just has to look the module name up in a dictionary.
Import Gotchas #
Using Modules as Scripts #
If you run a module as a script (i.e. give its name to the interpreter, rather than importing it), it’s loaded under the module name __main__.
If you then import the same module from your program, it’s reloaded and reexecuted under its real name. If you’re not careful, you may end up doing things twice.
Circular Imports #
In Python, things like def, class, and import are statements too.
Modules are executed during import, and new functions and classes won’t appear in the module’s namespace until the def (or class) statement has been executed.
This has some interesting implications if you’re doing recursive imports.
Consider a module X which imports module Y and then defines a function called spam:
# module X import Y def spam(): print "function in module x"
If you import X from your main program, Python will load the code for X and execute it. When Python reaches the import Y statement, it loads the code for Y, and starts executing it instead.
At this time, Python has installed module objects for both X and Y in sys.modules. But X doesn’t contain anything yet; the def spam statement hasn’t been executed.
Now, if Y imports X (a recursive import), it’ll get back a reference to an empty X module object. Any attempt to access the X.spam function on the module level will fail.
# module Y from X import spam # doesn't work: spam isn't defined yet!
Note that you don’t have to use from-import to get into trouble:
# module Y import X X.spam() # doesn't work either: spam isn't defined yet!
To fix this, either refactor your program to avoid circular imports (moving stuff to a separate module often helps), or move the imports to the end of the module (in this case, if you move import Y to the end of module X, everything will work just fine).
Copyright © 1998-2001 by Fredrik Lundh