gpt4 book ai didi

Python导入模块错误

转载 作者:行者123 更新时间:2023-11-30 22:49:22 24 4
gpt4 key购买 nike

我在项目中导入模块时遇到问题。我正在创建测试,但无法从测试文件 teste_ex.py 导入 main 来测试应用程序的一个端点。这是我的项目结构:

backend_api/
api/
__init__.py
main.py
testes/
__init__.py
test_ex.py

在我的 test_ex.py 中,我尝试以这种方式导入 main:

import api.main
from webtest import TestApp

def test_functional_concursos_api():
app = TestApp(main.app)
assert app.get('/hello').status == '200 OK'

我只得到ImportError:没有名为“api”的模块

  • 如何将我的 main 导入到我的测试文件中?

最佳答案

以下是我对可能有帮助的最佳猜测:

您应该确保可以在 sys.path 中找到 main.py 文件所在目录的路径。您可以通过在 test_ex.py 中运行此代码片段来自行检查:

import sys

for line in sys.path:
print line

如果 main.py 所在的目录包含在您的路径中,您可以将该路径附加到 sys.path,如下所示(将此代码段包含在 test_ex.py 中):

import sys
sys.path.append("/path/to/your/module/backendapi/api/")

关于Python导入模块错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39813581/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com