From: Stefano Rivera <stefanor@debian.org>
Date: Sat, 7 Oct 2017 09:38:58 +0200
Subject: Debian: Disable ensurepip in Debian for now

Origin: Debian cpython packaging
Last-Update: 2017-05-21
---
 lib-python/3/ensurepip/__init__.py | 34 ++++++++++++++++++++++++++++++++++
 lib-python/3/venv/__init__.py      | 22 ++++++++++++++++++++--
 2 files changed, 54 insertions(+), 2 deletions(-)

diff --git a/lib-python/3/ensurepip/__init__.py b/lib-python/3/ensurepip/__init__.py
index 165f396..e6d1d38 100644
--- a/lib-python/3/ensurepip/__init__.py
+++ b/lib-python/3/ensurepip/__init__.py
@@ -77,6 +77,35 @@ def _get_packages():
 _PACKAGES = None
 
 
+def _ensurepip_is_disabled_in_debian_for_system():
+    # Detect if ensurepip is being executed inside of a python-virtualenv
+    # environment and return early if so.
+    if hasattr(sys, 'real_prefix'):
+        return
+
+    # Detect if ensurepip is being executed inside of a stdlib venv
+    # environment and return early if so.
+    if sys.prefix != getattr(sys, "base_prefix", sys.prefix):
+        return
+
+    # If we've gotten here, then we are running inside of the system Python
+    # and we don't want to use ensurepip to install into the system Python
+    # so instead we'll redirect the user to using dpkg and apt-get.
+    print('''\
+ensurepip is disabled in Debian/Ubuntu for the system python.
+
+Python modules for the system python are usually handled by dpkg and apt-get.
+
+    apt-get install python3-<module name>
+
+Install the python3-pip package to use pip itself.  Using pip together
+with the system python might have unexpected results for any system installed
+module, so use it on your own risk, or make sure to only use it in virtual
+environments.
+''')
+    sys.exit(1)
+
+
 def _run_pip(args, additional_paths=None):
     # Run the bootstrapping in a subprocess to avoid leaking any state that happens
     # after pip has executed. Particularly, this avoids the case when pip holds onto
@@ -146,6 +175,11 @@ def _bootstrap(*, root=None, upgrade=False, user=False,
 
     Note that calling this function will alter both sys.path and os.environ.
     """
+
+    # Ensure that we are only running this inside of a virtual environment
+    # of some kind.
+    _ensurepip_is_disabled_in_debian_for_system()
+
     if altinstall and default_pip:
         raise ValueError("Cannot use altinstall and default_pip together")
 
diff --git a/lib-python/3/venv/__init__.py b/lib-python/3/venv/__init__.py
index 91d6288..95a1c8a 100644
--- a/lib-python/3/venv/__init__.py
+++ b/lib-python/3/venv/__init__.py
@@ -431,8 +431,26 @@ class EnvBuilder:
 
     def _setup_pip(self, context):
         """Installs or upgrades pip in a virtual environment"""
-        self._call_new_python(context, '-m', 'ensurepip', '--upgrade',
-                              '--default-pip', stderr=subprocess.STDOUT)
+        try:
+            self._call_new_python(context, '-m', 'ensurepip', '--upgrade',
+                                  '--default-pip', stderr=subprocess.STDOUT)
+        except subprocess.CalledProcessError:
+            stdlib = sysconfig.get_path('stdlib')
+            if not os.path.exists(f'{stdlib}/ensurepip/__main__.py'):
+                print("""\
+The virtual environment was not created successfully because ensurepip is not
+available.  On Debian/Ubuntu systems, you need to install the pypy3-venv
+package using the following command.
+
+    apt-get install pypy3-venv
+
+You may need to use sudo with that command.  After installing the pypy3-venv
+package, recreate your virtual environment.
+
+Failing command: {}
+""".format(context.env_exec_cmd))
+                sys.exit(1)
+            raise
 
     def setup_scripts(self, context):
         """
