Separation of Code in CGI App

Paul McNett p at ulmcnett.com
Fri Jul 22 11:36:40 EDT 2005


Rob Cowie wrote:
> So, do I separate out whatever code I want to into .py modules, then
> have the cgi script import them, and utilise them as required?

Bingo!


> If so, do I need to put these modules somewhere described in the System
> Path? 

Put them in the same directory, and you don't have to worry any more 
about it. You can get more advanced if you want by putting your modules 
into a "package", which is just a directory with a __init__.py file - 
see the docs at python.org.


> Can I modify the PATH at runtime to point to my modules? How?

If you want to have them in a different directory, and the directory 
isn't already in sys.path, then just issue, from your main script, 
before your import statements:

import sys
sys.path.insert(0, "/path/to/your/modules")


> Cheers for being patient!

Welcome to Python! Being new to it, you may want to consider having 
someone review your code before putting it on the public internet, 
simply because there are lots of opportunities for inadvertantly 
providing backdoors (Python is powerful). This is especially true if you 
let your users type into a text box, or if you do dynamic database 
queries, or both.

Also, have you run through the tutorial on python.org?


-- 
Paul McNett
http://paulmcnett.com




More information about the Python-list mailing list