[mod_python] Trouble importing files through mod_python

hungrybackspace at gmail.com hungrybackspace at gmail.com
Mon Jun 2 17:01:29 EDT 2008


Thanks, that does help a bit, but where we noticed it doesn't work is
if the ImportTest module imports modules that are nested deeper within
site-packages, which is the case for our production code.  (There is
the requisite __init__.py in each of the module directories.)

I guess what I find most puzzling is why everything works as expected
on the Ubuntu system and there's this difficulty on the CentOS system;
seems that the vital parts of the configuration are compatible.

Thanks again.
MC

On Mon, Jun 2, 2008 at 3:30 PM, Rob Shinn <rob.shinn at gmail.com> wrote:
> I think you have to either install the ImportTest in your Python
> site-packages directory or use apache.import_module().  If you wish to use
> apache.import_module(), you can do something like this:
>
> ImportTest=apache.import
> _module('ImportTest','full-path-to-~/scratch-here/ImportTest')
> TEST_CONSTANT=ImportTest.TEST_CONSTANT
>
> Note that if you don't use apache.import_module(), you need to restart
> Apache everytime you modify the ImportTest module.
>
> HTH
>
> On Mon, Jun 2, 2008 at 3:58 PM, hungrybackspace at gmail.com
> <hungrybackspace at gmail.com> wrote:
>>
>> 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