What is the consensus on the best way reference module level variables? I have a wx app that has grown too large, so I started to do some housekeeping. I'm trying to move several functions outside of the main module while still being able to reference the main variables.. The program works if I include

from __main__ import *

at the top of the child modules, however it only works once. If I run the script without completely closing all python windows it raises

Traceback (most recent call last):
  File "C:\Python25\Lib\roster\roster_main.pyw", line 427, in Login
  File "C:\Python25\Lib\roster\Conform.py", line 197, in Run
  File "C:\Python25\lib\site-packages\wx-2.8-msw-unicode\wx\_core.py", line 14528, in __getattr__
    raise PyDeadObjectError(self.attrStr % self._name)
wx._core.PyDeadObjectError: The C++ part of the MainFrame object has been deleted, attribute access no longer allowed.

Is there a better way to do this?

8 Years
Discussion Span
Last Post by Gribouillis

.. Okay. I'm just not quite sure of where to start, I'm sure you've had some experience with this. I've looked at several other package modules and have really only seen explicit import calls ( importing each variable by name )

Edited by ihatehippies: n/a


from __main__ import * must not be written if you want to write maintainable code. What you should do is first see if some parts of your program can be implemented as separate modules, at functional level (for example a module could be devoted to communicate with a database, etc). The variables that you have in __main__ could become members of a single object which could be passed to functions and classes in other modules.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.