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.pyscript , 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

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -