Problem calling python modules from python apache handler

Discussion in 'Python' started by mannewalis, Oct 16, 2007.

  1. mannewalis

    mannewalis Guest

    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

    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
    }
     
    mannewalis, Oct 16, 2007
    #1
    1. Advertising

  2. On Oct 17, 8:00 am, mannewalis <> 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
    >
    > }
     
    Graham Dumpleton, Oct 17, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shruds
    Replies:
    1
    Views:
    871
    John C. Bollinger
    Jan 27, 2006
  2. Replies:
    1
    Views:
    727
    Damien
    Feb 22, 2007
  3. joe jacob
    Replies:
    3
    Views:
    711
    joe jacob
    Aug 14, 2007
  4. Scooter

    Python Apache Handler

    Scooter, Jan 9, 2009, in forum: Python
    Replies:
    2
    Views:
    617
    Sells, Fred
    Jan 9, 2009
  5. tanmoy.b81
    Replies:
    0
    Views:
    902
    tanmoy.b81
    Feb 4, 2009
Loading...

Share This Page