[mod_python] PythonPath and importer in 3.3

Martijn Moeling martijn at xs4us.nu
Tue Feb 6 13:19:07 EST 2007

I remember that there is a new directive to tell python/MP where the
modules are, sys.path should NOT be included, I looked it up in my
config and you should set the following line in your GLOBAL apache

PythonOption mod_python.importer.path ["/Users/me/Sites/www/python/"]

The pythonPath works with the old importer, and the above is Used in the
new 3.3 Importer

That should do the job for you...

Let me know if there are more questions


(I am trying to contribute as much as I can to help, especially whilst
Graham is away, I am a havy 3.3 user, but don't know everything either)

-----Oorspronkelijk bericht-----
Van: mod_python-bounces at modpython.org
[mailto:mod_python-bounces at modpython.org] Namens Jim Gallacher
Verzonden: Tuesday, February 06, 2007 18:30
Aan: Dirk van Oosterbosch, IR labs
CC: mod_python list
Onderwerp: Re: [mod_python] PythonPath and importer in 3.3

Dirk van Oosterbosch, IR labs wrote:
> Hi,
> I am not sure if this the right list to ask, since 3.3 is still in
> but after reading the documentation about PythonPath and import_module

> I'm running into problems as I try to follow the directions expressed
> the documentation.

3.3 is out of beta and will be released as 3.3.1. There are no changes 
from 3.3.0b except the version strings. We're just waiting for Grisha to

make the official announcement.

Graham is the guy that really understands the new importer, but he is 
heading off on holiday (or may in fact already be gone). In the worst 
case scenario you can revert to the 3.2.10 importer behaviour by using 
the following directive:

PythonOption mod_python.legacy.importer *

Hopefully we can muddle through before Graham gets back, but at least 
you won't be completely stuck.

I have no time to investigate right now but I'll try to take a look


> 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
> 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
> 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.
> :
>> 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
> I tried to change it according to the docs
> (these 
> )
> 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
> 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
> 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
> proper way to solve this issue?
> Thanks!
> dirk
> -----------------------------
> Dirk van Oosterbosch
> de Wittenstraat 225
> 1052 AT Amsterdam
> the Netherlands
> http://labs.ixopusada.com
> -----------------------------
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python

Mod_python mailing list
Mod_python at modpython.org

More information about the Mod_python mailing list