Repository: jazzband/pip-tools

Issue:
Refactor import statements in repositories/pypi.py
#### What's the problem this feature will solve?
<!-- What are you trying to do, that you are unable to achieve with pip-tools as it currently stands? -->

Refactor import statements in `piptools/repositories/pypi.py` related to pip compatibility.

#### Describe the solution you'd like
<!-- A clear and concise description of what you want to happen. -->

<!-- Provide examples of real-world use cases that this would enable and how it solves the problem described above. -->

Move the following lines to piptools/_compat/pip_compat.py: https://github.com/jazzband/pip-tools/blob/00fe6e5119bde900c9515542988edbb974070612/piptools/repositories/pypi.py#L42-L54

Refactor this try-except block: https://github.com/jazzband/pip-tools/blob/00fe6e5119bde900c9515542988edbb974070612/piptools/repositories/pypi.py#L51-L54

 to  `do_import(...)`.

#### Alternative Solutions
<!-- Have you tried to workaround the problem using pip-tools or other tools? Or a different approach to solving this issue? Please elaborate here. -->

N/A

#### Additional context
<!-- Add any other context, links, etc. about the feature here. -->

N/A


Discussion:

Resolving patch:
diff --git a/piptools/_compat/__init__.py b/piptools/_compat/__init__.py
--- a/piptools/_compat/__init__.py
+++ b/piptools/_compat/__init__.py
@@ -16,8 +16,10 @@
     PackageFinder,
     PyPI,
     RequirementSet,
+    RequirementTracker,
     Resolver,
     Wheel,
+    WheelCache,
     cmdoptions,
     get_installed_distributions,
     install_req_from_editable,
diff --git a/piptools/_compat/pip_compat.py b/piptools/_compat/pip_compat.py
--- a/piptools/_compat/pip_compat.py
+++ b/piptools/_compat/pip_compat.py
@@ -1,11 +1,22 @@
 # -*- coding=utf-8 -*-
+from __future__ import absolute_import
+
 import importlib
+from contextlib import contextmanager
 
 import pip
 from pip._vendor.packaging.version import parse as parse_version
 
 PIP_VERSION = tuple(map(int, parse_version(pip.__version__).base_version.split(".")))
 
+try:
+    from pip._internal.req.req_tracker import RequirementTracker
+except ImportError:
+
+    @contextmanager
+    def RequirementTracker():
+        yield
+
 
 def do_import(module_path, subimport=None, old_path=None):
     old_path = old_path or module_path
@@ -50,6 +61,7 @@ def do_import(module_path, subimport=None, old_path=None):
 Link = do_import("models.link", "Link", old_path="index")
 Session = do_import("_vendor.requests.sessions", "Session")
 Resolver = do_import("legacy_resolve", "Resolver", old_path="resolve")
+WheelCache = do_import("cache", "WheelCache", old_path="wheel")
 
 # pip 18.1 has refactored InstallRequirement constructors use by pip-tools.
 if PIP_VERSION < (18, 1):
diff --git a/piptools/repositories/pypi.py b/piptools/repositories/pypi.py
--- a/piptools/repositories/pypi.py
+++ b/piptools/repositories/pypi.py
@@ -10,12 +10,15 @@
 
 from .._compat import (
     FAVORITE_HASH,
+    PIP_VERSION,
     Link,
     PyPI,
     RequirementSet,
+    RequirementTracker,
     Resolver as PipResolver,
     TemporaryDirectory,
     Wheel,
+    WheelCache,
     contextlib,
     is_dir_url,
     is_file_url,
@@ -37,22 +40,6 @@
 )
 from .base import BaseRepository
 
-from piptools._compat.pip_compat import PIP_VERSION
-
-try:
-    from pip._internal.req.req_tracker import RequirementTracker
-except ImportError:
-
-    @contextmanager
-    def RequirementTracker():
-        yield
-
-
-try:
-    from pip._internal.cache import WheelCache
-except ImportError:
-    from pip.wheel import WheelCache
-
 FILE_CHUNK_SIZE = 4096
 FileStream = collections.namedtuple("FileStream", "stream size")
 


Test patch: