0

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
Crunchy
  • 186
  • 10
  • Look in the webserver error log for the error, and post it here. – John Gordon Jan 05 '23 at 03:11
  • @JohnGordon The error doesn't help much because it says the Azure client_id is wrong but when I test the file alone it works fine. Check my updates to the post. – Crunchy Jan 05 '23 at 03:17
  • Show us the code that gets the azure client id, and how it is used. – John Gordon Jan 05 '23 at 03:45
  • @JohnGordon that part does not matter because I have used the code elsewhere and test it, works fine. More importantly I cannot import ANY .py file into the settings.py. Not even a blank python file. That's a more serious issue. – Crunchy Jan 05 '23 at 17:27
  • If you're not willing to show the problem code, then I don't see how I can help further. Good luck. – John Gordon Jan 05 '23 at 17:32
  • @JohnGordon thats not what I am saying. I was just pointing out your focusing on a specific file (which I know works) when the bigger problem is I cannot import ANY files into settings.py. Does that make sense? I will happily show my test.py file if you want to see it still but do not see how it will help. Did not mean to upset you. I added the test.py file for you. I appreciate your help and replies. – Crunchy Jan 05 '23 at 17:40
  • The error message and your follow-up comments mention Azure a lot. Consider adding Azure to the tags and question title. Also, there are successful imports from `settings.py` exhibited here: https://stackoverflow.com/questions/4909958/django-local-settings Please look check the Azure configuration and research advice specific to that, e.g. https://learn.microsoft.com/en-us/answers/questions/904896/app-page-shows-34internal-server-error34-while-dep.html – Sarah Messer Jan 05 '23 at 17:48
  • The error is coming from the azure call, so the actual import did not cause an error. The problem is that the import isn't getting the _correct_ value, and we can only diagnose that problem if you show us all the code. Telling us "well I tried it in a test program and it worked" is not helpful. – John Gordon Jan 05 '23 at 18:49
  • @JohnGordon your not understanding. The error is misleading. It is not importing correctly. When I use the same azure code different file it works fine. When I try and import and use in settings.py it gives that error. So its not the azure call because it works elsewhere. Also, when I try and import ANY file like import testimport.py it gives a different error. It DOES NOT allow me to import any python file. Ignore the Azure because I am telling you I have tested it and it works. See my update I added for my test. – Crunchy Jan 05 '23 at 19:06
  • @SarahMesser see my updates, It's confirmed not an Azure issue. I have checked your links and I can do imports the same way as the link like "# try: # from working_settings_backup import * # except ImportError: # pass" but that does not fixing anything for I am trying to import a .py file not another settings.py file. Thank you for the reply though. – Crunchy Jan 05 '23 at 20:09
  • @JohnGordon To help clear confusion, I uploaded the error code I get when trying any other python file besides the Azure one to show it still fails with ANY .py file imported. – Crunchy Jan 05 '23 at 22:34
  • What if the import is `import .test` instead of `import test`? What if you print `PYTHONPATH` and `os.listdir(os.getcwd())` before the import? What if you rename `test.py` to something else so that you're not possibly colliding with Azure test modules or similar? Have you tried importing an installed python module, like `import re` or Azure might be modifying the environment variables, execution root directory or both. – Sarah Messer Jan 06 '23 at 17:30
  • @SarahMesser Thank you for your replies, I have tried all of that. The file is not actually called "test" I just made it that for security reasons here. I have tried doing other file names though. It will not import ANY .py file. No matter the name. I have confirmed PYTHONPATH and more. Sadly no luck. See my updated question for further info. Thanks for helping I appreciate you :) https://stackoverflow.com/questions/75026467/python-django-settings-py-module-not-found-error-module-is-just-local-py-file – Crunchy Jan 06 '23 at 18:19

3 Answers3

0

Not sure whether it works, but can you rename the file to __init__.py in mysite directory, and import test.py like following

from mysite import test
Reed_Xia
  • 1,300
  • 3
  • 17
  • 29
0

Whether absolute imports work depends on the directory the python interpreter is running from. I've had trouble with PYTHONPATH before too. Have you tried from . import test ?

Aves
  • 11
  • 5
0

@JohnGordon was correct with his hypothesis. The original error was caused by an Azure issue. More specifically. Enviornmental variabeles. Local enviornmental variables were set, however, the apache service has its own IP. Therefore, global variables needed to be set. Once I set the client id as a global variable in environmental variables it worked.

Crunchy
  • 186
  • 10