File: install_pip.py

package info (click to toggle)
python-psutil 7.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,312 kB
  • sloc: python: 19,411; ansic: 15,378; makefile: 465; javascript: 153; sh: 54
file content (48 lines) | stat: -rwxr-xr-x 1,074 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
#!/usr/bin/env python3

# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import sys

try:
    import pip  # noqa: F401
except ImportError:
    pass
else:
    print("pip already installed")
    sys.exit(0)

import os
import ssl
import tempfile
from urllib.request import urlopen

URL = "https://bootstrap.pypa.io/get-pip.py"


def main():
    ssl_context = (
        ssl._create_unverified_context()
        if hasattr(ssl, "_create_unverified_context")
        else None
    )
    with tempfile.NamedTemporaryFile(suffix=".py") as f:
        print(f"downloading {URL} into {f.name}")
        kwargs = dict(context=ssl_context) if ssl_context else {}
        req = urlopen(URL, **kwargs)
        data = req.read()
        req.close()

        f.write(data)
        f.flush()
        print("download finished, installing pip")

        code = os.system(f"{sys.executable} {f.name} --user --upgrade")

    sys.exit(code)


if __name__ == "__main__":
    main()