python - Create constants using a "settings" module? -
i have done searches on google , here @ stackoverflow can not find looking for.
i relatively new python. looking create "settings" module various application specific constants stored.
here how wanting setup code
settings.py
constant = 'value'
script.py
import settings def func(): var = constant --- more coding --- return var
i getting python error stating: "global name 'constant' not defined.
i have noticed on django's source code settings.py file has constants named do. confused on how can imported script , referenced through application.
edit
thank answers! tried following:
import settings print settings.constant
i same error: importerror: cannot import name constant
the easiest way have settings module.
(settings.py)
constant1 = "value1" constant2 = "value2"
(consumer.py)
import settings print settings.constant1 print settings.constant2
when import python module, have prefix the variables pull module name. if know values want use in given file and not worried them changing during execution, can do
from settings import constant1, constant2 print constant1 print constant2
but wouldn't carried away last one. makes difficult people reading code tell values coming from. , precludes values being updated if client module changes them. 1 final way is
import settings s print s.constant1 print s.constant2
this saves typing, propagate updates , requires readers remember after s
settings module.
Comments
Post a Comment