问题描述
我正在使用Python 3.8并为Azure创建函数。我有这个目录结构..
shared
__init__.py
my_function
__init.py
tests
__init__.py
functions
__init__.py
test_myfunction.py
在shared / init .py文件中,我有一些常量...
import os
HEADERS = {"Content-Type": "text/plain"}
在我的my_function / init .py文件中,我还没有弄清楚如何编写一行来包含共享文件。如果我这样做
from ..shared import HEADERS
我在运行pytest时收到此错误。
pytest tests/functions/test_myfunction.py
...
ERROR tests/functions/test_myfunction.py - ValueError: attempted relative import beyond top-level package
如果我这样做,
from shared import HEADERS
启动应用程序时出现此错误(例如,使用“ func start”)...
Exception: ModuleNotFoundError: No module named 'shared'
我不得不求助于try-except
try:
from ..shared import HEADERS
except:
from shared import HEADERS
但是这确实感觉很混乱和错误。编写一行以包含我都需要的什么是正确的方法?
编辑:以下是pytest测试文件的主要内容,tests / functions / test_myfunction.py ...
_import = __import__('myfunction')
...
req = func.HttpRequest(
method='GET', body=None, url='/myfunction', params=params, headers=headers
)
resp = _import.main(req)
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)