"No name in module" error from Pylint

Question:

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.

Asked By: Elias Zamaria

||

Answers:

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.

Answered By: Jacob Tomlinson

I got this error when my function was named start:

from views import start

It worked when I changed the name of the function.

Answered By: user984003

I sometimes got this pylint warning in PyCharm. The code works and PyCharm is able to navigate to the imported function if I Ctrl-click on the name.

From https://pycodequ.al/docs/pylint-messages/e0611-no-name-in-module.html

There can be false positive issues of the type when

  • pylint is run with an incompatible python version

  • pylint is run in an environment that misses a depedency of your code (this is more likely to lead to issues of type import-error
    (E0401))

  • the name you are importing is in turn imported into the target module by a wildcard import

A false positive warning can be disabled with a comment:

# pylint: disable = no-name-in-module

The warning can also be disabled in pyproject.toml section of pylint:

[tool.pylint.'MESSAGES CONTROL']

disable=[
  'no-name-in-module'
]
Answered By: Stefan

If the module that you are importing has an error in it, then you will also get this pylint error.

Answered By: zakum1
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.