I have three modules:
constants
, which contains loaded configuration and other stuff in a classmain
, which initializesconstants
when it is runuser
, which importsconstants
and accesses its configuration.
constants
module (simplified) looks like this:
class Constants:
def __init__(self, filename):
# Read values from INI file
config = self.read_inifile(filename)
self.somevalue = config['ex']['ex']
def read_inifile(self, filename):
# reads inifile
constants: Constants = None
def populate_constants(filename):
global constants
constants = Constants(filename)
A simple object which should hold the configuration, nothing groundbreaking.
The function populate_constants()
is called from main
on program startup.
Now the weirdness happens - when I import the constants
module from user
like this, the constants
is None
:
from toplevelpkg.constants import constants
print(constants)
None
However, if I import it like this, constants
is initialized as one would expect:
from toplevelpkg import constants
print(constants.constants)
<trumpet.constants.Constants object at 0x035AA130>
Why is that?
EDIT: in my code, the function attempting to read constants
is run asynchronously via await loop.run_in_executor(None, method_needing_import)
. Not sure if this may cause issues?
(and as a side-question, is it a good practice to have a configuration-holding object which parses the config file and provides it as its member variables?)