Problem calling python modules from python apache handler

Graham Dumpleton Graham.Dumpleton at gmail.com
Tue Oct 16 19:39:56 EDT 2007


On Oct 17, 8:00 am, mannewalis <justinagra... at gmail.com> wrote:
> Hi,
>
> I have the following setup...
>
> Apache 2.2.3, Python 2.5 and mod_python 3.3.1
>
> I have configured apache to call a python script when fetching .py
> resources...so far so good... I did the hello world script in the
> mod_python docs and it works great.
>
> Now when I want to add some meat and potatoes and call some other
> python code from the script things get pear shaped.. I try to import
> the module I want, but it never can find it... I always get the
> error...
>
> ImportError: No module named ???
>
> no matter what I do. I have moved my python modules around, to no
> avail. I've even put them in the same directory as the handler script
> and even that doesn't work.
>
> I am fairly new to python, so I expect there is some search-path-
> fu(tm) that I am presently unaware of. Can anybody explain how to get
> this working?
>
> Thanks
> Justin
>
> For example...
>
> I have the following folders...
>
> \python25\lib\test\
>
> with the script mytest.py
>
> In my apache hander I try to import this script
>
> from test import mytest

First, done call your package directory 'test'. There is a module in
Python called that and if it finds the Python module first it will not
find your package.

Second, you can't dump Python packages in the same directory as your
mod_python handler code file, they will not be found. Place them
outside of the Apache document tree and use PythonPath directive as
described in documentation to indicate where they are located.

Also ensure you read documentation for import_module() in:

  http://www.modpython.org/live/current/doc-html/pyapi-apmeth.html

This covers issues around module loading in mod_python.

Graham


> and get the error..
>
> MOD_PYTHON ERROR
>
> ProcessId:      4988
> Interpreter:    'localhost'
>
> ServerName:     'localhost'
> DocumentRoot:   'C:/Program Files/EasyPHP 2.0b1/www'
>
> URI:            '/foo.py'
> Location:       None
> Directory:      'C:/Program Files/EasyPHP 2.0b1/www/'
> Filename:       'C:/Program Files/EasyPHP 2.0b1/www/foo.py'
> PathInfo:       ''
>
> Phase:          'PythonHandler'
> Handler:        'query'
>
> Traceback (most recent call last):
>
>   File "C:\Python25\lib\site-packages\mod_python\importer.py", line
> 1537, in HandlerDispatch
>     default=default_handler, arg=req, silent=hlist.silent)
>
>   File "C:\Python25\lib\site-packages\mod_python\importer.py", line
> 1202, in _process_target
>     module = import_module(module_name, path=path)
>
>   File "C:\Python25\lib\site-packages\mod_python\importer.py", line
> 296, in import_module
>     log, import_path)
>
>   File "C:\Python25\lib\site-packages\mod_python\importer.py", line
> 680, in import_module
>     execfile(file, module.__dict__)
>
>   File "C:\Program Files\EasyPHP 2.0b1\www\query.py", line 2, in
> <module>
>     from test import mytest
>
> ImportError: No module named test
>
> MODULE CACHE DETAILS
>
> Accessed:       Tue Oct 16 14:50:22 2007
> Generation:     4
>
> _mp_9bae44ea3d2a76746732f5d982e3e12d {
>   FileName:     'C:\\Program Files\\EasyPHP 2.0b1\\www\\query.py'
>   Instance:     13 [RELOAD]
>   Generation:   4 [ERROR]
>   Modified:     Mon Oct 15 19:54:19 2007
>   Imported:     Mon Oct 15 19:50:44 2007
>
> }





More information about the Python-list mailing list