Using local imports in Pytest

无人久伴 提交于 2019-12-11 16:57:12

问题


I have never really fully understood how packages are handled in Python and I'm having a problem with that right now. But googling doesn't seem to help as I find the topic really confusing.

I have a project with this structure:

project_name/
    src/
        main.py
        utils/
            string_utils.py
    tests/
        test_string_utils.py

I am using Pytest for running unit testing and currently inside the "test_string_utils.py" file I have the following:

from ..src.utils.string_utils import StringUtilsClass

But I go to the folder "project_name" and try to run tests with any of this command I get errors:

$ pytest tests/

ValueError: attempted relative import beyond top-level package

I know about the -m argument for python, but it seems that running "pytest -m" has a completely different behavior.

How can I solve this? Am I using the wrong folder architecture? I don't think what I'm building should be a pip package (which would simplify imports)


回答1:


did you try : from src.utils.string_utils import StringUtilsClass without .. before src? or from string_utils import StringUtilsClass



来源:https://stackoverflow.com/questions/53362259/using-local-imports-in-pytest

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!