I have a Python code for a GUI app that I need to migrate. Here’s the global structure (it’s a flat-layout):
Project/
|-- format_controllers/
| |-- __init__.py
| |-- controller1/
| |-- controller2/
|-- core/
| |-- vars.py
| |-- __init__.py (empty)
|-- Ctrl_App
The module vars.py
define global variables, including this one:
global appCache
appCache = None
Then in my __init__.py
file, there are many functions made to handle metadata about the main controllers
, and the appCache
var is finally initialized :
from core import vars
class AppCache():
# Initialisation
def __init__(self):
self.__dataTypeMetaDict = {}
...
# End of AppCache ========================================================
basevars.CtrlCache = ControllerCache()
Finally, in my Ctrl_App
(where there is import format_controllers
, I have lines like this:
metaInfo = vars.appCache.metaInfoForType(self.config.dataType)
To sum it up, in Ctrl_App
, functions from the __init__.py
file of the package format_controllers
are used on a global variable defined in a module from the core
package, initialized to None
, and reinitialized to something not null in the same __init__.py
.
My problem is that I get errors. Because appCache
is initialized to None
in the init from the controllers package, in my Ctrl_App
I get errors like this:
AttributeError: 'NoneType' object has no attribute 'metaInfoForType'
I’ve tried to redefine the variable in the beginning but at the end it’s the same. I don’t know why the __init__.py
file, where the variable is initialized, is not taken into account when I import the package.
Is there an easier, and cleaner way to do that to finally fix this error? I can patch it but just writing global appCache
and appCache = ControllerCache()
in all the modules that use this variable but
- I think it’s not clean at all
- I’m afraid it’s a very unstable solution
Plus there must have been a good reason for the developer to do that this way, unfortunately I can’t get in touch with them or with anybody that could know.
Important to note : The code was written in Python 2.7 and used a lot of deprecated librairies and functions.
axelle is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
3