[Tutor] How Do I Make Imports Work

Chris Fuller cfuller084 at thinkingplanet.net
Thu Dec 27 17:36:33 CET 2007


On Thursday 27 December 2007 08:29, james.homme at highmark.com wrote:
>
> e = Elevator()
> e.show_state()
>
> raw_input("\n\nPress the enter key to exit.")
>

Two observations:

This "module-level" code that does the testing will be run when the module is 
imported.  A more flexible approach would be to place it in a test() function 
and call it whenever you desire.  The test() function could even be a method 
of the class, if it makes sense to do that.  If you had a lot of classes, and 
each needed its own demo/test function, for instance.  If they all worked 
together, maybe one test function for all would suffice.

If you start to collect a lot of modules, you will probably want to organize 
them into directories.  It is a hassle to append stuff to sys.path every time 
you start python, so you may want to consider setting up .pth files.

See the python docs: http://docs.python.org/lib/module-site.html

Cheers


More information about the Tutor mailing list