How to share a variable globally in python program? -
i'm working on program has config. need use different config tests , normal running of program.
i manage software , tests using manage.py
script , tests under python manage.py test ...
(eg: python manage.py test all
, python manage.py test db
, ...)
to choose config did in __init__.py
of config:
is_testing = sys.argv[1] == 'test' if is_testing: app.config.own import testconfig config else: app.config.own import config
and after import config file.
but sometime test runner takes first place in argv test in argv[2] or run tests launching script directly , not manage.py
.
my question ways can share variable across full codebase?
thanks.
you extract determining config config class , have script accept cli arg of config path execute. allow config used.
python manage.py test --config=import.path.to.settings
then have map config loadding actual module. django exact thing settings
file, consult implementation details
also i'd def recommend using argsparse
, allow not have deal sys.argv[1]
provides type enforcement, documenation generation, positional , keyword arg support
Comments
Post a Comment