“模块中没有名称"来自 Pylint 的错误 [英] "No name in module" error from Pylint

查看:49
本文介绍了“模块中没有名称"来自 Pylint 的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 main.py 的文件,其代码如下:

I have a file named main.py with the following code:

#!/usr/bin/env python3

import utils.stuff

if __name__ == "__main__":
    print("hi from main.py")
    utils.stuff.foo()

在带有 main.py 的目录中,我有一个名为 utils 的子目录,其中有一个名为 stuff.py 的文件,其中包含以下代码:

In the directory with main.py, I have a subdirectory named utils which has a file named stuff.py with the following code:

print("hi from stuff.py")

def foo():
    print("foo")

如果我从命令行运行 ./main.py,我会得到以下输出:

If I run ./main.py from the command line, I get the following output:

hi from stuff.py
hi from main.py
foo

这正是我所期望的.但是,如果我运行 pylint main.py,我会得到以下输出:

That is exactly what I expect. However, if I run pylint main.py, I get the following output:

No config file found, using default configuration
************* Module main
C:  1, 0: Missing module docstring (missing-docstring)
E:  3, 0: No name 'stuff' in module 'utils' (no-name-in-module)
E:  3, 0: Unable to import 'utils.stuff' (import-error)
E:  7, 4: Module 'utils' has no 'stuff' member (no-member)

接下来是一些似乎与我的问题无关的更详细的统计数据.为什么会这样?我该怎么做才能让 Pylint 知道 utils/stuff.py?我正在运行 Python 3.5.2、Pylint 1.6.4 和 OS X 10.11.6.

followed by some more detailed statistics that do not seem relevant to my question. Why is this happening? What can I do to make Pylint aware of utils/stuff.py? I am running Python 3.5.2, Pylint 1.6.4 and OS X 10.11.6.

推荐答案

您需要创建一个 utils/__init__.py.这将使 python 知道子模块,并且还允许您运行任何想要在导入时发生的代码.如果你不想运行任何东西,那么只需包含一个文档字符串.

You need a create utils/__init__.py. This will make python aware of the submodule and also allows you to run any code you want to happen on import. If you don't want anything to run then just include a docstring.

这篇关于“模块中没有名称"来自 Pylint 的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆