I shall simplify the code slightly in the hopes that someone can shed some light on this and maybe give me a more pythonic approach if what im doing is wrong.
Inside dbcontrollers.py
import autosettings
import aerialcontroller
import controller
import gobject
import gtk
import utils
# Create the settings object so we can call and edit settings easily
SETTINGS = autosettings.autoSettings()
CONTROLLERS = {}
class dBControllers(object):
def __init__(self):
# This works happily so nothing wrong in autosettings module
print SETTING.last_port
port_to_use = SETTINGS.last_port
if __name__ == "__main__":
# Create an object that holds all the GTK stuff (when not simplified)
dBControllers = dBControllers()
aerialController = aerialcontroller.aerialController()
Inside aerialcontroller.py
class aerialController(object):
def __init__(self):
self.motor_number = str(SETTINGS.aerial_motor_number)
CONTROLLERS[self.motor_number] = self
But when I run this, I get “NameError: global name ‘SETTINGS’ is not defined”.
I don’t really understand how this is supposed to work, sharing global variables accross modules. I assume this is not the correct way to do things like this, so can someone point me in the right direction.
Thank-you.
P.S. Simply code examples would be beneficial – still a relative newcomer to python.
Python doesn’t have a way to share a name across modules. If there’s something you want access to from multiple modules, you declare it in a separate module (e.g. “settings”), and then import it wherever you want it.
settings.py:
aerialcontroller.py
N.B. Doing
from settings import SETTINGSwill work as long as you only want to modify SETTINGS. If you want to completely replace it with a new set of settings (SETTINGS = ...) you need to do import settings, and refer to it assettings.SETTINGS.This is by design: if modules got variables from the script that imported them, it would be much harder to see where they came from when reading the code (and importing the module from somewhere else might not work). Doing it this way, you can always see where the variable came from within each file (well, almost always: please don’t overuse
from <name> import *).