【问题标题】:pytest failing in AWS SAM project due to ModuleNotFoundError由于 ModuleNotFoundError,pytest 在 AWS SAM 项目中失败
【发布时间】:2021-12-04 02:14:47
【问题描述】:

我有一个具有以下文件夹结构的 AWS SAM 项目:

account/
   __init__.py
   layers/
       __init__.py
       config/
           settings.py
       utils/
           logger.py
   get_accounts/
       app.py
       requirements.txt
   tests/
        requirements.txt
        integration/
            test_api_gateway.py
        unit/
            test_handler.py

当我从项目根目录运行python3 -m pytest tests/unit/ 时,出现以下错误:

导入测试模块时出现ImportError '/Users/johnsmith/dev/myproject/account/tests/unit/test_handler.py'。暗示: 确保您的测试模块/包具有有效的 Python 名称。

追溯: /usr/local/Cellar/python@3.9/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/init.py:127: 在 import_module return _bootstrap._gcd_import(name[level:], package, level) tests/unit/test_handler.py:3: in 从 get_accounts 导入应用程序 get_accounts/app.py:14: 在 从 layers.utils.logger 导入 get_logger 层/utils/logger.py:1:在 from config.settings import ApplicationConfig E ModuleNotFoundError: No module named 'config'

问题是当测试文件导入主应用程序时,主应用程序的依赖项位于“layers”文件夹中;由于某种原因,它无法导入这些文件。当我使用sam local 命令启动应用程序时,它可以工作,但是当我运行 pytest 时它会失败。

我试过了

  • __init__.py 添加到“layers”文件夹下的所有文件夹中。
  • 创建 setup.py 并指定“config”作为包名,然后运行 ​​pip install -e . based on pytest's documentation
  • get_accounts/app.py 中,在导入这些项目时在config.settingsutils.logger 之前指定“层”
  • 从测试文件夹中删除__init__.py

这些都是 stackoverflow 中提到的所有可能的解决方案,但没有一个对我有用。

get_accounts/app.py 中的导入如下所示:

import json
import requests
from config.settings import ApplicationConfig
from utils.logger import get_logger
from pprint import pprint

有人对我做错了什么以及如何让它发挥作用有任何想法吗?

【问题讨论】:

    标签: python python-module


    【解决方案1】:

    我设法或多或少地重现了您遇到的情况。这是我的文件:

    account > cat tests/unit/test_import.py 
    import get_accounts.app
    
    def test_hello():
        print("Hello World")
    account > cat get_accounts/app.py 
    from config.settings import ApplicationConfig
    
    account > cat layers/config/settings.py 
    
    class ApplicationConfig:
        pass
    

    它会引发一个错误,说无法导入配置。更改自:

    from config.settings import ApplicationConfig
    

    from layers.config.settings import ApplicationConfig
    

    解决了我的设置中的问题。

    account > python3 -m pytest tests/
    ===================================================================== test session starts ======================================================================platform linux -- Python 3.8.10, pytest-6.2.5, py-1.10.0, pluggy-1.0.0
    rootdir: /home/brunno/account
    collected 1 item
    
    tests/unit/test_import.py .                                                                                                                              [100%] 
    
    ====================================================================== 1 passed in 0.01s =======================================================================
    

    【讨论】:

    • 嗨,bvan,感谢您调查问题。我已经尝试过这个解决方案,但它对我不起作用。当我尝试在导入语句中添加“层”并构建应用程序时,它实际上会失败。我不完全理解这个“层”文件夹是如何工作的,但我相信当项目使用 docker 映像来模拟 AWS API Gateway 环境时,它会在构建期间被编译。模拟这种情况的更好方法是create a new SAM project
    • 您是否尝试在 app.py 文件的顶部添加 sys.path.append("..")sys.path.append("../layers")? SAM 似乎没有找到正确的路径或从不同于预期的目录开始。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 2021-06-05
    • 2022-08-16
    • 1970-01-01
    • 2022-01-18
    • 2022-07-04
    • 1970-01-01
    相关资源
    最近更新 更多