# modules.py ''' Use Python modules and write your own by splitting your code into different files. ''' # Create a file with your functions, e.g. functions.py. # functions.py ''' Module containing some functions. ''' x = 5 def fib(n): a, b = 0, 1 while b < n: print(b) a, b = b, a+b def fib2(n): result = [] a, b = 0, 1 while b < n: result.append(b) a, b = b, a+b return result # Import the module. import functions # Use the functions defined in your module. functions.fib(3) # Import selectively. from functions import fib, fib2 a = fib(3) # Import all. from functions import * a = fib(3) b = fib2(3) # Import a module with a specified name. import functions as fun a = fun.fib(3) b = fun.fib2(3) import numpy as np np.sin(np.pi/2) # Acces the doc string. fun.__doc__ np.__doc__ # Reload a module when it is changed (Python 3) import importlib importlib.reload(fun) importlib.reload(np) # Search path for module: local directory and PYTHONPATH. import sys sys.path