I have created a separate file test.py to interface with Azure Key Vault. When I run just the test.py file it works as expected. However, I cannot import it in settings.py for its values to be used there. I cannot import any file into settings.py for that matter or it causes an internal 500 error on Apache.
For instance I want to add these lines to my settings.py
import test
Pass = test.Pass
However, when adding that and restarting the Apache server is gives an error 500 page until I remove the lines and restart. test.py has no syntax errors because I can run it on its own and produce the result I am looking for but bringing any file into settings.py causes the crash. The error logs have been no help. Why would I not be able to import a file into settings.py?
The file I am importing is successful a part of the PYTHONPATH variable and I have checked that by printing sys.path. Also the file is located in the same directory as settings.py
mysite/
settings,py
test.py
urls.py
wsgi.py
init.py
The error from the server logs reads
ValueError: client_id should be the id of an Azure Active Directory application\r, referer: http://test.example.com/test/login/
However, this when I test the file individually it works as expected successfully connecting to Azure and doing the work it needs a retrieves and prints a correct password. This error is only caused when importing it in setting.py
Test.py file
from os import environ as env
from azure.identity import ClientSecretCredential
from azure.keyvault.secrets import SecretClient
TENANT_ID = env.get("AZURE_TENANT_ID", "")
CLIENT_ID = env.get("AZURE_CLIENT_ID", "")
CLIENT_SECRET = env.get("AZURE_CLIENT_SECRET", "")
KEYVAULT_NAME = env.get("AZURE_KEYVAULT_NAME", "")
KEYVAULT_URI = f"https://{KEYVAULT_NAME}.vault.azure.net/"
_credential = ClientSecretCredential(
tenant_id=TENANT_ID,
client_id=CLIENT_ID,
client_secret=CLIENT_SECRET
)
_sc = SecretClient(vault_url=KEYVAULT_URI, credential=_credential)
Pass = _sc.get_secret("Pass").value
IMPORTANT NOTE
The Azure call is not the problem, its the import. When I try the Azure code in standalone tests in other files by doing:
import test
Pass = test.Pass
print(pass)
it prints the correct password. Hence why the Azure code is NOT the problem and should be focused on. The problem is I cannot import ANY .py file into settings.py without it causes error. For instance, if I try and import in any python file this is the error I get:
import test\r
ModuleNotFoundError: No module named 'test'\r