Skip to content

Commit 34b1aad

Browse files
committed
Bundle pip in the base Python AppImages
1 parent 7a02dd5 commit 34b1aad

2 files changed

Lines changed: 62 additions & 12 deletions

File tree

‎README.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ packages we recommmed extracting the AppImage, e.g. as:
2525

2626
```sh
2727
./python3.8.2-cp38-cp38-manylinux1_x86_64.AppImage --appimage-extract
28-
mv squashfs-root python3.8.2-cp38-cp38-manylinux1_x86_64.AppDir
28+
mv squashfs-root python3.8
2929
rm -f python3.8.2-cp38-cp38-manylinux1_x86_64.AppImage
3030

31-
ln -s python3.8.2-cp38-cp38-manylinux1_x86_64.AppDir/AppRun python3.8
31+
export PATH="$(pwd)/python3.8/usr/bin:${PATH}
3232
```
3333
3434
Then, extra packages can be installed to the extracted AppDir using `pip`. For
35-
example updating pip can be done as:
35+
example upgrading pip can be done as:
3636
3737
```sh
38-
./python3.8 -m pip install -U pip
38+
pip install --upgrade pip
3939
```
4040
4141

‎python_appimage/appimage/relocate.py‎

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ def relocate_python(python=None, appdir=None):
8585
FULLVERSION = '{:}.{:}.{:}'.format(*sys.version_info[:3])
8686
VERSION = '.'.join(FULLVERSION.split('.')[:2])
8787
PYTHON_X_Y = 'python' + VERSION
88+
PIP_X_Y = 'pip' + VERSION
89+
PIP_X = 'pip' + VERSION[0]
8890

8991
APPDIR = os.path.abspath(appdir)
9092
APPDIR_BIN = APPDIR + '/usr/bin'
@@ -123,6 +125,30 @@ def relocate_python(python=None, appdir=None):
123125
copy_tree(HOST_PKG, PYTHON_PKG)
124126
copy_tree(HOST_INC, PYTHON_INC)
125127

128+
pip_source = HOST_BIN + '/' + PIP_X_Y
129+
if not os.path.exists(pip_source):
130+
pip_source = HOST_BIN + '/' + PIP_X
131+
if os.path.exists(pip_source):
132+
with open(pip_source) as f:
133+
f.readline()
134+
body = f.read()
135+
136+
target = PYTHON_BIN + '/' + PIP_X_Y
137+
with open(target, 'w') as f:
138+
f.write('#! /bin/sh\n')
139+
f.write(' '.join((
140+
'"exec"',
141+
'"$(dirname $(readlink -f ${0}))/' + PYTHON_X_Y + '"',
142+
'"$0"',
143+
'"$@"\n'
144+
)))
145+
f.write(body)
146+
shutil.copymode(pip_source, target)
147+
148+
relpath = os.path.relpath(target, APPDIR_BIN)
149+
make_tree(APPDIR_BIN)
150+
os.symlink(relpath, APPDIR_BIN + '/' + PIP_X_Y)
151+
126152

127153
# Remove unrelevant files
128154
log('PRUNE', '%s packages', PYTHON_X_Y)
@@ -159,20 +185,44 @@ def relocate_python(python=None, appdir=None):
159185
elif version.startswith('3') and version >= latest3:
160186
latest3 = version
161187
if latest2 == VERSION:
162-
remove_file(APPDIR_BIN + '/python2')
163-
os.symlink(PYTHON_X_Y, APPDIR_BIN + '/python2')
188+
python2 = APPDIR_BIN + '/python2'
189+
remove_file(python2)
190+
os.symlink(PYTHON_X_Y, python2)
191+
has_pip = os.path.exists(APPDIR_BIN + '/' + PIP_X_Y)
192+
if has_pip:
193+
pip2 = APPDIR_BIN + '/pip2'
194+
remove_file(pip2)
195+
os.symlink(PIP_X_Y, pip2)
164196
if latest3 == '0.0':
165197
log('SYMLINK', 'python, python2 to ' + PYTHON_X_Y)
166-
remove_file(APPDIR_BIN + '/python')
167-
os.symlink('python2', APPDIR_BIN + '/python')
198+
python = APPDIR_BIN + '/python'
199+
remove_file(python)
200+
os.symlink('python2', python)
201+
if has_pip:
202+
log('SYMLINK', 'pip, pip2 to ' + PIP_X_Y)
203+
pip = APPDIR_BIN + '/pip'
204+
remove_file(pip)
205+
os.symlink('pip2', pip)
168206
else:
169207
log('SYMLINK', 'python2 to ' + PYTHON_X_Y)
208+
if has_pip:
209+
log('SYMLINK', 'pip2 to ' + PIP_X_Y)
170210
elif latest3 == VERSION:
171211
log('SYMLINK', 'python, python3 to ' + PYTHON_X_Y)
172-
remove_file(APPDIR_BIN + '/python3')
173-
os.symlink(PYTHON_X_Y, APPDIR_BIN + '/python3')
174-
remove_file(APPDIR_BIN + '/python')
175-
os.symlink('python3', APPDIR_BIN + '/python')
212+
python3 = APPDIR_BIN + '/python3'
213+
remove_file(python3)
214+
os.symlink(PYTHON_X_Y, python3)
215+
python = APPDIR_BIN + '/python'
216+
remove_file(python)
217+
os.symlink('python3', python)
218+
if os.path.exists(APPDIR_BIN + '/' + PIP_X_Y):
219+
log('SYMLINK', 'pip, pip3 to ' + PIP_X_Y)
220+
pip3 = APPDIR_BIN + '/pip3'
221+
remove_file(pip3)
222+
os.symlink(PIP_X_Y, pip3)
223+
pip = APPDIR_BIN + '/pip'
224+
remove_file(pip)
225+
os.symlink('pip3', pip)
176226

177227

178228
# Set a hook in Python for cleaning the path detection

0 commit comments

Comments
 (0)