- Saved searches
- Use saved searches to filter your results more quickly
- License
- codrsquad/portable-python
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.rst
- About
- Keep These Portable Python Builds for Linux Always With You
- Python Standalone Builds
- Portable PyPy for Linux
- Anaconda
- About the author
- Nitesh Kumar
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
License
codrsquad/portable-python
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.rst
Portable-Python is a CLI (and a python library) for compiling python binaries from source than can be decompressed in any folder, and used from there without further ado (ie: no need to run an «installer»).
The idea here is to allow for automated systems to:
- Easily obtain a python binary, that can be used in sandboxes / workstations / laptops / instances.
- Inspect any python installation, and point out how portable it is, which shared or non-standard libraries it is using
portable-python is a regular python CLI, it can be installed with:
pickley install portable-python portable-python --help portable-python inspect /usr/bin/python3
pipx install portable-python 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
Supported operating systems
Portable python binaries can be built for Linux and MacOS (Intel/M1/M2).
Currently Windows is NOT supported, contributions are welcome.
Python binaries can be produced as «portable» (statically linked, can run from any folder where the binary is unpacked in), or with a —prefix (build targeted to live in a pre-determined folder, like /apps/pythonM.m )
Operating system | Portable | —prefix |
---|---|---|
Linux | ✅ | ✅ |
Macos | ✅ | ✅ |
Windows | ❌ | ❌ |
Building a portable cpython
Once portable-python is installed:
cd some-temp-folder portable-python build 3.9.7 ls -l dist/cpython-3.9.7-macos-arm64.tar.gz
tar -C ~/tmp/versions/ -xf dist/cpython-3.9.7-macos-arm64.tar.gz ls -l ~/tmp/versions/
~/tmp/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
Portable Python can be used as a python library to invoke builds, or inspect an installation.
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
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
- 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
- No patches: C compilation is done as simply as possible without modifying the upstream source code. Rely solely on the 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).
- 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)
- 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
About
Keep These Portable Python Builds for Linux Always With You
Most Linux distributions come with pre-installed Python packages. These packages are deeply integrated into the OS and they depend on shared libraries. If you want to keep Python projects isolated, using virtual environments is an excellent option. Another realistic option would be to use Python installed on an external drive as a portable package that you can carry it at your convenience (useful for teaching for example).
Unfortunately, as far as portability of Python is concerned, Linux users don’t have much choice. While compiling Python with shared libraries is easy, building Python with statically linked libraries takes more than a few tweaks, patches and changing lines in source code.
This article will list the few portable Python options available for Linux. Note that any of the methods mentioned below will work on external drives formatted in NTFS or EXT3/EXT4 file systems only. FAT32 file system doesn’t support symlinks which is a requirement for these packages to work.
Python Standalone Builds
Python Standalone Builds is a project that builds and distributes portable Python versions. These builds can be stored on external media like USB sticks and they include a full featured Python installation along with all of its standard libraries.
Various Python builds are available for Windows, macOS, and Linux on its releases page. These builds are compiled with a huge focus on static libraries instead of shared ones (OpenSSL is statically linked for example).
You can download builds from here. These builds have “CPython” in their filename which is the same official version of Python that you can download from the official Python website. To extract the downloaded file, you will have to run the commands below:
Using the command above, you will get a tar file that can be extracted using your file or archive manager. After extracting, you can find Python executable located in “/path/to/extracted/folder/python/install/bin/” directory.
To execute a Python file using this version of Python, you have to run:
Note that you will have to specify the full path of the Python executable or use “dot slash (./)” from a terminal opened in the directory of the executable.
Portable PyPy for Linux
Portable PyPy, as the name suggests, provides portable PyPy builds for various Linux distributions. PyPy is an alternative implementation of the Python language that focuses on speed with its “Just-in-Time” compiler. In many scenarios, its performance is comparable to Java and C.
Using these builds is as easy as downloading a portable PyPy archive, uncompressing it and then running the Python binary instantly without installing any extra libraries. These builds are tested to be working on various DEB and RPM based distributions that include RHEL/Centos 6 and later, Fedora, SuSE Linux, Ubuntu and Debian stable. PyPy binaries should run on any distribution that includes glibc 2.17.
You can download the latest build from here. Downloaded tar.bz2 file can be extracted using your file or archive manager.
After extracting, you can find Python executable located in “/path/to/extracted/folder/bin/” directory.
To execute a Python file using this version of Python, you have to run:
As stated earlier, you will have to specify the full path of the Python executable or use “dot slash (./)” from a terminal opened in the directory of the executable.
Anaconda
Anaconda is a free and open-source distribution of the Python for scientific computing (data science, machine learning etc.). It has its own package manager “conda” that aims to simplify package management and deployment, though you can also use pip package manager with it after some tinkering.
Anaconda provides a command line installer for Linux and it can be installed anywhere, even directly on external drives. You can find the installer here.
After downloading, run the .sh installer file and follow the on screen instructions. You can directly specify path to external USB drive or later copy the installation folder.
Once the installation has finished, you can find Python executable located in “/path/to/installation/folder/bin/” directory.
To execute a Python file using this version of Python, you have to run:
Note that you will have to specify full path of the Python executable or use “dot slash (./)” from a terminal opened in the directory of the executable.
These are the only three options available today that provides precompiled, portable binaries of Python for Linux. If you are storing them on an external drive, consider keeping them in compressed archive format and then extract them on the host OS where USB drive is plugged in. Usually internal drives have superior read and write speeds and Python binaries running from these internal drives will give you better performance than those running in external media. Of course, you always have a choice to run Python directly from an extracted folder stored in an external drive, if you prefer that.
About the author
Nitesh Kumar
I am a freelancer software developer and content writer who loves Linux, open source software and the free software community.