[mod_python] Modules and scoping

Tobiah toby at rcsreg.com
Fri Sep 10 04:53:47 EDT 2004


With PHP, I used to simply 'include()' sections of code to
get the code into different files.  Basically, all of the files
shared the same namespace, and I used a few global vars to tie
everything together.

No I'm tring to understand how to use the separate namespaces
of imported modules.  My web app has certain things, like a
user's progress through the app, stuff in his cart, his ID
number, the database link etc... that almost every module
needs to have access to.  What I have done is to make a wrapper
object, stuffed the req object into it, and pass that around to
just about every class constructor or as an argument to a function
call.  Does this seem reasonable, or am I missing the point.

Another issue is the import path.  I am building the app
in my regular web space using the Publisher handler.  The
publisher seems to wake up with some other working directory,
and so does not see modules in the same directory as index.py.
I am choosing not to use parts of the URL to index into module
files... all requests will be to index.py, and I will conditionally
import the appropriate content generating modules based on
context.

So, I modified sys.path, after examining the req object to find the
script location.  But it seems that I have to do this for every
module that imports other modules.  Does that seem reasonable?
I hope that I am way off of the mark with my method, and that
someone can enlighten me.

Thanks,

Tobiah


More information about the Mod_python mailing list