[mod_python] Module importing and classes

Graham Dumpleton graham.dumpleton at gmail.com
Fri Mar 28 04:45:56 EDT 2008


There should have been a section after that in the page returned in the browser.

Anyway, the problem is that you have a circular import, you shouldn't
do it as not only will you get this problem, but mod_python will
reimport both modules on every request even if you haven't changed
them because of how dependency management between modules works. What
you need to do is create a third file which contains the:

class WebGUI:
 def __init__(self):
   attribute = 0

definition and have webEdit.py import that so as to avoid the cycle.

Graham

On 28/03/2008, Dominique.Holzwarth at ch.delarue.com
<Dominique.Holzwarth at ch.delarue.com> wrote:
> Hi Graham
>
>  The following is the complete code of the 2 files.
>
>  WebGUI.py
>
> ********************************************************************************
>  from mod_python import apache
>
> webEdit = apache.import_module("~/test/GUI/Edit/webEdit.py")
>
>  WebGUIMessage = "Hello from WebGUI.py"
>
>  class WebGUI:
>   def __init__(self):
>     attribute = 0
>
>  def index(req):
>   myClassObject = webEdit.WebEdit()
>   return myClassObject.myAttribute
>  ********************************************************************************
>
>
>  webEdit.py
>  ********************************************************************************
>  from mod_python import apache
>
> webGUI = apache.import_module("~/test/GUI/WebGUI.py")
>
>
>  class WebEdit(webGUI.WebGUI):
>   def __init__(self):
>
>     self.myAttribute = 1
>
>  def index(req):
>   return webGUI.__dict__
>  ********************************************************************************
>
>  The (more or less complete) error page looks like this:
>
>  URI:            '/python/test/GUI/WebGUI.py'
>  Location:       None
>  Directory:      'C:/Program Files/Apache Software Foundation/Apache2.2/htdocs/python/'
>  Filename:       'C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\python\\test\\GUI\\WebGUI.py'
>  PathInfo:       ''
>
>  Phase:          'PythonHandler'
>  Handler:        'mod_python.publisher'
>
>  Traceback (most recent call last):
>
>   File "C:\Program Files\Python25\Lib\site-packages\mod_python\importer.py", line 1537, in HandlerDispatch
>     default=default_handler, arg=req, silent=hlist.silent)
>
>   File "C:\Program Files\Python25\Lib\site-packages\mod_python\importer.py", line 1229, in _process_target
>     result = _execute_target(config, req, object, arg)
>
>   File "C:\Program Files\Python25\Lib\site-packages\mod_python\importer.py", line 1128, in _execute_target
>     result = object(arg)
>
>   File "C:\Program Files\Python25\Lib\site-packages\mod_python\publisher.py", line 204, in handler
>     module = page_cache[req]
>
>   File "C:\Program Files\Python25\Lib\site-packages\mod_python\importer.py", line 1059, in __getitem__
>     return import_module(req.filename)
>
>   File "C:\Program Files\Python25\Lib\site-packages\mod_python\importer.py", line 296, in import_module
>     log, import_path)
>
>   File "C:\Program Files\Python25\Lib\site-packages\mod_python\importer.py", line 680, in import_module
>     execfile(file, module.__dict__)
>
>   File "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\python\test\GUI\WebGUI.py", line 6, in <module>
>     webEdit = apache.import_module("~/test/GUI/Edit/webEdit.py")
>
>   File "C:\Program Files\Python25\Lib\site-packages\mod_python\importer.py", line 296, in import_module
>     log, import_path)
>
>   File "C:\Program Files\Python25\Lib\site-packages\mod_python\importer.py", line 680, in import_module
>     execfile(file, module.__dict__)
>
>   File "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\python\test\GUI\Edit\webEdit.py", line 4, in <module>
>
>     class WebEdit(webGUI.WebGUI):
>
>  AttributeError: 'module' object has no attribute 'WebGUI'
>
>
> If I omit the 'webEdit = apache.import_module("~/test/GUI/Edit/webEdit.py")' inside 'WebGUI.py' as you mentioned I get a:
>
>   File "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\python\test\GUI\WebGUI.py", line 19, in index
>     myClassObject = webEdit.WebEdit()
>
>  NameError: global name 'webEdit' is not defined
>
>  Error...
>
>  And if I omit the 'webGUI = apache.import_module("~/test/GUI/WebGUI.py")' inside 'webEdit.py' I get:
>
>  File "C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\python\test\GUI\Edit\webEdit.py", line 4, in <module>
>     class WebEdit(webGUI.WebGUI):
>
>  NameError: name 'webGUI' is not defined
>
>  So that's why I thought I need to have both import statements...
>
>
>
>  -----Original Message-----
>  From: Graham Dumpleton [mailto:graham.dumpleton at gmail.com]
>  Sent: Donnerstag, 27. März 2008 20:52
>  To: Holzwarth, Dominique (Berne Bauhaus)
>  Cc: mod_python at modpython.org
>  Subject: Re: [mod_python] Module importing and classes
>
>  Does webGUI.py try and import webEdit and thus you have a circular import?
>
>  With PythonDebug On in configuration, what is the import dependency graph information that is included in HTTP error page response after the trace back information?
>
>  Graham
>
>



More information about the Mod_python mailing list