File: ensurepip-disabled

package info (click to toggle)
pypy3 7.3.11%2Bdfsg-2%2Bdeb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 201,024 kB
  • sloc: python: 1,950,308; ansic: 517,580; sh: 21,417; asm: 14,419; cpp: 4,263; makefile: 4,228; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 11; awk: 4
file content (97 lines) | stat: -rw-r--r-- 3,805 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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      | 23 ++++++++++++++++++++++-
 2 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/lib-python/3/ensurepip/__init__.py b/lib-python/3/ensurepip/__init__.py
index 1854a04..3ce4dad 100644
--- a/lib-python/3/ensurepip/__init__.py
+++ b/lib-python/3/ensurepip/__init__.py
@@ -16,6 +16,35 @@ _PROJECTS = [
 ]
 
 
+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 bootstraping in a subprocess to avoid leaking any state that happens
     # after pip has executed. Particulary, this avoids the case when pip holds onto
@@ -90,6 +119,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 c847b24..83cbf73 100644
--- a/lib-python/3/venv/__init__.py
+++ b/lib-python/3/venv/__init__.py
@@ -346,7 +346,28 @@ 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: <python>-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:
+            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(cmd))
+            sys.exit(1)
 
     def setup_scripts(self, context):
         """