I am trying to use python-decouple for sensitive data in my project but When i use decouple.config for SECRET_KEY it raises an error
error
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/admin/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/home/admin/.local/lib/python3.6/site-packages/django/core/management/__init__.py", line 345, in execute
settings.INSTALLED_APPS
File "/home/admin/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 76, in __getattr__
self._setup(name)
File "/home/admin/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 63, in _setup
self._wrapped = Settings(settings_module)
File "/home/admin/.local/lib/python3.6/site-packages/django/conf/__init__.py", line 142, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python3.6/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/admin/Legaland/src/sqh/settings/dev.py", line 1, in <module>
from ._base import *
File "/home/admin/Legaland/src/sqh/settings/_base.py", line 40, in <module>
SECRET_KEY = config("SECRET_KEY")
File "/home/admin/.local/lib/python3.6/site-packages/decouple.py", line 199, in __call__
return self.config(*args, **kwargs)
File "/home/admin/.local/lib/python3.6/site-packages/decouple.py", line 83, in __call__
return self.get(*args, **kwargs)
File "/home/admin/.local/lib/python3.6/site-packages/decouple.py", line 65, in get
value = self.repository[option]
File "/home/admin/.local/lib/python3.6/site-packages/decouple.py", line 113, in __getitem__
return self.parser.get(self.SECTION, key)
File "/usr/lib/python3.6/configparser.py", line 800, in get
d)
File "/usr/lib/python3.6/configparser.py", line 394, in before_get
self._interpolate_some(parser, option, L, value, section, defaults, 1)
File "/usr/lib/python3.6/configparser.py", line 444, in _interpolate_some
"found: %r" % (rest,))
configparser.InterpolationSyntaxError: '%' must be followed by '%' or '(', found: '%^e=(r2l0*73)4zxv!(!4x(%(_koxr049zlesn3"'
How shall i make it read SECRET_KEY as text
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = config("SECRET_KEY")
I am already I using .ini file for my email address and password the problem is that it tries to perform SECRET_KEY instead of accepting it as a raw text how can i prevent it
Right to the point, as said by Henrique Bastos, here:
Note: Since ConfigParser supports string interpolation, to represent the character % you need to escape it as %%.
To use python-decouple with a .ini file you can do this: Use your variable like said in the first answer:
SECRET_KEY = config(SECRET_KEY, default='some-random-text')
You can use cast, if needed, like:
DEBUG = config(DEBUG, default=True, cast=bool)# in the .ini file, the DEBUG value can be: 0, 1, True, False...
So for last, your traceback error show that there is a percent sign in your .ini string value, you may need to do a scape with a %% as said in the official documentation https://github.com/henriquebastos/python-decouple
Do a escape parser with double percents, instead just one and the error will be gone!