1

I have the structure:

 |- file run_app.py

 |- folder 'tasks'

 |-- file app.py

There is a string in run_app.py:

import tasks.app

And pylint alerts that

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

When I rename tasks to taskss, the error disappears. Whats this? How to fix this strange behavior if I want to name the folder exactly 'tasks'?

sanyassh
  • 8,100
  • 13
  • 36
  • 70
  • The name `tasks` is already a python module – seralouk Nov 01 '18 at 20:44
  • I am not sure: python Python 3.6.6 (default, Sep 12 2018, 18:26:19) [GCC 8.0.1 20180414 (experimental) [trunk revision 259383]] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import tasks Traceback (most recent call last): File "", line 1, in ModuleNotFoundError: No module named 'tasks' >>> – sanyassh Nov 01 '18 at 20:45
  • can you try: `from tasks import app`? have you also included an `init` in the folder named `tasks` ? – seralouk Nov 01 '18 at 20:48
  • see alos this concerning the `init` file: https://stackoverflow.com/a/448279/5025009 – seralouk Nov 01 '18 at 20:49
  • __init__ helped, thanks. And the link is helpful too – sanyassh Nov 01 '18 at 20:52
  • glad that i could help. i posted a more complete answer – seralouk Nov 01 '18 at 20:56

2 Answers2

5

Just to add to @makis answer: __init__.py were required to make a folder a package until python 3.2:

package_name/
  __init__.py <- makes package_name a package
  foo.py

But if you are in Python 3.3+ using __init__.py that way may generate pylint "no name in module" error:

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

from package_name.subpackage import other will generate the mentioned error.
If you remove that __init__.py, pylint stops warning you.

My source: http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html

EttoreP
  • 404
  • 6
  • 16
Jean Da Rolt
  • 51
  • 1
  • 1
3

Try to include an __init__.py file in the folder.

Reason:

The __init__.py files are required to make Python treat the directories as containing packages.

Structure:

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

More info and examples here: https://docs.python.org/3/tutorial/modules.html#packages

seralouk
  • 30,938
  • 9
  • 118
  • 133