Skip to content

Commit 6ca01e0

Browse files
AWhetterPCManticore
authored andcommitted
Fixed being unable to find distutils submodules by name when in a virtualenv (#672)
Close pylint-dev/pylint#73
1 parent 66e673b commit 6ca01e0

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

ChangeLog

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ Release Date: TBA
6565

6666
Close PyCQA/pylint#1628
6767

68+
* Fixed being unable to find distutils submodules by name when in a virtualenv.
69+
70+
Close PyCQA/pylint#73
6871

6972
What's New in astroid 2.2.0?
7073
============================

astroid/interpreter/_import/spec.py

+7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import abc
1010
import collections
11+
import distutils
1112
import enum
1213
import imp
1314
import os
@@ -145,6 +146,12 @@ def contribute_to_path(self, spec, processed):
145146
for p in sys.path
146147
if os.path.isdir(os.path.join(p, *processed))
147148
]
149+
# We already import distutils elsewhere in astroid,
150+
# so if it is the same module, we can use it directly.
151+
elif spec.name == "distutils" and spec.location in distutils.__path__:
152+
# distutils is patched inside virtualenvs to pick up submodules
153+
# from the original Python, not from the virtualenv itself.
154+
path = list(distutils.__path__)
148155
else:
149156
path = [spec.location]
150157
return path

astroid/tests/unittest_modutils.py

+5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"""
1515
unit tests for module modutils (module manipulation utilities)
1616
"""
17+
import distutils.version
1718
import email
1819
import os
1920
import sys
@@ -60,6 +61,10 @@ def test_find_egg_module(self):
6061
["data", "MyPyPa-0.1.0-py2.5.egg", self.package],
6162
)
6263

64+
def test_find_distutils_submodules_in_virtualenv(self):
65+
found_spec = spec.find_spec(["distutils", "version"])
66+
self.assertEqual(found_spec.location, distutils.version.__file__)
67+
6368

6469
class LoadModuleFromNameTest(unittest.TestCase):
6570
""" load a python module from it's name """

0 commit comments

Comments
 (0)