I recently switched a Django project from sqlite3 to postgres. I’d like to read db password from a module placed in a hidden directory.
.secrets
__init__.py
db.py # DB_PASSWORD = 'mypassword'
How do I import DB_PASSWORD from the db module placed in the hidden .secrets directory? The following doesn’t work (ImportError):
from .secrets.db import DB_PASSWORD
Do I have to use the __import__ trick? For code styling conventions I’d prefer to stay with the from/import pattern.
I found a formal answer in the Python reference.
The
fromforms are:or
In the first form the dot has nothing to do with the wrong concept of “hidden packages”, it refers to relative imports (as mentioned by Thomas Orozco). In the second form the module is defined as follow:
and identifier must start with a letter.
As you suggested I’ll use an external configuration file, not Python code.