From 0626ced9d31d999ccecca375c4838a70240b471c Mon Sep 17 00:00:00 2001 From: Matthias Klose Date: Sun, 16 Jan 2022 17:12:18 +0000 Subject: [PATCH] ensurepip-disabled # DP: Disable ensurepip for the system installation, only enable it for virtual environments. # DP: Disable ensurepip for the system installation, only enable it for virtual environments. Gbp-Pq: Name ensurepip-disabled.diff --- Lib/ensurepip/__init__.py | 33 +++++++++++++++++++++++++++++++++ Lib/venv/__init__.py | 26 +++++++++++++++++++++++++- 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py index 9a50aa4..69d88ab 100644 --- a/Lib/ensurepip/__init__.py +++ b/Lib/ensurepip/__init__.py @@ -8,6 +8,34 @@ import subprocess from importlib import resources +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 install python3- + +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) + __all__ = ["version", "bootstrap"] _PROJECTS = [ @@ -75,6 +103,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/venv/__init__.py b/Lib/venv/__init__.py index 6f1af29..2f34190 100644 --- a/Lib/venv/__init__.py +++ b/Lib/venv/__init__.py @@ -310,7 +310,31 @@ class EnvBuilder: # intended for the global Python environment cmd = [context.env_exec_cmd, '-Im', 'ensurepip', '--upgrade', '--default-pip'] - subprocess.check_output(cmd, stderr=subprocess.STDOUT) + # Debian 2015-09-18 barry@debian.org: -venv is a separate + # binary package which might not be installed. In that case, the + # following command will produce an unhelpful error. Let's make it + # more user friendly. + try: + subprocess.check_output( + cmd, stderr=subprocess.STDOUT, + universal_newlines=True) + 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 python3-venv +package using the following command. + + apt install python{}-venv + +You may need to use sudo with that command. After installing the python3-venv +package, recreate your virtual environment. + +Failing command: {} +""".format(sysconfig.get_python_version(), cmd)) + sys.exit(1) + raise def setup_scripts(self, context): """ -- 2.30.2