% class-10 talking points Organizing large systems: modules, packages, PYTHONPATH Examples - BioPython (or NumPy - ? or gnuradio - ?) Review modules, distinction between module file and module object in program Given module foo with attribute x, x could be variable, function, class, ... import foo # loads all contents of foo module file into foo module object ... foo.x ... # module name prefix distinguishes x, foo.x, bar.x, ... import foo as f # 'nickname': loads contents of foo into f module object ... f.x ... # maintains prefix, but saves space (and your fingers) from foo import x # loads x from foo into current module ... x ... # NO prefix, but what if there was an x here already? from foo import * # puts all contents of module foo into current module ... x ... # NOT recommended in module, can be OK in interactive session Packages add dotted (hierarchical) module names import foo # import package (just the foo module itself) import foo.util # import module from package from foo import bar # import module from package into current module import bar.util as u # import module rom package with nickname ... foo.x ... foo.util.x ... bar.x ... u.x ... # all distinguished by prefix A package is a module that contains other packages Modules are files, packages are directories + contents A package P is a directory named P that contains a file named __init__.py Other modules in directory P are the modules of package P Some of those modules can themselves be packages (dirs with own __init__.py) __init__.py can be empty or just a docstring, can include any code modules in package can import other modules in package without package prefix. for example see scipy/integrate/__init__.py This means you can easily turn a bunch of modules into a package at any time. Where do installed packages live? Usually in .../site_packages There might be several on my Windows laptop: C:\Python26\lib\site-packages on my Mac: /Library/Python/2.6/site-packages, but also /opt/local/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages How does the python interpreter find site-packages? sys.path How does sys.path get set? program startup, PYTHONPATH, among other things Installation should put packages in right place, update PYTHONPATH if needed etc Binary installers: windows .msi or .exe, Mac .dmg package managers: Debian/Ubuntu apt/Synaptic, Red Hat rpm, Mac macports, ... python distutils: unpack source tar or zip, then python setup.py install, etc. bunch of modules: put them somewhere convenient, assign PYTHONPATH yourself