I have created a separate filetest.pyto interface with Azure Key Vault. When I run just thetest.pyfile it works as expected. However, I cannot import it insettings.pyfor 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
但是,当添加并重新启动Apache服务器时,会出现错误500页,直到我删除这些行并重新启动。test. py没有语法错误,因为我可以自己运行它并产生我要的结果,但将任何文件引入settings. py会导致崩溃。错误日志没有帮助。为什么我不能将文件导入settings. py?
我正在导入的文件是PYTHONPATH变量的一部分,我已经通过打印sys.path进行了检查。此外,该文件与settings. py位于同一目录中
我的网站/
设置,py
test.py
urls.py
wsgi.py
- 姓名首字母py**
服务器日志中的错误如下
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
重要说明
Azure调用不是问题,问题在于导入。当我在其他文件中的独立测试中尝试Azure代码时,请执行以下操作:
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
3条答案
按热度按时间wh6knrhe1#
不确定是否有效,能否在
mysite
目录下将文件重命名为__init__.py
,然后像下面这样导入test.py
f2uvfpb92#
绝对导入是否有效取决于python解释器运行的目录,我以前也遇到过
PYTHONPATH
的问题,你试过from . import test
吗?mu0hgdu03#
@JohnGordon的假设是正确的。最初的错误是由Azure问题引起的。更具体地说,是环境变量。设置了本地环境变量,然而,Apache服务有自己的IP。因此,需要设置全局变量。一旦我将客户端ID设置为环境变量中的全局变量,它就工作了。