"No name in module" error from Pylint

Elias Zamaria picture Elias Zamaria · Oct 31, 2016 · Viewed 62.3k times · Source

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()

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")

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

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:

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.

Answer

Jacob Tomlinson picture Jacob Tomlinson · Dec 16, 2016

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.