Graham Dumpleton
graham.dumpleton at gmail.com
Tue Jun 3 02:51:01 EDT 2008
Also read the documentation. See 'import_module()' under: http://www.modpython.org/live/current/doc-html/pyapi-apmeth.html This can be used directly, but is also what PythonHandler directive uses to import top level handler module. Graham 2008/6/3 <Dominique.Holzwarth at ch.delarue.com>: > Importing packaged/modules under mod_python is a little bit complicated (imo) cause mod_python tries to make a difference between then standard system python search path and a own, mod_python specific search path for modules. > For full reference of the problem see: http://www.modpython.org/pipermail/mod_python/2006-November/022726.html > > As a quick (but not complete) summary here's what I've used so far: > > 1. put your packages as "normal" python packages under <python-dir>/Lib/site-packages, then you can import them the same way as you would do with the console. However, if anything changes, you need to restart apache (takes like 1 sec ;-)) > > 2. put your packages under the apache (python-) document root. If you configured apache to search for *.py files under ../htdocs/python and your packages are inside that directory then you can import them like this: > > from mod_python import apache > someName = apache.import_module(~/package-name/module-name.py) > > Note that "someName" can be choosen whatever you want. The "~" points to the python document root. > Using that method you don't need a __init__.py file to reflect the package structure btw. Also, using this method changes inside the packages are reloaded on-the-fly by apache without the need to restart it. It is however possible to access the *.py files through the web if apache is not properly configured (not sure if you can hide them fully tbh). > > 3. put your packages ANYWHERE outside the apache document root AND outside of site-packages. > In this case you need to set the mod_python importer path. Packages/modules in this path aren't automaticly reloaded, so after a change, you need to restart apache: > > PythonOption mod_python.importer.path "['some/path/to/my/Module.py', 'some/other/path/to/a/directory/with/modules']" > > This statement goes into apache configuration file (httpd.conf or .htsomething ;-)) > Importing these packages/modules should work the same as you do from within the console. I'm not totally sure about that tho as I used this method only to import modules - never packages (so I'm not sure wether "import package.subpackage.module" works...) > > Greetings > dominique > >> -----Original Message----- >> From: hungrybackspace at gmail.com [mailto:hungrybackspace at gmail.com] >> Sent: Montag, 2. Juni 2008 21:59 >> To: mod_python at modpython.org >> Subject: [mod_python] Trouble importing files through mod_python >> >> 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 >> >> > > ***************************************************************************** > This e-mail and any files attached are strictly confidential, may be legally > privileged and are intended solely for the addressee. If you are not the > intended recipient please notify the sender immediately by return email and > then delete the e-mail and any attachments immediately. > > The views and or opinions expressed in this e-mail are not necessarily the > views of De La Rue plc or any of its subsidiaries and the De La Rue Group > of companies, their directors, officers and employees make no representation > about and accept no liability for its accuracy or completeness. > > You should ensure that you have adequate virus protection as the De La Rue > Group of companies do not accept liability for any viruses. > > De La Rue plc Registered No.3834125, De La Rue Holdings plc Registered > No 58025 and De La Rue International Limited Registered No 720284 are all > registered in England with their registered office at: > De La Rue House, Jays Close, Viables, Hampshire RG22 4BS > ***************************************************************************** > > > _______________________________________________ > Mod_python mailing list > Mod_python at modpython.org > http://mailman.modpython.org/mailman/listinfo/mod_python >
|