[mod_python] frustrating problem importing packages with mod_python

Graham Dumpleton grahamd at dscpl.com.au
Thu May 26 18:17:52 EDT 2005


You are running up against the last case I mentioned, namely:

> Further, even if "Directory" is used, if both the handler and
> the "steve" package are in a subdirectory of the directory for
> which PythonHandler is defined, it will not find "steve" as only
> the top most PythonHandler directory is added to the module search
> path.

Ie., because PythonHandler is for '/var/www', only that directory
is added to the Python module search path, not the "python" 
subdirectory.
Change the Directory directive to "var/www/python" and it should
work. Alternatively, move "steve" into '/var/www'. Ie., a module
import from a handler does not implicitly look in the same directory
as the handler first like when using Python normally.

Graham

On 27/05/2005, at 8:10 AM, Stephen Potenza wrote:

> Graham,
>
> Here is my directory structure:
>
> /var/www/python/
>                 index.py
>                 steve/
>                       __init__.py
>                       dummy.py
>
> sys.path (from my simple index.py publisher and run using the browser):
>
> /var/www/
> /usr/lib/python23.zip
> /usr/lib/python2.3
> /usr/lib/python2.3/plat-linux2
> /usr/lib/python2.3/lib-tk
> /usr/lib/python2.3/lib-dynload
> /usr/local/lib/python2.3/site-packages
> /usr/lib/python2.3/site-packages
>
> I'm not overriding the PythonPath directive and I'm using the main
> Apache configuration to enable use of the publisher:
>
>     <Directory /var/www/>
>         ...
>
>         SetHandler mod_python
>         PythonHandler mod_python.publisher
>         PythonDebug On
>     </Directory>
>
>
>
> Graham Dumpleton wrote:
>> Where is the "steve" package located in relation to the directory
>> containing the handler? What is your Python sys.path set to? Are
>> you overriding "PythonPath" directive? Are you using a ".htaccess"
>> file to enable use of publisher or the main Apache configuration
>> file? If the latter, have you used the "Directory" directive or
>> the "Location" directive to surround the mod_python definitions?
>>
>> Try and answer these questions. Also post what the part of your
>> Apache configuration looks like where you enable use of publisher
>> for mod_python.
>>
>> One warning, if you use "Location" in the Apache configuration
>> instead of "Directory", mod_python will not be able to add to the
>> Python module search path the physical directory for which the
>> PythonHandler directive is defined, ie., where your handler
>> potentially resides. As such, if your "steve" package is in a
>> subdirectory of your handler it will not be able to find it.
>> Further, even if "Directory" is used, if both the handler and
>> the "steve" package are in a subdirectory of the directory for
>> which PythonHandler is defined, it will not find "steve" as only
>> the top most PythonHandler directory is added to the module search
>> path.
>>
>> Anyway, explain better how your directories are laid out etc
>> and then we might be able to be more specific.
>>
>> Graham
>>
>>
>> On 27/05/2005, at 7:08 AM, Stephen Potenza wrote:
>>
>>> I setup a simple mod_python publisher and it's working perfectly.
>>>
>>> [index.py]
>>> def index():
>>>     return "hi from index"
>>>
>>> When I access it through http://localhost/python/, I see "hi from
>>> index"... everything works fine.
>>>
>>>
>>> However, when I try import a module from a package I created named
>>> "steve", I get the following error:
>>>
>>> -------------------------------------------------------------------
>>> Mod_python error: "PythonHandler mod_python.publisher"
>>>
>>> Traceback (most recent call last):
>>>
>>>   File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line
>>> 299, in HandlerDispatch
>>>     result = object(req)
>>>
>>>   File "/usr/lib/python2.3/site-packages/mod_python/publisher.py", 
>>> line
>>> 98, in handler
>>>     path=[path])
>>>
>>>   File "/usr/lib/python2.3/site-packages/mod_python/apache.py", line
>>> 457, in import_module
>>>     module = imp.load_module(mname, f, p, d)
>>>
>>>   File "/var/www/python/index.py", line 1, in ?
>>>     from steve import dummy
>>>
>>> ImportError: No module named steve
>>> -------------------------------------------------------------------
>>>
>>> [index.py]
>>> from steve import dummy
>>>
>>> def index():
>>>     return dummy.say()
>>>
>>> if __name__ == "__main__":
>>>     print index()
>>>
>>>
>>> The "steve" package/directory contains two files:
>>>
>>> - __init__.py (blank)
>>> - dummy.py
>>>
>>> [dummy.py]
>>> def say():
>>>     return "this is dummy!"
>>>
>>> When I run index.py from the command line, it works perfectly. Any
>>> ideas??
>>>
>>> Thanks,
>>> Steve
>>> _______________________________________________
>>> Mod_python mailing list
>>> Mod_python at modpython.org
>>> http://mailman.modpython.org/mailman/listinfo/mod_python
>>
>>
>
> -- 
> Stephen Potenza
> Potenza Consulting
>
> 916.207.0578
> steve at potenzaconsulting.com
> http://www.potenzaconsulting.com



More information about the Mod_python mailing list