A module is basically a single file, containing executable code, and class and function definitions.
When a module is imported all code in it is executed.
For example:
/modules a.py b.py
a.py:
import b print "this is module a" def function_a(): print "this is function a"
b.py:
print "this is module b" def function_b(): print "this is function b"
run:
>python a.py this is module b this is module a
However, if you are declaring variables in the imported module, you need to reference the variable’s namespace.
eg.
b.py:
var_b = 10 def function_b(): print "this is function b" class class_b: class_var_b = 10 def class_function_b(self): print "this is class function b"
a.py:
import b b.function_b() print b.var_b my_class = b.class_b() my_class.class_function_b()
output:
>python a.py this is function b 10 this is class function b
Using ‘from’.
When you use from, you no longer need to specify the namespace, but all code within the imported module is still executed.
b.py:
print "this is module b" var_b = 10 def function_b(): print "this is function b"
a.py:
from b import function_b print "this is module a" def function_a(): print "this is function a" function_a() function_b() print var_b [/python] output: >python a.py this is module b this is module a this is function a this is function b <snip> NameError: name 'var_b' is not defined
Here, we can access function_b because we explicitly imported it, but we can't access var_b because we haven't imported it.
Packages
We can use packages to organise our modules. Packages are a essentially a just a folder, and modules then include the folder name in the module's namespace.
Packages require an __init__.py in the folder in order to be detected.
/modules /folder_a __init__.py a.py main.py
__init__.py:
print "this is folder_a __init__.py"
a.py:
print "this is module a" def function_a(): print "this is function a"
main.py:
import folder_a.a print "this is module main" folder_a.a.function_a()
output:
>python main.py this is folder_a __init.py this is module a this module main this is function a
You can use 'as' to reduce the namespace length
eg.
import folder_a.a as a print "this is module main" a.function_a()
Putting imports into your __init__.pys
Whenever the a package is referenced, the code in the __init__.py is exectuted.
This means that you can include import statements in your __init__.py.
For example you can import the whole package, instead of individually importing the package contexts (or using import *).
eg.
/modules /folder_a __init__.py a.py a2.py main.py
__init__.py:
print "this is folder_a __init__.py" import a, a2
a.py:
print "this is module a" def function_a(): print "this is function a"
a2.py:
print "this is module a2" def function_a_two(): print "this is function a two"
main.py:
import folder_a print "this is module main" folder_a.a.function_a() folder_a.a2.function_a_two()
output:
>python main.py this is folder_a __init.py this is module a this is module a2 this module main this is function a this is function a two
What if two modules contain a method with the same name?
If you are straight importing the modules, then this won't be a problem, as the method's namespace will distingush the two.
However, if you are using the 'from module import method' method, then the method that was important second will take precedent.
/modules main.py a.py b.py
main.py:
from a import foo from b import foo foo()
a.py:
print "this is module a" def foo(): print "module a foo"
b.py:
print "this is module b" def foo(): print "module b foo"
output:
>python main.py this is module a this is module b module b foo
eg.
Imports - from and import statements:
Here is a good stack overflow question that discusses 'from module import foo' vs 'import module'.
Resources
Namespaces http://bytebaker.com/2008/07/30/python-namespaces/
Pingback: Project 1: Day 9 – Python import comprehension. – Thirty Day Projects Blog