[mod_python] pythonpath

Jorey Bump list at joreybump.com
Mon Jan 31 10:42:21 EST 2005


Lionel Roubeyrie wrote:
> Hi,
> Le Lundi 31 Janvier 2005 15:12, Jorey Bump a écrit :
> 
>>You didn't provide enough details. Show us:
>>
>>1. How you've configured mod_python (in httpd.conf or .htaccess)
> 
> in httpd.conf of Apache2:
> Alias /cartairweb /topdir/webdir
> Directory /topdir/webdir>
>         Order allow,deny
>         Allow from all
>         Options Indexes Includes FollowSymLinks MultiViews
>         AddHandler mod_python .py
>         PythonHandler mod_python.publisher
> #       PythonPath "sys.path+['/topdir']"
>         PythonDebug On
> </Directory>
> 
>>2. How and where you've set the PYTHONPATH
> 
> Like in the previous lines, or using a more Goliath' like method calling 
> setEnviron() :
> ####################################
> def getParentDir():
>     currentfile = os.path.abspath(__file__)
>     currentdir = os.path.split(currentfile)[0]
>     parentdir = os.path.split(currentdir)[0]
>     return parentdir
> def setEnviron():
>     parentdir = getParentDir()
>     if parentdir not in sys.path : sys.path.append(parentdir)
>     os.environ['PYTHONPATH']=parentdir
> ####################################
> In all case, I have to change the current working directory to where the web 
> pages are.
> 
>>3. The relevant parts of your importing module
> 
> In fact, the error comes when I import the desired module (cartair) located in 
> the topdirectory:
> ####################################
> import os, utils
> import cartair
> utils.setCurrentWkDir()
> utils.setEnviron()
>     
> def index(req, executer='0', section='PREVAIR', date_calcul='', date_prevu='', 
> code_modele='', polluant='', niveau='', squelette=''):
>     result = ''
>     conf= cartair.CartairConf()
>     date = cartair.Cdate()
>     try:
>         if (code_modele == '') or (code_modele == None): 
> code_modele=conf.get(section, 'code_modele')
> ...
> ####################################
> 
>>4. The URL you are using
> 
> http://localhost/cartairweb/
> 
>>5. The relevant error entries in apache's error log
> 
> no real error
> 
>>Don't bother with /etc/profile or .bashrc. Also, include a function in
>>your module to display the path, so you can confirm it's being set.
> 
> Sometimes yes, sometimes no ...

Okay, in order to avoid some the quirks of Publisher, let's give the 
module a unique name, like foo.py. Rename the index function to 
something else (I'll use "start" as an example). Avoid using the Alias 
while you're troubleshooting, and don't set the PythonPath for now. This 
gives us an URL like this:

  http://localhost/topdir/webdir/foo/start

Assuming cartair.py is in /topdir/webdir, it should be imported 
properly, because the directory in which mod_python is defined is 
prepended to the path (unless PythonPath is set).

You didn't mention the name of your published module, but it looks like 
you're calling it index.py. That's a bad habit you should break. 
Although it's offered as a convenience, it creates problems because 
newbies treat it like index.html and don't understand that you can only 
have one index.py per interpreter, regardless of the location. Published 
modules are imported into the root namespace and will cause collisions 
if all published module names are not unique. This can cause the erratic 
behaviour you describe. Used properly, it can allow you to use a module 
as the root of your site, but it's really just smoke and mirrors. To put 
it in a somewhat gross oversimplification, Publisher allows you to have 
only one index.py in your site.





More information about the Mod_python mailing list