Pylint "unable to import" error but works fine with Pycharm

10,203

Solution 1

Module (package) cannot have minus in name. Rename Sub-dir to sub_dir, Sub-sub-dir to sub_sub_dir and Sub-sub-dir2 to sub_sub_dir2.

Next read PEP-8 The Python Style Guide

Solution 2

Are top_dir and sub_dir named the same thing? If so, and there are no files to import in the top_dir (just modules housed in that directory), delete the __init__.py & __init__.pyc files and try again.

Imagine the following case:

foo
__init__.py
  foo
  __init__.py
     bar
     __init__.py
     baz.py

If your import statement is something like import foo.bar.baz or from foo.bar import baz and you're running your script from the top level 'foo', importing will fail because python importing places the current directory in sys.path. You either need to tell python that the top level is not a module, or you need to insert the path you want into sys.path.

Share:
10,203
monpy
Author by

monpy

Updated on June 18, 2022

Comments

  • monpy
    monpy almost 2 years

    The basic structure of my program looks like this:

    top_dir
    __init__.py
    readme.rst
        sub_dir
        __init__.py
            sub_sub_dir
            __init__.py
            example_module.py
            sub_sub_dir2
            __init__.py
            module_with_import.py
    

    In Pycharm all imports just work fine. For example I use the following import in 'module_with_import.py':

    from sub_dir.sub_sub_dir.example_module import function
    

    However if I run pylint on module_with_import.py I will get the following error:

    Unable to import 'sub_dir.sub_sub_dir.example_module' (import-error)
    

    Does anybody see what's wrong here?