ModuleNotFoundError: No module named ‘app.lang’; ‘app’ is not a package

huangapple go评论63阅读模式
英文:

ModuleNotFoundError: No module named 'app.lang'; 'app' is not a package

问题

我在我的Python项目中有这样的文件结构:

|__src 
   |__main.py
   |__gen.py  
     |__app     
        |__ __init__.py
        |__ app.py
        |__ lang.py

意图

我想要在兄弟模块 lang 中使用 Language 类。
所以我在 app.py 中尝试了这个导入语句:

from app.lang import Language

问题

但是当我运行 app.py 时,我收到一个 ModuleNotFoundError 错误,说 'app' 不是一个包

ModuleNotFoundError: No module named ‘app.lang’; ‘app’ is not a package

这是没有道理的,因为 app 有一个 __init__.py

如何解决这个问题?

英文:

I have this file structure in my python project

|__src 
   |__main.py
   |__gen.py  
     |__app     
        |__ __init__.py
        |__ app.py
        |__ lang.py

Intention

I want to use the Language class from sibling module lang.
So I tried with this import statement in app.py:

from app.lang import Language

Issue

But when I run app.py I get a ModuleNotFoundError error saying 'app' is not a package:

ModuleNotFoundError: No module named ‘app.lang’; ‘app’ is not a package

Which doesn't make sense since app has __init__.py.

How can I solve this?

答案1

得分: 1

因为app.pylang.py都在同一个目录中,尝试像这样导入:

from .lang import Language

或者你可以从位于app文件夹外部的另一个文件中使用from app.lang import Language

英文:

Because both app.py and lang.py are in the same directory try to import like this :

from .lang import Language

or you can use from app.lang import Language from another file located outside app folder

huangapple
  • 本文由 发表于 2023年2月18日 02:53:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75488271.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定