diff --git a/ChangeLog b/ChangeLog index 6a8e5091b5..9982b5a2fe 100644 --- a/ChangeLog +++ b/ChangeLog @@ -59,6 +59,9 @@ Release Date: TBA Close PyCQA/pylint#1628 +* Fixed being unable to find distutils submodules by name when in a virtualenv. + + Close PyCQA/pylint#73 What's New in astroid 2.2.0? ============================ diff --git a/astroid/interpreter/_import/spec.py b/astroid/interpreter/_import/spec.py index 982f9b6f41..fcd78c0cee 100644 --- a/astroid/interpreter/_import/spec.py +++ b/astroid/interpreter/_import/spec.py @@ -8,6 +8,7 @@ import abc import collections +import distutils import enum import imp import os @@ -145,6 +146,12 @@ def contribute_to_path(self, spec, processed): for p in sys.path if os.path.isdir(os.path.join(p, *processed)) ] + # We already import distutils elsewhere in astroid, + # so if it is the same module, we can use it directly. + elif spec.name == "distutils" and spec.location in distutils.__path__: + # distutils is patched inside virtualenvs to pick up submodules + # from the original Python, not from the virtualenv itself. + path = list(distutils.__path__) else: path = [spec.location] return path diff --git a/astroid/tests/unittest_modutils.py b/astroid/tests/unittest_modutils.py index b517422e80..beb7b99588 100644 --- a/astroid/tests/unittest_modutils.py +++ b/astroid/tests/unittest_modutils.py @@ -14,6 +14,7 @@ """ unit tests for module modutils (module manipulation utilities) """ +import distutils.version import email import os import sys @@ -60,6 +61,10 @@ def test_find_egg_module(self): ["data", "MyPyPa-0.1.0-py2.5.egg", self.package], ) + def test_find_distutils_submodules_in_virtualenv(self): + found_spec = spec.find_spec(["distutils", "version"]) + self.assertEqual(found_spec.location, distutils.version.__file__) + class LoadModuleFromNameTest(unittest.TestCase): """ load a python module from it's name """