Skip to content

Instantly share code, notes, and snippets.

@boozook
Last active January 1, 2026 08:11
Show Gist options
  • Select an option

  • Save boozook/d693415ac0ec0291181241ae733ebc75 to your computer and use it in GitHub Desktop.

Select an option

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())
@M4nw3l
Copy link

M4nw3l commented Jan 1, 2026

Couple of small fixes/improvements for this:

  • Install pip and set packages location in pip.conf with an absolute path for site-packages-3 (otherwise installs can be relative to install.py and run.py file locations)
  • Add missing import editor in install.py
  • Filter out “pip” or “pip3” strings from first argument in run.py for easier pasting of pip commands

install.py

import requests
import sys, os
import editor
from io import BytesIO
from zipfile import ZipFile

# Get the location of the Python 3 site-packages
site_packages = os.path.join(os.path.expanduser('~'), 'Documents', 'site-packages-3')
print(‘installing pip to directory:’, site_packages)

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 = '''
Add the following lines to your `pythonista_startup.py`:

if __name__ == '__main__':
	pass

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+'''

no-dependencies = no
no-compile = yes
no-warn-script-location = false
'''

with open(cfg, 'w') as file:
	file.write(cfg_src)
	file.write('\n')
	
editor.open_file(cfg, new_tab=True)

run.py

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)
		query = query.split()
		if query[0] == 'pip' or query[0] == 'pip3': # ignore pip if first arg for pasting commands
			query = query[1:]
		run(query)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment