简体   繁体   English

导入模块时出现“ModuleNotFoundError”

[英]"ModuleNotFoundError" when importing modules

A similar question was asked at: Python subpackage import "no module named x" But I was still not able to solve my problem.在以下位置提出了类似的问题: Python subpackage import "no module named x"但我仍然无法解决我的问题。

For the first time, I divided my python code into modules and packages.我第一次将我的 python 代码分成模块和包。 Here is how the files are set up (not with the real names):以下是文件的设置方式(不是真实姓名):

└── projectfolder
    |––main.py
    |––package1
        |--__init.py__
        |--module1.py
        |--subpackage1
            |--__init.py__
            |--module2.py

Inside module2.py there is a function ("function1") that is supposed to be used inside a function ("function2") in module1.py.在 module2.py 中有一个 function(“function1”)应该在 module1.py 中的 function(“function2”)中使用。 Function2 is then supposed to be used inside main.py.然后应该在 main.py 中使用 Function2。

This is firstly done by importing suppackage1 inside module1:这首先是通过在 module1 中导入 suppackage1 来完成的:

import subpackage1

This is the code inside the __init__.py inside subpackage1:这是 subpackage1 中的__init__.py中的代码:

from.module2 import function1

Function1 is then used inside function2 which lies in module1.py like this:然后在位于 module1.py 中的 function2 中使用 Function1,如下所示:

subpackage1.function1()

Which does not create any error message.这不会创建任何错误消息。 But now I want to call function2 in main.py.但是现在我想在 main.py 中调用 function2。 Package1 is imported in main.py like this: Package1 像这样导入到 main.py 中:

import package1

This is the code inside the __init__.py file inside package1:这是 package1 中的__init__.py文件中的代码:

from.module1 import function2

I was then expecting that I could use function2 without a problem in main.py like this:然后我期待我可以像这样在 main.py 中毫无问题地使用 function2:

package1.function2()

But when running the code from main.py I get this error message:但是当从 main.py 运行代码时,我收到此错误消息:

Error:错误:

"Inside main.py" import package1
"Inside __init__.py which lies in package1" from .module1 import function2
"Inside module1.py" ModuleNotFoundError: No module named 'subpackage1'

What have I done wrong?我做错了什么? And is there a better way to organize packages and modules?有没有更好的方法来组织包和模块? Because this is already hard to manage only with a few files.因为这已经很难只用几个文件来管理了。

Briefly:简要地:

When you run main.py your subpackage1 is out of the scope, therefore you can't import it with import subpackage1 .当您运行main.py时,您的subpackage1不在 scope 中,因此您无法使用import subpackage1导入它。 More info on this topic here .有关此主题的更多信息,请参见此处

Consider rewriting import subpackage1 like:考虑像这样重写import subpackage1

from package1 import subpackage1

Or add it to the PYTHONPATH env variable, but I personally find that way more confusing或者将它添加到PYTHONPATH env 变量中,但我个人觉得那样更混乱

From this answer , There is nothing like real sub‑package in Python, as all package references goes to a global dictionnary, only, which means there's no local dictionary, which implies there's is no way to manage local package reference.从这个答案来看,Python 中没有真正的子包,因为所有 package 引用都转到全局字典,这意味着没有本地字典,这意味着无法管理本地 package 引用。

You have to either use full prefix or short prefix or alias.您必须使用完整前缀或短前缀或别名。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM