Skip to content

Instantly share code, notes, and snippets.

@ericsnowcurrently
Created February 27, 2020 00:07
Show Gist options
  • Save ericsnowcurrently/cfe5e614681757a23e50591bb1ace119 to your computer and use it in GitHub Desktop.
Save ericsnowcurrently/cfe5e614681757a23e50591bb1ace119 to your computer and use it in GitHub Desktop.
A workaround for Ubuntu's ensurepip problem.
"""
This is a script for working around the lack of ensurepip in
Ubuntu's system Python installs.
See: https://bugs.launchpad.net/ubuntu/+source/python3.4/+bug/1290847
An alternative is the following:
$ python3 -m venv --without-pip my-venv
$ curl https://bootstrap.pypa.io/get-pip.py
$ my-venv/bin/python3 get-pip.py
"""
import os
import os.path
import site
import sys
import urllib.request
# This should be compatible back a ways (3.8+ isn't).
ENSUREPIP_URL = 'https://github.com/python/cpython/raw/3.7/Lib/ensurepip'
FILES = (
'__init__.py',
'__main__.py',
'_uninstall.py',
'_bundled/pip-19.2.3-py2.py3-none-any.whl',
'_bundled/setuptools-41.2.0-py2.py3-none-any.whl',
)
def _download_ensurepip(pkgdir):
print('installing ensurepip from {}'.format(ENSUREPIP_URL))
os.makedirs(os.path.join(pkgdir, '_bundled'))
for name in FILES:
urllib.request.urlretrieve(
ENSUREPIP_URL + '/' + name,
os.path.join(pkgdir, os.path.normcase(name)),
)
def _ensure_ensurepip(stdlib):
try:
import ensurepip
except ImportError:
pkgdir = os.path.join(stdlib, 'ensurepip')
# We cannot install to site.USER_SITE since venv ignores it.
try:
_download_ensurepip(pkgdir)
except PermissionError:
msg = 'ERROR: could not open "{}/" for writing (need sudo?)'
print(msg.format(pkgdir), file=sys.stderr)
return
print('ensurepip installed at {!r}'.format(pkgdir))
else:
# XXX Support --force?
# XXX Make sure it's the right one?
print('ensurepip already installed: {!r}'.format(ensurepip.__file__))
return
##################################
# the script
def main():
stdlib = os.path.dirname(site.__file__)
_ensure_ensurepip(stdlib)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment