Professional Documents
Culture Documents
We've seen that a module is a file that can contain classes as well as its own
variables.
We've seen that you need to import it to access the code, and then use the
module name to refer to it.
import module1
a = module1.ClassName()
Packages
modules: usually single files to do some set of jobs
packages: modules with a namespace, that is, a unique way of referring
to them
libraries: a generic name for a collection of code you can use to get
specific types of job done.
Packages
The Standard Python Library comes with a number of other packages
which are not imported automatically.
Some people like this, but it does make the code harder to understand.
When importing, Python will import parent packages (but not other
subpackages)
If hasn’t been used before, will search import path, which is usually
(but not exclusively) the system path.
If you're importing a package, don't have files with the same name (i.e.
package_name.py) in the directory you're in, or they'll be imported
rather than the package (even if you're inside them).
Interpreter
To reload a module:
import importlib
importlib.reload(modulename)
The main difference when thinking of module and scripts is that the
former is generally imported, and the latter generally runs directly.
import module1
print(module1.module_variable)
module1.module_function()
a = module1.ClassName()
Indeed, you have to be slightly careful when Importing modules
importing modules. Modules and the classes in
them will run to a degree on import.
class cls:
print ("class loading") # Runs
def m2():
print("instance method loading")
Modules that run
If you're going to use this to run code, note that in general, code accessing a class or
method has to be after if is defined:
c = A()
c.b()
class A:
def b (__self__) :
print ("hello world")
class A:
def b (self) :
print ("hello world")
c = A()
Modules that run
However, generally having large chunks of unnecessary code running is
bad.
Setting up variables is usually ok, as assignment generally doesn't cause
issues.
Under the philosophy of encapsulation, however, we don't really want
code slooping around outside of methods/functions. The core
encapsulation level for Python are the function and objects (with
self; not the class).
It is therefore generally worth minimising this code.
Running a module
The best option is to have a 'double headed' file, that runs as a script with
isolated code, but can also run as a module. As scripts run with a global
__name__ variable in the runtime set to "__main__", the following code in a
module will allow it to run either way without contamination.
if __name__ == "__main__":
# Imports needed for running.
function_name()
Structure that constructs a dot delimited
namespace based around a directory Packages
structure.
Allows files to import each other without being limited to same directory.
The more detailed the namespace (e.g. including unique identifiers) the less
likely your identifiers (classnames; function names; variables) are to clash with
someone else's.