Python 来自 Pylint 的“模块中没有名称”错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40334643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:24:02  来源:igfitidea点击:

"No name in module" error from Pylint

pythonpylint

提问by Elias Zamaria

I have a file named main.pywith the following code:

我有一个main.py用以下代码命名的文件:

#!/usr/bin/env python3

import utils.stuff

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

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

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

print("hi from stuff.py")

def foo():
    print("foo")

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

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

hi from stuff.py
hi from main.py
foo

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

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

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)

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.

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

回答by Jacob Tomlinson

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.

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

回答by user984003

I got this error when my function was named start:

当我的函数被命名为 start 时,我收到了这个错误:

from views import start

It worked when I changed the name of the function.

当我更改函数名称时它起作用了。