Python: How to Write a Module
You can define functions, save it in a file, then later on, load the file and use these functions.
Save the following in a file and name it yy.py
def f1(n): return n+1
To load the file, use import yy
, then to call the function, use yy.f1
# import the module import yy # calling a function print yy.f1(5)
See also:
Python Module/Package/Namespace
- a “module” is a single file of Python code.
- a “package” is a directory of Python modules. (but can mean a single file module too.)
If you have multiple files, they make up a package. You put all files inside directory, possibly with nested sub directories. Like this:
fangame __init__.py soundx __init__.py rabbit_punch.py vid __init__.py tiger.py thinky __init__.py supertramp.py subby __init__.py a.py b.py
For example, in the above, you can load a module by:
import fangame.soundx.rabbit_punch import fangame.thinky.subby.a # ...
Dir with __init__.py
means that directory is part of the package. Python will look at the dir structure and file names as module's name when import
is called.
For example, if dir soundx
does not have a __init__.py
file, then import fangame.soundx.rabbit_punch
won't work, because Python won't consider dir soundx
as part of the package.
Similarly, fangame
must have a __init__.py
. And, if you want import fangame
to automatically load soundx/rabbit_punch
, then, in fangame/__init__.py
you must have import soundx.rabbit_punch
.
The file __init__.py
will be executed when that particular module in that directory is loaded. If you don't need any such init file, just create a empty file.
Here's a summary:
- When you do
import name
(or variant syntax ofimport
), Python searches for modules at current dir andsys.path
. [see Python: List Modules, Search Path, Loaded Modules] - Module files are just regular Python code. There's no code for module/package/namespace declaration.
- A package is directory of modules.
- A package's directory must have
__init__.py
file, but it can be empty. This file is loaded when modules in that dir is imported. - File names and directory structure are automatically taken to be the module's name and form namespace. For example, a dir named
a/b/c.py
is a module that can be imported withimport a.b.c
Note: it is suggested that module names be short and lower case only.
Note: the Python language does not have direct technical concept of “module” nor “package”. For example, there's not keyword “module” nor “package”. These words are only used to describe the concept of libraries. Technically, Python language only has a sense of namespace, and is exhibited by {import
, __init__.py
, __name__
, __all__
, …}.
Syntax: from x.y import z
Alternative syntax for loading module is from x.y import z
.
Typically, this is used to import the name z
, of single function z
of module file at x/y.py
. But the actual semantics is a bit complex.
from x.y import z
will do ONE of two things:
- ① Import the name
z
of a single function/variablez
of a module at pathx/y.py
, if that module does define the namez
. - ② Import the module names as prefix name
z
of a module at pathx/y/z.py
, if the filex/y.py
does not contain the namez
. For example, ifx/y/z.py
contains a function namedf
, then afterfrom x.y import z
, you can callf
byz.f()
.
If the module x/y.py
doesn't have the name z
, and there's no module at x/y/z.py
, then a ImportError
is raised.
Syntax: from x.y import *
Another syntax for loading module is from x.y import *
.
from x.y import *
will do this:
If the file at x/y/__init__.py
defines a variable named __all__
(which should contain a list of strings, each string is a submodule name), then, all those sub-modules are imported. The names imported are the strings of __all__
.
For example, if __all__ = ["a","b"]
, then the module at x/y/a.py
is imported as name a
and the module at x/y/b.py
is imported as name b
.
If __all__
is not defined, then from x.y import *
just imports the name x.y
(for module x/y.py
) and all names from module x/y.py
2014-04-09 Thanks to Kaito Kumashiro for correction. Thanks to Demian Brecht for suggestion.