16
16
"""imports checkers for Python code"""
17
17
18
18
import collections
19
+ from distutils import sysconfig
19
20
import os
21
+ import platform
20
22
import sys
21
23
22
24
import six
23
25
24
26
import astroid
25
27
from astroid import are_exclusive
26
- from astroid .modutils import (EXT_LIB_DIR , get_module_part , is_standard_module ,
28
+ from astroid .modutils import (get_module_part , is_standard_module ,
27
29
file_from_modpath )
28
30
29
31
from pylint .interfaces import IAstroidChecker
@@ -265,7 +267,6 @@ class ImportsChecker(BaseChecker):
265
267
given file (report RP0402 must not be disabled)' }
266
268
),
267
269
)
268
- ext_lib_dir = os .path .normcase (os .path .abspath (EXT_LIB_DIR ))
269
270
270
271
def __init__ (self , linter = None ):
271
272
BaseChecker .__init__ (self , linter )
@@ -280,6 +281,28 @@ def __init__(self, linter=None):
280
281
self ._report_dependencies_graph ),
281
282
)
282
283
284
+ self ._site_packages = self ._compute_site_packages ()
285
+
286
+ def _compute_site_packages (self ):
287
+ def _normalized_path (path ):
288
+ return os .path .normcase (os .path .abspath (path ))
289
+
290
+ paths = set ()
291
+ real_prefix = getattr (sys , 'real_prefix' , None )
292
+ for prefix in filter (None , (real_prefix , sys .prefix )):
293
+ path = sysconfig .get_python_lib (prefix = prefix )
294
+ path = _normalized_path (path )
295
+ paths .add (path )
296
+
297
+ # Handle Debian's derivatives /usr/local.
298
+ if os .path .isfile ("/etc/debian_version" ):
299
+ for prefix in filter (None , (real_prefix , sys .prefix )):
300
+ libpython = os .path .join (prefix , "local" , "lib" ,
301
+ "python" + sysconfig .get_python_version (),
302
+ "dist-packages" )
303
+ paths .add (libpython )
304
+ return paths
305
+
283
306
def open (self ):
284
307
"""called before visiting project (i.e set of modules)"""
285
308
self .linter .add_stats (dependencies = {})
@@ -449,10 +472,6 @@ def _check_imports_order(self, node):
449
472
extern_imports = []
450
473
local_imports = []
451
474
std_imports = []
452
- stdlib_paths = [sys .prefix , self .ext_lib_dir ]
453
- real_prefix = getattr (sys , 'real_prefix' , None )
454
- if real_prefix is not None :
455
- stdlib_paths .append (real_prefix )
456
475
457
476
for node , modname in self ._imports_stack :
458
477
package = modname .split ('.' )[0 ]
@@ -468,13 +487,12 @@ def _check_imports_order(self, node):
468
487
try :
469
488
filename = file_from_modpath ([package ])
470
489
except ImportError :
471
- extern_imports .append ((node , package ))
472
490
continue
473
491
if not filename :
474
- extern_imports .append ((node , package ))
475
492
continue
493
+
476
494
filename = os .path .normcase (os .path .abspath (filename ))
477
- if not any (filename .startswith (path ) for path in stdlib_paths ):
495
+ if not any (filename .startswith (path ) for path in self . _site_packages ):
478
496
local_imports .append ((node , package ))
479
497
continue
480
498
extern_imports .append ((node , package ))
0 commit comments