Executable Python Program with All Dependencies for Linux

Executable Python program with all dependencies for Linux

Use PyInstaller in Linux based systems
PyInstaller is a program used to convert Python scripts into standalone deployable applications.

Install PyInstaller from PyPI:

pip install pyinstaller

Go to your program’s directory and run:

pyinstaller yourprogram.py

This will generate the bundle in a subdirectory called dist

You can use -onefile argument in order to generate the bundle with
only a single executable file.

How can I make a Python script standalone executable to run without ANY dependency?

You can use py2exe as already answered and use Cython to convert your key .py files in .pyc, C compiled files, like .dll in Windows and .so on Linux.

It is much harder to revert than common .pyo and .pyc files (and also gain in performance!).

Packaging a Python script on Linux into a Windows executable

Did you look at PyInstaller?

It seems that versions through 1.4 support cross-compilation (support was removed in 1.5+). See this answer for how to do it with PyInstaller 1.5+ under Wine.

Documentation says:

Add support for cross-compilation: PyInstaller is now able to build Windows executables when running under Linux. See documentation for more details.

I didn't try it myself.

I hope it helps

How to create standalone program(on linux) from my python code?

You can try PyInstaller. It is easy to use for a simple case.

pip install pyinstaller

then for a single-file executable

pyinstaller covid_19.py --onefile

will generate the executable in the \dist directory.

PyInstaller is not cross platform, so on Windows, for example, it will create a .exe file. If you wanted an executable to work on, say, MacOS, you would need to compile on that system.

Running compiled Linux executable on other Linux computers

The problem is that you have different C system libraries on the two machines. There can sometimes be difficulties when porting a pre-built binary. Either you ensure that you're using a similar environment or you put all dependencies into the binary - this may increase its size significantly. To do the latter you need to use the ´--static´ keyword during compilation. I'm not sure, though, whether this is enough.

EDIT:

Since this is a pure Python project, you should make sure that the used Python version is compatible, i.e., same Python version and also the same Python build (CPython, etc.).

How to package Python Project into a standalone executable?

PyInstaller

PyInstaller is a program that freezes (packages) Python programs into stand-alone executables, under Windows, GNU/Linux, Mac OS X, FreeBSD, Solaris and AIX. Its main advantages over similar tools are that PyInstaller works with Python 2.7 and 3.4—3.7, it builds smaller executables thanks to transparent compression, it is fully multi-platform, and use the OS support to load the dynamic libraries, thus ensuring full compatibility.

It work even if users do not have python installed.

Here an example from a github project. As you can see, you can download sources, but also a zip containing every package used to run your project. In this example, it contains many files, but you can package everything into a single .exe file.

How to use (Manual):

Install PyInstaller from PyPI:

pip install pyinstaller

Go to your program’s directory and run:

pyinstaller yourprogram.py

This will generate the bundle in a subdirectory called dist.

You can use --onefile argument in order to generate the bundle with only a single executable file.

Case specific:

You asked how to get arguments send by the user. Here is some way to do it, more or less convenient:

  • Use input(),
  • Use a config file,
  • Use default parameter.


Related Topics



Leave a reply



Submit