Let's fully unload pip

This commit is contained in:
Pedro Algarvio 2019-04-14 20:19:16 +01:00
parent 0b6f848082
commit 0b01f21f00
No known key found for this signature in database
GPG key ID: BB36BF6584A298FF

View file

@ -22,6 +22,7 @@ requisite to a pkg.installed state for the package which provides pip
# Import python libs
from __future__ import absolute_import, print_function, unicode_literals
import re
import types
import logging
try:
import pkg_resources
@ -60,34 +61,25 @@ if HAS_PIP is True:
HAS_PIP = False
# Remove references to the loaded pip module above so reloading works
import sys
pip_related_entries = [
(k, v) for (k, v) in sys.modules.items()
or getattr(v, '__module__', '').startswith('pip.')
or (isinstance(v, types.ModuleType) and v.__name__.startswith('pip.'))
]
for name, entry in pip_related_entries:
sys.modules.pop(name)
del entry
del pip
if 'pip' in sys.modules:
del sys.modules['pip']
sys_modules_pip = sys.modules.pop('pip', None)
if sys_modules_pip is not None:
del sys_modules_pip
try:
from pip.exceptions import InstallationError
except ImportError:
InstallationError = ValueError
# pylint: disable=unused-import
# When reloading, some functions are ofen lost from sys.modules. Try to prevent that.
try:
# Pip 9.0.x
from pip.download import is_url, path_to_url, is_archive_file
from pip.utils import is_installable_dir
from pip.req.req_install import _strip_extras
except ImportError:
try:
# Pip 10.x
from pip._internal.req.req_install import _strip_extras
except ImportError:
# Pip >= 18.x
from pip._internal.req.constructors import _strip_extras
# Pip >= 10.x
from pip._internal.download import is_url, path_to_url, is_archive_file
from pip._internal.utils.misc import is_installable_dir
# pylint: enable=unused-import
# pylint: enable=import-error
log = logging.getLogger(__name__)