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 variable PATH
).
- 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 A.B
designates a
submodule named B
in a package named A
. 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.