[mod_python] PythonPath and importer in 3.3

Dirk van Oosterbosch, IR labs labs at ixopusada.com
Tue Feb 6 11:56:31 EST 2007


Hi,

I am not sure if this the right list to ask, since 3.3 is still in  
beta, but after reading the documentation about PythonPath and  
import_module I'm running into problems as I try to follow the  
directions expressed in the documentation.

First off, let me state that when I upgraded to mod_python 3.3, I was  
happily surprised that everything just worked like before, with only  
changes visible in the log file. However, as I tried to fix something  
(PythonPath and importing modules) which didn't seem to cause  
problems, things broke down and I can't get them to work according to  
the docs.
"Why trying to fix it then, when there was no problem?", you ask.  
Good question ;-)

My initial setup was like this:
in httpd.conf:
====================
<IfModule mod_python.c>
	PythonPath "sys.path + ['/Users/me/Sites/www/python/']"
	PythonTransHandler translate
...

in the .htaccess file in .../www/python/:
====================
AddHandler mod_python .py
PythonHandler main

Then in main.py (the main handler which is called for every request):
====================
# for loading a Cheetah template
templatesFolderLocation = "/Users/me/Sites/www/python/templates"
if templatesFolderLocation not in sys.path:
	sys.path.append(templatesFolderLocation)
...
modulename = 'page'
...
try:
	module = __import__(modulename, globals(), locals(), [modulename])
except ImportError:
	...
templateClass = getattr(module, name)


As said, this code works. Though it is not very nice and clean, and I  
understood I had to go this way, while I was waiting for mod_python  
3.3. Now I got python 3.3 and I read the documentation about  
apache.import_module() and about PythonPath, and I want to change the  
code so it will make proper use of the new importer.

The first thing I noticed in the docs is that the PythonPath  
shouldn't contain paths which are in the document tree.
http://www.modpython.org/live/mod_python-3.3.0b/doc-html/app-changes- 
from-3.2.10.html :
> The PythonPath directive MUST not be used to point at directories  
> within the document tree.

So I changed the PythonPath line in httpd.conf into
	PythonOption mod_python.importer.path "['/Users/dirk/Sites/www/ 
python']"

But –as somewhat expected– this breaks the Cheetah module importing  
code.
I tried to change it according to the docs
(these http://www.modpython.org/live/mod_python-3.3.0b/doc-html/pyapi- 
apmeth.html )

So now the code in main.py looks like this:
====================
# for loading a Cheetah template
templatesFolderLocation = "/Users/me/Sites/www/python/templates"
# not appending to sys.path anymore
...
modulename = 'page'
...
try:
	module = apache.import_module(modulename, path= 
[templatesFolderLocation])
except ImportError:
	... # importing the module fails here!
templateClass = getattr(module, name)


The reason for the failing import seems to be the inheritance system  
of the templates. Inside the chain of Cheetah modules that are  
inheriting eachother, there is a pure python module (framework.py),  
residing in /www/python (not in /www/python/templates/ !) which tries  
to import a module from /www/python/templates/ through this line:
	from baseTemplate import baseTemplate
I believe this fails and is the origin of my new problem.

I thought the module search path, which I supplied with the path=  
argument in import_module() was copied forward to all subsequent  
imports, but it seems this does not work for me.

Now I would be happy to add the templates directory to the python  
path again, but as I understood this is not the proper new mod_python  
way.
However I am reluctant to change the importing mechanism in  
framework.py and make framework dependant on mod_python's apache,  
since I also want to be able to test the cheetah part of my system  
outside of apache.

I hope I am missing something obvious. What would be the most clean  
and proper way to solve this issue?

Thanks!
dirk





-----------------------------
Dirk van Oosterbosch
de Wittenstraat 225
1052 AT Amsterdam
the Netherlands

http://labs.ixopusada.com
-----------------------------

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mm_cfg_has_not_been_edited_to_set_host_domains/pipermail/mod_python/attachments/20070206/f462ef3b/attachment.html


More information about the Mod_python mailing list