By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,092 Members | 1,650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,092 IT Pros & Developers. It's quick & easy.

Question about the "new" module

P: n/a
Hi,

I'm using Python 2.5 to develop a simple MVC framework based on
mod_python. To load my controllers, I create new modules using the
"new" module like this:

# ....
my_module = new.module("random_name")
my_module.__file__ = module_path

exec open(module_path, "r") in my_module.__dict__

then I initialize the class defined inside the module and call a method
of this class based on the HTTP request.

All works fine but the imports. If my module contains something like
this:

import something

class MyController(Controller):
def index(self):
something.do_something()

when MyController.index is called an exception is raised "NoneType
object has not attribute do_something". Why does it happen ? I have to
load the module in different ways (but I'd like to force the reload
every time the module is loaded) ?

Gabriele

Dec 29 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

GabrieleI'm using Python 2.5 to develop a simple MVC framework based
Gabrieleon mod_python. To load my controllers, I create new modules
Gabrieleusing the "new" module like this:

Gabriele# ....
Gabrielemy_module = new.module("random_name")
Gabrielemy_module.__file__ = module_path
Gabrieleexec open(module_path, "r") in my_module.__dict__

Gabrielethen I initialize the class defined inside the module and call
Gabrielea method of this class based on the HTTP request.

Why use the new module? Why not call __import__() or execfile()? Details
on their use are here:

http://docs.python.org/dev/lib/built-in-funcs.html

Skip
Dec 29 '06 #2

P: n/a

sk**@pobox.com wrote:
GabrieleI'm using Python 2.5 to develop a simple MVC framework based
Gabrieleon mod_python. To load my controllers, I create new modules
Gabrieleusing the "new" module like this:

Gabriele# ....
Gabrielemy_module = new.module("random_name")
Gabrielemy_module.__file__ = module_path
Gabrieleexec open(module_path, "r") in my_module.__dict__

Gabrielethen I initialize the class defined inside the module and call
Gabrielea method of this class based on the HTTP request.

Why use the new module? Why not call __import__() or execfile()? Details
on their use are here:

http://docs.python.org/dev/lib/built-in-funcs.html
Or why not use mod_python.apache.import_module() from mod_python
itself. It is designed as a way of users importing specific modules,
including the capability for modules to be reloaded if they have been
changed. See:
http://www.dscpl.com.au/wiki/ModPyth...oduleImporting

as well as the mod_python documentation.

Is recommended though to use mod_python 3.3 as soon as you can though
if you want the module reloading to always work. You can find
descriptions of problems with the module reloading in older versions of
mod_python at:
http://www.dscpl.com.au/wiki/ModPyth...ortingIsBroken

Note that module importer in mod_python 3.3 has been enhanced some what
over prior versions in addition to fixing problems. An important new
feature is being able to load modules based on their path with modules
with same names in different directories being properly distinguished,
something which is quite useful where modules are used in web
applications to represent pages. You may want to look at documentation
for it at:
http://www.modpython.org/live/mod_py...pi-apmeth.html

Graham

Dec 30 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.