I am trying to get a module to import, but only if an object of a certain class is called. For instance:
class One(object):
try:
import OneHelper
except ImportError:
pass
def __init__(self):
...
def blah(self):
OneHelper.blah()
This is called NameError: global name 'OneHelper' is not definedwhen the function is called One.blah(). So far, the only thing I've found that works is to import the module into the actual functions that use it. So:
class One(object):
def __init__(self):
...
def blah(self):
try:
import OneHelper
except ImportError:
pass
OneHelper.blah()
But I do not want to import the module into every function in which I want to use it, I want it to be available for the whole class, but only if an instance of this class is created. Sorry if I am not clear enough ...
source
share