pylint 无法导入自定义模块和函数(模块中无名称)

pylint Cannot import custom module and function (no-name-in-module)

我的结构是:

 |- file run_app.py

 |- folder 'tasks'

 |-- file app.py

run_app.py中有一个字符串:

import tasks.app

并且 pylint 提醒

run_app.py:8:0: E0611: No name 'app' in module 'tasks' (no-name-in-module)

当我将 tasks 重命名为 taskss 时,错误消失了。这是什么?如果我想准确命名文件夹,如何解决这种奇怪的行为 'tasks'?

尝试在文件夹中包含一个 __init__.py 文件。

原因:

需要 __init__.py 文件才能使 Python 将目录视为包含软件包。

结构:

package_name/
  __init__.py
  foo.py
  subpackage/
    other.py

更多信息和例子在这里: https://docs.python.org/3/tutorial/modules.html#packages

只是添加到@makis 的回答: 在 python 3.2:

之前,__init__.py 需要将文件夹打包
package_name/
  __init__.py <- makes package_name a package
  foo.py

但是如果你在 Python 3.3+ 中使用 __init__.py 这样可能会生成 pylint "no name in module" 错误:

package_name/
  __init__.py
  foo.py
  subpackage/
    other.py

from package_name.subpackage import other 将产生上述错误。
如果您删除 __init__.pypylint 将停止警告您。

我的来源: http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html