[mod_python] PythonPath and importer in 3.3

Graham Dumpleton grahamd at dscpl.com.au
Tue Feb 6 15:50:07 EST 2007


Delayed boarding ...

Reading again I see you already did what I was saying, I think though
you just need to list both directories in new import path.

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

The 'path' argument to import_module isn't inherited down. For a
module import by name, only used for top level module search, not
child module imports from that module. The 'path' only gets embedded
in top level module where absolute path for module and not module
name used. Again, this is not passed down through to child imports,
you would need to do that manually by passing __mp_path__ down to
import_module() as described in documentation. This will be no good
in your case because Cheetah generates 'import'.

BTW, where is framework.py. Is it in your '.../www.python' directory
and thus would it be getting imported by mod_python importer even if
by 'import' rather than import_module()? If it is, that it may be
using 'import' should be okay.

Look at the log files to see when stuff is being imported by mod_python
as opposed to getting pushed onto Python itself.

Anyway, plane now loading so have to go.

If you still can't get it going explain the chain of imports including
framework.py, where the files are located and how they are being
imported if you didn't fully already.

Bye.

Graham

Graham Dumpleton wrote ..
> Very quickly as plane goes in about an hour..... :-)
> 
> See comments below.
> 
> Dirk van Oosterbosch, IR labs wrote ..
> > 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.
> 
> This is only really an issue if the modules in the directory in the
> document tree are being imported directly by mod_python importer
> by reference in Python*Handler, by mod_python.publisher or by
> direct use by apache.import_module(). If the modules in said
> directory aren't being included in this way, it is okay to list
> a subdirectory somewhere in the document tree in PythonPath.
> 
> The important thing is that you want to avoid having modules imported
> through both mechanisms.
> 
> One step to ensure that anything in a directory isn't used by mod_python
> in some way inadvertantly by mod_python.publisher, is to add into the
> directory a .htaccess file containing:
> 
>   deny from all
> 
> If using own handler, not so big a deal.
> 
> I have managed to get Cheetah to use mod_python importer from within its
> generated code before. The trick is to use import_module() instead of
> __import__ to import the Cheetah template top level code file.
> 
> Ie., don't use:
> 
>   module = __import__(modulename, globals(), locals(), [modulename])
> 
> use:
> 
>   module = apache.import_module('/actual/path.py')
> 
> Then have the templates directory in:
> 
>   PythonOption mod_python.importer.path ['/actual/path/to/templates']
> 
> Try playing with that. If after my nine hour flight to next airport
> I find some free internet access, I'll have a look to see how you went.
> 
> Anyway, hope I didn't muddle that because of being in a hurry.
> 
> :-) :-) :-) :-)
> 
> Graham
> 
> 
> > 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 &#8211;as somewhat expected&#8211; 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
> > -----------------------------
> _______________________________________________
> 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