【问题标题】:Import neighboring module in python在python中导入相邻模块
【发布时间】:2023-03-17 09:52:01
【问题描述】:

我有以下文件夹结构,以使我的单元测试文件与他们测试的实际模块分开。

proj/
  foo/
    __init__.py
    bar.py
  tests/
    test.py

如何从test.py 导入模块foo

【问题讨论】:

  • proj 相对于你的 Python 路径在哪里?你是如何运行你的测试的?您是否使用像 nose 这样的测试运行器?
  • 哪个“模块 foo”? foofoo.foo?
  • @MartijnPieters proj 不在我的 python 路径中。我用unittestproj $ python tests/test.py 运行它。
  • @cheezsteak: 那么tests 在你的路径上,但foo 不会在你的路径中,除非你明确地将它添加到你的路径中。像鼻子这样的测试运行器可以帮助管理测试路径。
  • @IgnacioVazquez-Abrams:是的,只要将__init__.py 文件添加到tests

标签: python import


【解决方案1】:

这应该可以工作

from .. import foo

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    相关资源
    最近更新 更多