[mod_python] Trouble importing files through mod_python

Graham Dumpleton graham.dumpleton at gmail.com
Mon Jun 2 17:41:43 EDT 2008


You PythonPath is wrong for a start and would cause various issues,
although not the immediate one here. You have:

PythonPath "['/home/me/scratch', '/home/me/scratch/ImportTest',
'/usr/lib/python2.5/', '/usr/lib/python2.5/lib-dynload/',
'/usr/lib/python2.5/site-packages/']"

It should be:

PythonPath "['/home/me/scratch', '/home/me/scratch/ImportTest'] + sys.path"

That is, you need to extend existing path, not replace it.

Other than that, problems like this are generally due to the user that
Apache runs as not being able to read files being imported. Thus,
check whether permissions on your account directory and stuff in it
are too restrictive and wouldn't allow another user to read the files
contained there in.

Graham

2008/6/3 hungrybackspace at gmail.com <hungrybackspace at gmail.com>:
> Greetings,
>
> We're having a bit of a time importing modules that aren't in the
> standard library while using mod_python on this particular server.  We
> have had, however, success with other servers.  We're trying to run a
> tiny application that looks like this:
>
>
> on the console side:
> ~/scratch/
>          RunImportTest.py
>          __init__.py
>          ImportTest/
>                     ImportTest.py
>                     __init__.py
>
>
> ...and the web side:
> /home/httpd/html/beta/
>                      RunImportTestModPython.py
>
>
> Below, the contents of the three files:
>
> RunImportTest.py:
> =================
> from ImportTest.ImportTest import TEST_CONSTANT
>
> print TEST_CONSTANT
>
>
> RunImportTestModPython.py:
> ==========================
> from mod_python import apache
> from ImportTest.ImportTest import TEST_CONSTANT
>
> def handler(req):
>    req.content_type = 'text/plain'
>    req.write(TEST_CONSTANT)
>    return apache.OK
>
>
> ImportTest/ImportTest.py:
> =========================
> TEST_CONSTANT = "success"
>
>
> Output from the import test from command line:
> ~/scratch> python RunImportTest.py
> success
>
> So we know the little program does indeed work.  Here is the output
> from Apache via mod_python:
>
> ========================
> MOD_PYTHON ERROR
>
> ProcessId:      18704
> Interpreter:    'beta.company.com'
>
> ServerName:     'beta.company.com'
> DocumentRoot:   '/home/httpd/html/beta'
>
> URI:            '/RunImportTestModPython.py'
> Location:       None
> Directory:      '/home/httpd/html/beta/'
> Filename:       '/home/httpd/html/beta/RunImportTestModPython.py'
> PathInfo:       ''
>
> Phase:          'PythonHandler'
> Handler:        'RunImportTestModPython'
>
> Traceback (most recent call last):
>
>  File "/usr/local/lib/python2.5/site-packages/mod_python/importer.py",
> line 1537, in HandlerDispatch
>    default=default_handler, arg=req, silent=hlist.silent)
>
>  File "/usr/local/lib/python2.5/site-packages/mod_python/importer.py",
> line 1202, in _process_target
>    module = import_module(module_name, path=path)
>
>  File "/usr/local/lib/python2.5/site-packages/mod_python/importer.py",
> line 296, in import_module
>    log, import_path)
>
>  File "/usr/local/lib/python2.5/site-packages/mod_python/importer.py",
> line 680, in import_module
>    execfile(file, module.__dict__)
>
>  File "/home/httpd/html/beta/RunImportTestModPython.py", line 2, in <module>
>    from ImportTest.ImportTest import TEST_CONSTANT
>
> ImportError: No module named ImportTest.ImportTest
>
>
> MODULE CACHE DETAILS
>
> Accessed:       Mon Jun  2 14:21:49 2008
> Generation:     0
>
> _mp_df6c4429391f4fb65dc56704a2c1e0e2 {
>  FileName:     '/home/httpd/html/beta/RunImportTestModPython.py'
>  Instance:     1 [IMPORT]
>  Generation:   0 [ERROR]
>  Modified:     Mon Jun  2 11:49:11 2008
> }
>
>
> ...and here's an .htaccess file we're using in the directory
> /home/httpd/html/beta, the directory that RunImportTestModPython.py is
> in:
> =========================
> Options Indexes FollowSymLinks MultiViews
> AddHandler mod_python .py
> PythonDebug On
> PythonHandler RunImportTestModPython
> PythonAutoReload On
> PythonPath "['/home/me/scratch', '/home/me/scratch/ImportTest',
> '/usr/lib/python2.5/', '/usr/lib/python2.5/lib-dynload/',
> '/usr/lib/python2.5/site-packages/']"
>
>
> ...where /home/me is the user directory referenced at the top of the message.
>
> It's quite likely we're overlooking something very simple--but this
> same setup works without hitch on another system (granted, versions of
> OS, Apache, mod_python and Python are slightly different...).  This
> non-functioning setup is:
>
> * CentOS 5
> * Apache 2.2.8
> * PHP 5.2.6
> * Python 2.5.2
> * mod_python 3.3.1
> * cx_oracle 4.3.3
>
> some of those are kind of irrelevant for the specific application
> here, but there might be hidden conflicts that I'm not aware of. I
> have read some stuff about using mod_python's native import methods,
> and I'd very much like to use the standard python import method
> instead, as it preserves command-line execution without having to add
> a lot of overhead.  And in fact, I shouldn't have to because this
> setup works on another server.  Unfortunately, I can't simply use the
> other server...
>
> Hope this isn't all too much information--I'm very grateful for any
> help anyone may provide.
>
> many thanks,
> MC
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
>


More information about the Mod_python mailing list