Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid "Module import itself" error for namespace packages. #2648

Open
feluxe opened this issue Dec 15, 2018 · 15 comments · Fixed by #6405
Open

Invalid "Module import itself" error for namespace packages. #2648

feluxe opened this issue Dec 15, 2018 · 15 comments · Fixed by #6405

Comments

@feluxe
Copy link

feluxe commented Dec 15, 2018

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]
@PCManticore
Copy link
Contributor

Thanks for reporting an issue!

@Oberon00
Copy link

Oberon00 commented Oct 8, 2019

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

@Oberon00
Copy link

Oberon00 commented Oct 9, 2019

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

@Oberon00
Copy link

Oberon00 commented Oct 9, 2019

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

@erensahin
Copy link

I run into the same problem when I use namespace packages. My pylint version is 2.4.4, I tried with more recent versions but it does not solve at all. Any suggestions?

@Pierre-Sassoulas
Copy link
Member

Did you try to install from scratch (do you have the latest astroid version too) ? This issue seems related to #5235 it might be a solution. If you have an example of repository with namespace feel free to comment on it :)

@erensahin
Copy link

Thanks! I uninstalled pylint and astroid, and re-installed pylint with its latest version (2.13.4).

I will try to explain the package structure. The problem is, namespace package name conflicts with another python library name (it is an internal one, so I will pick pandas as an example);

├── namespace_pkg
│   ├── pandas
│   │   └── __init__.py
│   └── something_else
│       └── __init__.py

The content of namespace_pkg/pandas/init.py is as follows;

cat namespace_pkg/pandas/__init__.py

from pandas import DataFrame
df = DataFrame([[1,2]])
print(df)

When I run the file directly as python namespace_pkg/pandas/__init__.py, I don't encounter any problem at all;

   0  1
0  1  2

But when I run pylint: pylint namespace_pkg/, I get the error as;

namespace_pkg/pandas/__init__.py:1:0: W0406: Module import itself (import-self)

@erensahin
Copy link

Did you try to install from scratch (do you have the latest astroid version too) ? This issue seems related to #5235 it might be a solution. If you have an example of repository with namespace feel free to comment on it :)

Plus, #5235 looks quite promising for my problem :)

@Pierre-Sassoulas
Copy link
Member

Plus, #5235 looks quite promising for my problem :)

Can you try installing pylint with : pip install -U git+https://github.com/hmc-cs-mdrissi/@add_option_disable_sys_path_patching and launch pylint with disable-path-patching=y in your pylintrc for the [master] section, to see if it fix the issue, please ?

@erensahin
Copy link

disable-path-patching

Yeah, it does fix it. Thanks :)

btw, the correct pip install command should be as;

pip install -U git+https://github.com/hmc-cs-mdrissi/pylint/@md/add_option_disable_sys_path_patching

@DanielNoord
Copy link
Collaborator

@Pierre-Sassoulas Can we use this example to create a folder structure to test on in the primer?

@Pierre-Sassoulas
Copy link
Member

Yes, I was thinking we should use something else than pandas, like pathlib, so we don't have to add dependencies to pandas.

@erensahin
Copy link

@Pierre-Sassoulas do you have any planned timeline for the next release? when we can get the new feature implemented in #5235 :)

@Pierre-Sassoulas Pierre-Sassoulas added this to the 2.13.5 milestone Apr 5, 2022
@Pierre-Sassoulas
Copy link
Member

Pierre-Sassoulas commented Apr 5, 2022

You can check https://github.com/PyCQA/pylint/milestone/61 to see the next patch release addvancement. It depends on the availability of implementers and reviewer but soonish ™️

@DanielNoord
Copy link
Collaborator

I have been working on a fix in #6405.

For anybody affected by this, please let me know if the changes in that PR fix your issues with linting of namespace packages.

@DanielNoord DanielNoord removed this from the 2.15.0 milestone Apr 20, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants