realpython / realpython/python-guide

Structuring your project guide leads to a ModuleNotFoundError in the tests module

未关闭
#1,013 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
Batchfile
星标
29.8k
派生
5.9k
PR 合并指标
30 天内没有已合并 PR

描述

Following this section, adding from .context import sample in my tests module gives me the following error: ModuleNotFoundError: No module named '__main__.context'; '__main__' is not a package

I was able to fix it by removing the . from from .context import sample, but I suspect that's not the ideal solution. I know that the from .context import sample is a relative import statement as described in PEP 328, but I just do not understand modules, packages, absolute and relative imports, etc. enough to say what's going on here.

I tried creating ./__init__.py and ./tests/__init__.py files, but that did not help.

Finally, I tried the following:

~> git clone git@github.com:navdeep-G/samplemod.git
~> python3 samplemod/tests/test_basic.py
Traceback (most recent call last):
  File "samplemod/tests/test_basic.py", line 3, in <module>
    from .context import sample
ModuleNotFoundError: No module named '__main__.context'; '__main__' is not a package

I'm not alone in hitting this issue. @rileyweber13 originally reported this problem a year ago.

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从链接的“Test Suite”部分开始,并使用 samplemod/tests/test_basic.py 重现报告中的命令。跟踪测试文件的调用方式以及其相对导入的解析方式,然后更新指南,使文档中说明的设置和执行路径能够正常工作,且不会出现报告中的 ModuleNotFoundError。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
documentation
Issue 类型
文档
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。