Commits

jtolds committed 5432e78

Make pythonpath behaviour closer to python's (closes #88218)

When pylint is called with a single filename as argument, walk its path
backwards to find the first directory that doesn't have a __init__.py,
and add that to sys.path instead of the cwd.

  • Participants
  • Parent commits ffc9061

Comments (0)

Files changed (1)

                     'html': HTMLReporter,}
 
 
+def _get_python_path(filepath):
+    dirname = os.path.dirname(os.path.realpath(
+            os.path.expanduser(filepath)))
+    while True:
+        if not os.path.exists(os.path.join(dirname, "__init__.py")):
+            return dirname
+        old_dirname = dirname
+        dirname = os.path.dirname(dirname)
+        if old_dirname == dirname:
+            return os.getcwd()
+
+
 # Python Linter class #########################################################
 
 MSGS = {
             sys.exit(32)
         # insert current working directory to the python path to have a correct
         # behaviour
-        sys.path.insert(0, os.getcwd())
+        if len(args) == 1:
+            sys.path.insert(0, _get_python_path(args[0]))
+        else:
+            sys.path.insert(0, os.getcwd())
         if self.linter.config.profile:
             print >> sys.stderr, '** profiled run'
             import cProfile, pstats