Skip to content

Instantly share code, notes, and snippets.

@boozook
Last active October 30, 2024 06:52
Show Gist options
  • Save boozook/d693415ac0ec0291181241ae733ebc75 to your computer and use it in GitHub Desktop.
Save boozook/d693415ac0ec0291181241ae733ebc75 to your computer and use it in GitHub Desktop.
Install PIP for Pythonista 3.x

Usage

  1. run install pip.py, follow instructions
  • edit pip config that placed in the local root
  • edit your pythonista_startup.py, add PIP_CONFIG_FILE env
  1. reboot Pythonista
  2. run run.py, then type show pip or freeze

That's all. Now you can (un)install packages, upgrade pip, whatever.

  • ⚠️ Currently python-only non-binary packages are supported.

Also I recomend to make shortcut btn to run pip.py.

Screenshots: 1 2 3

# Inspired by: https://bartbroere.eu/2022/07/22/adding-pip-to-pythonista-ios/
import requests
import sys, os
from io import BytesIO
from zipfile import ZipFile
# Get the location of the Python 3 site-packages
site_packages = next(filter(lambda x: 'site-packages' in x, sys.path))
if not os.path.isdir(site_packages) or not os.path.exists(site_packages):
print('directory does not exist:', site_packages)
sys.exit(0)
# extract directly into site-packages
ZipFile(
BytesIO(
requests.get(
'https://files.pythonhosted.org/packages/90/a9/1ea3a69a51dcc679724e3512fc2aa1668999eed59976f749134eb02229c8/pip-21.3-py3-none-any.whl'
).content)).extractall(site_packages)
print("Downloaded pip")
msg = '''
I recommend to add following lines to your `pythonista_startup.py`:
if __name__ == '__main__':
pass
else:
import os
os.environ['PIP_CONFIG_FILE'] = os.path.join(os.path.expanduser('~'), 'Documents', 'pip.conf')
'''
print(msg)
cfg = os.path.join(os.path.expanduser('~'), 'Documents', 'pip.conf')
cfg_src = '''
[global]
verbose = 0
[install]
target = site-packages-3
no-dependencies = no
no-compile = yes
no-warn-script-location = false
'''
if not os.path.exists(cfg):
with open(cfg, 'w') as file:
file.write(cfg_src)
file.write('\n')
editor.open_file(cfg, new_tab=True)
else:
print('''Also add this to your pip config:
[install]
target = site-packages-3
''')
# Inspired: https://bartbroere.eu/2022/07/22/adding-pip-to-pythonista-ios/
# TODO: https://bartbroere.eu/2022/07/31/binary-wheels-pythonista/
import pip
from pip._internal.cli.main import main
import sys, os
def run(args):
try:
output = int(main(args))
if output != 0:
sys.exit(output)
except Exception as e:
print(str(e))
if __name__ == '__main__':
exit_req = False
while not exit_req:
query = None
try:
query = input('> pip ').strip()
except Exception as e:
print(str(e))
if query == 'q' or query == 'quit' or query == 'exit':
exit_req = True
sys.exit(103)
run(query.split())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment