Open sam-s opened 3 years ago
Love how minimal this example is, I've been able to reproduce this behaviour exactly, including that the false positive import-error disappears after removing __init__.py
I'm doing research on static code analysis in machine learning code, where I run Pylint on a number of Python ML projects (after installing their requirements in a virtualenv). While this currently only includes 8 projects so far, Pylint reports each one of those as having several import-errors. I've noticed that most of those are imports from local files not being resolved correctly, even though they do indeed exist on the filesystem.
$ pylint --version
pylint 2.6.0
astroid 2.4.2
Python 3.8.6 (default, Sep 30 2020, 04:00:38)
[GCC 10.2.0]
@sam-s thanks for your report. I can reproduce it.
I'm seeing this fixed in astroid 2.12. Let's add this test case to pylint.
@jacobtylerwalls Is this still the case? 🚀
No, I was probably testing in a directory on sys.path, and we now know that's been polluting our test results.
Failing test for unittest_lint.py
:
def test_import_sibling_module_from_explicit_package(initialized_linter: PyLinter) -> None:
linter = initialized_linter
with tempdir() as tmpdir:
create_files(["siblingone.py", "siblingtwo.py", "__init__.py"], tmpdir)
with open("siblingone.py", "w", encoding="utf-8") as f:
f.write(
"""\"\"\"This module contains siblingone().\"\"\"
def siblingone():
print("siblingone")
"""
)
with open("siblingtwo.py", "w", encoding="utf-8") as f:
f.write(
"""\"\"\"This module imports siblingone().\"\"\"
import siblingone
print(siblingone.siblingone())
"""
)
linter.check(["siblingtwo"])
assert not linter.stats.by_msg
Hi,
I am still facing this issue with latest pylint version. pylint --version output:
pylint 2.16.2
astroid 2.14.2
Python 3.10.4 (main, Feb 15 2023, 15:33:42) [GCC 9.4.0]
I found 2 solutions for this issue. For those who suffer from this issue like me:
from . import a
Explanation: As I understand, when we create __init__.py
file, the directory which contains this file becomes a package/ module. So we need to import this as module importing syntax.
init-hook="import sys; import os; sys.path.append(os.path.abspath('.'));"
Explanation: Add the system path of the directory which contains a.py
to pylint
.If your code structure is more complicated, modify an appropriate path value. Hope it helps!
issue remain in
pylint 2.17.4
astroid 2.15.5
Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)]
For those who still facing this issue and have a similar structure like below
.
├── src
│ │
│ └─ pkgs
│ ├── __init__.py
│ ├── mod1.py
│ └── mod2.py (import mod1)
│ ...
├── pyproject.toml
└── .pylintrc
Try to add source-roots
in your .pylintrc
[MAIN]
source-roots = src/pkgs
It's working in
pylint 3.2.7
astroid 3.2.4
Python 3.11.9 (main, Apr 19 2024, 16:39:34) [GCC 11.2.0]
Steps to reproduce
in an empty directory:
a.py
:b.py
__init__.py
Current behavior
Expected behavior
no diagnostics
Important note
if you remove
__init__.py
, theimport-error
disappears.pylint --version output