Python 3 项目间导入不起作用

标签 python python-3.x pytest importerror

我的目录结构如下:

project/
    README.md
    project/
        foobar/
            foo.py
        tests/
            test_foo.py

test_foo 只是 from foobar import foo ,当我运行 py.test 或只是 python3tests/test_foo.pyproject 子目录中,我收到 No module named foobar 错误。我在这里尝试了其他答案,例如将 .. 添加到 sys.path,使用相对导入,没有任何效果,除非运行 python3 test_foo.py > 在 tests 内使用 sys.path.append('..')

最佳答案

首先从您的项目中创建一个包并安装它。

如果安装了,您可以简单地使用绝对链接,以您的包名称开头:

from project import foobar
from project.foobar import foo

另一种方法 - 使用包内链接(无需修改 sys.path):

test_foo.py

from .. import foobar

关于Python 3 项目间导入不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53944823/

相关文章:

python - 如何将 EST/EDT 转换为 GMT?

python - Sklearn特征选择

python - CircleCi 上的 WebDriverException selenium-python 测试

python - Py.test 运行直到通过装饰器

python - 通过自身而不是方法参数的类的 Pytest fixture

python - 使用 sqlalchemy 连接到 Windows 上的 firebird super 服务器

Python BeautifulSoup - 防止特定的模糊/损坏标签匹配

python-3.x - 无法通过 ldap3 Python3 更改用户密码

Python 3 : find item in list and answer based on that

python - pandas df.to_sql到Oracle数据库数据类型不一致