简体   繁体   English

我的主包的 Python ModuleNotFoundError

[英]Python ModuleNotFoundError for my main package

This is a follow up question for a question I asked not long ago .这是我不久前问的一个问题的后续问题。 The answer is correct and works great, but when I tried to apply it to the main package it didn't work.答案是正确的并且效果很好,但是当我尝试将它应用到主包时它不起作用。

Lets say I have the following files structure:假设我有以下文件结构:

a/
->b/
  ->c/
    -> __init__.py
    -> script1.py
    -> script2.py
  ->d/
    -> __init__.py
    -> script3.py

(The __init__ files are just like in the answer I linked above). __init__文件就像我上面链接的答案一样)。

And in script3.py I import script1.py like so: from bc import script1 .script3.py我像这样导入script1.pyfrom bc import script1

It works when I run it in Pycharm, but when I clone the repository in Colab (all this code is in a GitHub repository) I get the error: ModuleNotFoundError: No module named 'b'当我在 Pycharm 中运行它时它可以工作,但是当我在 Colab 中克隆存储库(所有这些代码都在 GitHub 存储库中)时,我收到错误: ModuleNotFoundError: No module named 'b'

Which makes sense because my package is not in the sys.path variable.这是有道理的,因为我的包不在sys.path变量中。 And adding the folder a to the sys.path manually, after I cloned the repository, helps but it is not a real solution because I can't always do it (eg in unit-tests).在我克隆存储库后,手动将文件夹a添加到sys.path帮助,但这不是一个真正的解决方案,因为我不能总是这样做(例如在单元测试中)。

So my question is, how can I fix it¿ Adding the __init__ file to folder b didn't help.所以我的问题是,我该如何修复它?将__init__文件添加到文件夹b没有帮助。

Notes:笔记:

  1. Folder a is the project's folder, so it is not part of the path.文件夹a是项目的文件夹,因此它不是路径的一部分。
  2. My GitHub repository is private, so I am not sure if making it installable (by adding setup.py and all that) would be helpful, but its my first time so I am not sure.我的 GitHub 存储库是私有的,所以我不确定让它可安装(通过添加 setup.py 和所有这些)是否会有所帮助,但这是我的第一次,所以我不确定。
  3. In Colab I import script3 like so from abd import script3 .在Colab我进口script3像现在这样from abd import script3 In this case I must specify folder a because, again, b is not in sys.path .在这种情况下,我必须指定文件夹a因为同样, b不在sys.path
  4. As I mentioned above, I can manually fix it in Colab but it doesn't really solves the issue, because if I want to run unit-tests for example it won't work.正如我上面提到的,我可以在 Colab 中手动修复它,但它并没有真正解决问题,因为如果我想运行单元测试,它就无法工作。

You can backwardly add folder b to your path and then import only from c folder, like this:您可以向后添加文件夹b到您的路径,然后仅从c文件夹导入,如下所示:

from os.path import realpath, dirname
import sys

sys.path.append(dirname(dirname(realpath(__file__))))

from c import script1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 尝试 package 我的 main.py 与模块但总是相同的错误: ModulenotfoundError - Python Try to package my main.py with modules but always same error : ModulenotfoundError ModuleNotFoundError:找不到我的 Python package - ModuleNotFoundError: cannot find my Python package Python 3 - 在同一目录中导入.py文件 - ModuleNotFoundError:没有名为'__main __。char'的模块; '__main__'不是包 - Python 3 - importing .py file in same directory - ModuleNotFoundError: No module named '__main__.char'; '__main__' is not a package 在docker中运行龙卷风python:ModuleNotFoundError:没有名为'__main __。config'的模块; '__main__'不是一个包 - running tornado python in docker: ModuleNotFoundError: No module named '__main__.config'; '__main__' is not a package 解决“ModuleNotFoundError: ...'__main__' is not a package”错误 - Working around "ModuleNotFoundError: ...'__main__' is not a package" error ModuleNotFoundError:__main__ 不是包是什么意思? - ModuleNotFoundError: What does it mean __main__ is not a package? 将 python 文件夹转换为包:`ModuleNotFoundError` - Transform a python folder into a package: `ModuleNotFoundError` 安装后出现“ModuleNotFoundError” python package - "ModuleNotFoundError" after installing a python package Python 导入 package 时出现 ModuleNotFoundError - Python ModuleNotFoundError when importing a package 安装的python包给出了ModuleNotFoundError - Installed python package gives ModuleNotFoundError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM