Pylint: Invalid "Module import itself" error for namespace packages.

Created on 15 Dec 2018  ยท  4Comments  ยท  Source: PyCQA/pylint

Steps to reproduce

You can either clone this repo to get the test file tree:

git clone https://github.com/feluxe/test_pylint_ns_package_err.git

cd into it and run pipenv install --dev --pre

or create the file tree manually with the following steps:

Create test file tree manually:

Create this file tree:

tmp_pylint_err
โ”œโ”€โ”€ nspackage
โ”‚   โ”œโ”€โ”€ mypackage
โ”‚   โ”‚   โ””โ”€โ”€ __init__.py
โ”‚   โ””โ”€โ”€ sty
โ”‚       โ””โ”€โ”€ __init__.py
โ””โ”€โ”€ test.py

Next, install pylint and sty from pypi in a Python 3.7 virtualenv:

pipenv install --dev --pre pylint sty

We add this line to nspackage/mypackage/__init__.py:

from nspackage.sty import fg

We add this line to nspackage/sty/__init__.py:

from sty import *

We add these lines to test.py:

from nspackage.sty import fg
from nspackage.mypackage import fg as fg2

print(f"{fg.green}hello1{fg.rs}")
print(f"{fg2.green}hello2{fg2.rs}")

Current behavior

Note: As you can see in the project file tree, nspackage is a name space package omitting __init__.py.

If I run pipenv run pylint nspackage, I get these errors:

************* Module sty
nspackage/sty/__init__.py:1:0: W0406: Module import itself (import-self)
************* Module mypackage
nspackage/mypackage/__init__.py:1:0: E0611: No name 'fg' in module 'nspackage.sty' (no-name-in-module)

Expected behavior

I think the example project is valid Python and pylint should not complain.

You can run test.py for validation:

pipenv run python test.py

which runs just fine.

pylint --version output

pylint 2.2.2
astroid 2.1.0
Python 3.7.1 (default, Nov 11 2018, 02:35:35) 
[GCC 8.2.1 20180831]
bug

Most helpful comment

At opentelemetry/opentelemetry-python, we have the same issue and it's very annoying as we would like to add companion modules for widely-used libraries that are named the same as the corresponding library, but in a different package.

EDIT: astroid==2.3.1,pylint==2.4.2
Python 3.7.3

All 4 comments

Thanks for reporting an issue!

At opentelemetry/opentelemetry-python, we have the same issue and it's very annoying as we would like to add companion modules for widely-used libraries that are named the same as the corresponding library, but in a different package.

EDIT: astroid==2.3.1,pylint==2.4.2
Python 3.7.3

It's very likely that #2862 is the same problem but that issue has a nicer repro.

Also, the topic-import-system label seems to be missing on this issue.

Was this page helpful?
0 / 5 - 0 ratings