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:

$ sudo apt install zstd
$ unzstd your/path/to/downloaded_file.tar.zst

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:

$ /path/to/extracted/folder/python/install/bin/python3 file.py

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:

$ /path/to/extracted/folder/bin/python3 file.py

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:

$ /path/to/installation/folder/bin/python3 file.py

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.



from Linux Hint https://ift.tt/2QLbsBL

Post a Comment

0 Comments