Clodoaldo
clodoaldo.pinto.neto at gmail.com
Sat Dec 2 04:46:19 EST 2006
2006/12/2, Graham Dumpleton <grahamd at dscpl.com.au>: > > On 02/12/2006, at 10:57 AM, Clodoaldo wrote: > > >> Ok, I did it and now this works: > >> > >> import constants as C > >> > >> But this does not work: > >> > >> from mod_python import apache > >> C = apache.import_module('constants.py') > >> > > > > Changed it to: > > > > from mod_python import apache > > C = apache.import_module('~/mod/constants.py') > > > > And it works. > > The apache.import_module() function is doing double duty in allowing > either a module name or a path for a file (including extension). For a > path it has to be able to be distinguishable from a module or module > within I should have tried it. This works: from mod_python import apache C = apache.import_module('constants') > package name. Thus, for a path, can only use: > > /some/path/module.py > ~/some/path/module.py > ./some/path/module.py > ../some/path/module.py > > For anything else it assumes it is a normal module import. For > 'constants.py' > that thus means it thinks you are wanting to import a submodule > called 'py' > from a package called 'constants'. > > There are a few other quirks with the new importer. The first is that > when a > path is specified, you don't actually need to use a .py extension. > This means > for example that the mpservlets package which uses a .mps extension > could > quite easily be rewritten to use the new module importer rather than > its own > and get all the benefit of the full depth checks on module reloading. > In other > words: > > module = apache.import_module("/some/path/servlet.mps") > > will work. > > This may be useful if you don't want to use a .py extension and want > to use > something else to hide the fact that Python is being used. > > Another strange characteristic of the new module importer, although this > also actually works in more recent Python versions if using __import__ > (), > although not perhaps on all platforms, is that one can use a '/' to > refer to a > module name in a subdirectory somewhere on the module search path. > > What this means is that if you had used: > > PythonOption mod_python.importer.path "['~/']" > > rather than: > > PythonOption mod_python.importer.path "['~/mod']" > > you could then have said: > > apache.import_module('mod/constants') > > Note that I haven't used a '.py' extension. In some way this is like > having a > package, but the big difference is that the 'mod' directory doesn't > have to be > an actual package, ie., no __init__.py file is required. > > This method can thus be used to create a namespace for modules still, > ie., > by using a subdirectory, but since a package isn't being used, automatic > module reloading still works. > > Since the same works with __import__() for some versions of Python, > falling back to using __import__() may be useful if you are trying to do > testing outside of mod_python. > > Graham > This topic is now a very complete tutorial on the new 3.3 importer. It can make a valuable article in the wiki, if properly cleaned. -- Clodoaldo Pinto Neto
|