Skip to main content

Portable python binaries

Project description

Portable python binaries

Version on pypi Tested with Github Actions Test coverage Python versions tested (link to github project)

This project is a python CLI that aims to make compiling portable python binaries automatable.

What is a “portable python”?

It’s a binary python distribution (.tar.gz or .zip) that can be decompressed in any folder, and used from there without further ado (ie: no need to run an “installer” and things like that).

The idea here is to allow for automated systems to:

  • Easily obtain a python binary, that can be used in sandboxes / workstations / laptops / instances…

  • Have an open source tool that can compile such portable pythons reliably, without having to worry about the compilation part.

  • Be able to inspect any python installation, and point out how portable it is, which shared or non-standard libraries it is using

Guiding principles

  • Focuses on just one thing: compile a portable python, and validate that it is indeed portable, produce outcome in (configurable) ./dist/ folder and that’s it

  • Only the last few non-EOL versions of python are supported (no historical stuff)

  • As time goes on, the code of this tool will evolve so that the latest pythons keep building (but won’t worry that older versions still keep building)

  • C compilation is done as simply as possible: no “patching” of any sort, rely solely on the upstream make/configure scripts, typically via stuff like --enable-shared=no

  • Builds are validated, an important part of the effort was to write up code that is able to inspect a python installation and detect whether it is portable or not (and why not if so).

  • cpython is initially supported, but aiming to compile any python family in the future (pypy, conda, …)

  • Cross-compilation would be cool, but only if upstream supports it (ie: only if doable via stuff like ./configure --host=... - no on-the-fly patching allowed)

For this repo itself:

  • Code is pure python, it is a CLI with one entry-point called portable-python

    • Can be ran in a debugger

    • 100% test coverage, has a --dryrun mode to help with testing / debugging / seeing what would be done quickly

    • No shell scripts (those are hard to maintain/test/debug)

    • Can be pip install-ed and reused

Installation

portable-python is a regular python CLI, it can be installed with:

pickley:

pickley install portable-python
portable-python --help

Or pipx:

pipx install portable-python

You can also run it directly (without explicit install) with pipx:

pipx run portable-python inspect /usr/bin/python3

Using pip install (a CI builder would probably do this):

/usr/bin/python3 -mvenv /tmp/pp
/tmp/pp/bin/python -mpip install portable-python
/tmp/pp/bin/portable-python --help
/tmp/pp/bin/portable-python inspect /usr/bin/python3

Invoke a build from python code:

from portable_python import BuildSetup

setup = BuildSetup("cpython:3.9.7")
setup.compile()

Invoke an inspection from python code:

from portable_python.inspector import PythonInspector

inspector = PythonInspector("/usr/bin/python3")
print(inspector.represented())
problem = inspector.full_so_report.get_problem(portable=True)
if problem:
    print("oops, it is not portable!: %s" % problem)

From source, contributions welcome!:

git clone https://github.com/codrsquad/portable-python.git
cd portable-python
tox -e venv
.venv/bin/portable-python --help
.venv/bin/portable-python inspect /usr/bin/python3

tox -e py39
tox -e style

Building a portable cpython

Once you’ve installed portable-python, you can get going like so:

# Build a binary
cd some-temp-folder
portable-python build 3.9.7
ls -l dist/cpython-3.9.7-macos-arm64.tar.gz

# Unpack it somewhere
tar -C ~/.pyenv/versions/ -xf dist/cpython-3.9.7-macos-arm64.tar.gz
ls -l ~/.pyenv/versions/

# It's ready to be used
~/.pyenv/versions/3.9.7/bin/python --version

Note that you can use --dryrun mode to inspect what would be done without doing it:

$ portable-python --dryrun build 3.9.7

INFO selected: xz openssl gdbm (3 modules) xz:5.2.5 openssl:1.1.1k gdbm:1.18.1
INFO Platform: macos-x86_64
...
--------------
-- xz:5.2.5 --
--------------
Would download https://tukaani.org/xz/xz-5.2.5.tar.gz
Would untar build/sources/xz-5.2.5.tar.gz -> build/components/xz
INFO env PATH=build/deps/bin:/usr/bin:/bin
INFO env MACOSX_DEPLOYMENT_TARGET=10.14
Would run: ./configure --prefix=build/deps --enable-shared=no --enable-static=yes ...
...
-------------------
-- cpython:3.9.7 --
-------------------
Would download https://www.python.org/ftp/python/3.9.7/Python-3.9.7.tar.xz
Would untar build/sources/Python-3.9.7.tar.xz -> build/components/cpython
...
Would run: ./configure --prefix=/ppp-marker/3.9.7 --enable-optimizations ...
Would run: /usr/bin/make
Would run: /usr/bin/make install DESTDIR=build
...
Would tar build/3.9.7 -> dist/cpython-3.9.7-macos-x86_64.tar.gz

Build folder structure

portable-python uses this file structure (build/ and dist/ folders configurable):

build/
    ppp-marker/3.9.7/                   # Full installation (after build completes)
    components/                         # Builds of statically compiled extension modules are here
    deps/                               # --prefix=.../deps passed to all component ./configure scripts
    sources/
        openssl-1.1.1k.tar.gz           # Downloaded artifacts (downloaded only once)
dist/
    cpython-3.9.7-macos-arm64.tar.gz    # Ready-to-go portable binary tarball

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

portable-python-1.3.5.tar.gz (34.0 kB view details)

Uploaded Source

Built Distribution

portable_python-1.3.5-py3-none-any.whl (36.1 kB view details)

Uploaded Python 3

File details

Details for the file portable-python-1.3.5.tar.gz.

File metadata

  • Download URL: portable-python-1.3.5.tar.gz
  • Upload date:
  • Size: 34.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for portable-python-1.3.5.tar.gz
Algorithm Hash digest
SHA256 7bdef0b01cf43b6718c13e7d2050827aac46670b4b0f6cef7631da1f89e33919
MD5 ae7b4cf0d7922fe5566b6d58d0646de4
BLAKE2b-256 a41ac96e3866466aa86b69b0e19028b685004033369288a0ba8b4d792aec9756

See more details on using hashes here.

File details

Details for the file portable_python-1.3.5-py3-none-any.whl.

File metadata

  • Download URL: portable_python-1.3.5-py3-none-any.whl
  • Upload date:
  • Size: 36.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for portable_python-1.3.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a4f8b0e763b003f72dc4c25fda83e5f4791522a5648b884a76b2805506e42f70
MD5 a91379119bbf9049c2202fc616ba477f
BLAKE2b-256 2dad8bc264688318e8865757b0139dbcccf666e23d424b25aee3e42d695cdceb

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page