I am writing a minimal replacement for mod_python's publisher.py
The basic premise is that it is loading modules based on a URL scheme:
/foo/bar/a/b/c/d
Whereby /foo/ might be a directory and 'bar' is a method ExposedBar in a publishable class in /foo/index.py. Likewise /foo might map to /foo.py and bar is a method in the exposed class. The semantics of this aren't really important. I have a line:
sys.path.insert(0, path_to_file) # /var/www/html/{bar|foo}
mod_obj = __import__(module_name)
mod_obj.__name__ = req.filename
Then the module is inspected for the appropriate class/functions/methods. When the process gets as far as it can the remaining URI data, /a/b/c is passed to that method or function.
This was working fine until I had /var/www/html/foo/index.py and /var/www/html/bar/index.py
When viewing in the browser, it is fairly random which 'index.py' gets selected, even though I set the first search path to '/var/www/html/foo' or '/var/www/html/bar' and then loaded __import__('index'). I have no idea why it is finding either by seemingly random choice. This is shown by:
__name__ is "/var/www/html/foo/index.py"
req.filename is "/var/www/html/foo/index.py"
__file__ is "/var/www/html/bar/index.py"
This question then is, why would the __import__ be randomly selecting either index. I would understand this if the path was '/var/www/html' but it isn't. Secondly:
Can I load a module by it's absolute path into a module object? Without modification of sys.path. I can't find any docs on __import__ or new.module() for this.
-
Are you reinventing TurboGears? turbogears.orgS.Lott– S.Lott2009年05月14日 13:24:27 +00:00Commented May 14, 2009 at 13:24
-
Nope, just looking for a lean publisher that will do exactly what I require, no more, no less. And getting my hands dirty like this is a good way to learn Python.Aiden Bell– Aiden Bell2009年05月14日 13:31:17 +00:00Commented May 14, 2009 at 13:31
-
Actually, my question was rhetorical. You might want to look very closely at what they're doing, it might save you a lot of time and effort.S.Lott– S.Lott2009年05月14日 13:37:17 +00:00Commented May 14, 2009 at 13:37
-
Looking at the site, it seems like overkill for what i'm afterAiden Bell– Aiden Bell2009年05月14日 13:39:36 +00:00Commented May 14, 2009 at 13:39
-
2Most new things seem like overkill until you get further into your project and discover you needed those features. Using an existing framework often works out better in the long run because you can focus your energy on the "value creating" part of the job instead of the "reinventing someone else's wheel" part of the job.S.Lott– S.Lott2009年05月14日 14:07:44 +00:00Commented May 14, 2009 at 14:07
1 Answer 1
Can I load a module by it's absolute path into a module object? Without modification of sys.path. I can't find any docs on
__import__or new.module() for this.
import imp
import os
def module_from_path(path):
filename = os.path.basename(path)
modulename = os.path.splitext(filename)[0]
with open(path) as f:
return imp.load_module(modulename, f, path, ('py', 'U', imp.PY_SOURCE))