I have a Math and Physics program that I want to make. I have decided that instead of putting all of it in one file I should separate it into several files. I later learnt that the act of doing this is creating a module. I have looked around the web and all the tutorials there are very confusing. I would like to know how to create a module and all of the basic technical details behind it. I am also confused by the term python path. When I looked up a tutorial on creating modules it looked like this:
# Fibonacci numbers module
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a+b
return result
import fibo
From the Python documentation about Modules :
A module is a file containing Python definitions and statements. The file name is the module name with the suffix
.py
appended.
To create a Module with this two functions, you have to move your function fib
and fib2
to a file called fibo.py
.
Then you have to create a second script to use this new module with import fibo
:
import fibo
fibo.fib(1000)
Make sure Python can find the Module. From the Python documentation about The Module Search Path :
When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable
sys.path
.sys.path
is initialized from these locations:
- the directory containing the input script (or the current directory).
PYTHONPATH
(a list of directory names, with the same syntax as the shell variablePATH
).- the installation-dependent default.
After initialization, Python programs can modify
sys.path
. The directory containing the script being run is placed at the beginning of the search path, ahead of the standard library path. This means that scripts in that directory will be loaded instead of modules of the same name in the library directory. This is an error unless the replacement is intended.
If you are interested in Modules, you probably will be interested in Packages too. From the Python documentation about Packages :
Packages are a way of structuring Python's module namespace by using “dotted module names”. For example, the module name
AB
designates a submodule namedB
in a package namedA
. Just like the use of modules saves the authors of different modules from having to worry about each other's global variable names, the use of dotted module names saves the authors of multi-module packages like NumPy or the Python Imaging Library from having to worry about each other's module names.
This is how to create a Package:
When importing the package, Python searches through the directories on
sys.path
looking for the package subdirectory.The
__init__.py
files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case,__init__.py
can just be an empty file, but it can also execute initialization code for the package or set the__all__
variable, described later.
Put this:
def fib(n): # write Fibonacci series up to n
a, b = 0, 1
while b < n:
print b,
a, b = b, a+b
def fib2(n): # return Fibonacci series up to n
result = []
a, b = 0, 1
while b < n:
result.append(b)
a, b = b, a+b
return result
in a file. If you want to use it only in the current directory, leave it there. If you want to use it from everywhere, put it in the site-packages directory of Python.
That's it...
You can add something like this at the end of the file:
def main():
# Add test code for the library functions here
return 0
if __name__ == '__main__':
main()
This makes it possible to 'execute' the library immediately, running eg. test code for the routines which are in it. If it's called as module (with import), then the if
at the end will not call main()
and it's just a library.
It's as simple as that. The python path assure where Python will look, which is normally in all site-packages dirs.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.