Pylint: Absolute import misidentified as relative import

Created on 14 Mar 2018  路  4Comments  路  Source: PyCQA/pylint

Steps to reproduce

  1. Create a package mypkg containing __init__.py and os.py
  2. In __init__.py, require something from the built-in os package, e.g. from os import path
  3. Run pylint __init__.py from within the mypkg directory.

Current behavior

Pylint reports No name 'path' in module 'os' (no-name-in-module), assuming that __init__.py refers to os.py in the mypkg directory, and not the built-in os module.

The problem only manifests when pylint is invoked from the directory containing os.py.

Expected behavior

The from os import path is identified as an absolute import, and no error is reported.

pylint --version output

pylint --version
No config file found, using default configuration
pylint 1.8.2,
astroid 1.6.1
Python 3.6.4 (default, Mar  9 2018, 23:15:03)
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
bug

Most helpful comment

  • The issue does not occur if Pylint is called from a different directory

~This actually depends on the python version - it is correct for python2, but for python3 pylint will also complain when called from a different directory.~
@tilgovi is correct for both python2 and 3; thanks @giovannipizzi (+repo updated).

See this github repo with a minimum working example https://github.com/ltalirz/prospector-pylint-absolute-import

I confirm that the overall issue still persists in pylint 2.1.1, with no obvious solution or workaround.

All 4 comments

Thanks for the issue, I can reproduce it.

A little more detail:

  • The issue does not occur if Pylint is called from a different directory
  • The issue does not appear to just be caused by the Python module path*

* Running pylint this way, which removes the current directory from the module path, still reproduces the issue: python -c 'import sys,runpy;sys.path.pop(0);runpy.run_module("pylint") file.py'.

  • The issue does not occur if Pylint is called from a different directory

~This actually depends on the python version - it is correct for python2, but for python3 pylint will also complain when called from a different directory.~
@tilgovi is correct for both python2 and 3; thanks @giovannipizzi (+repo updated).

See this github repo with a minimum working example https://github.com/ltalirz/prospector-pylint-absolute-import

I confirm that the overall issue still persists in pylint 2.1.1, with no obvious solution or workaround.

This still seems to be a problem in pylint 2.3.1

Was this page helpful?
0 / 5 - 0 ratings