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.
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!).
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.
Build Python Standalone Executable for Linux using Windows Machine
No, there is no way to do this with PyInstaller and just a Windows development machine.
PyInstaller creates executables for the system you're running it on. If you run it on a Windows machine, you get a Windows executable.
In the past, I solved this problem by creating a Linux development VM, and running PyInstaller there; it creates a Linux executable appropriately.
Convert a Python package into a standalone application
Solved!
I simply added a path to my package in site-package directory of python, then wrote the main script that uses this package to get my task done. I then used pyinstaller to make a binary of this main script and it automatically included and complied all the dependencies that includes this very package i created.
I essentially copied the __main__.py
as that had all the code that was doing the final task. Working like a charm :)
If anyone has any suggestions or a better way, please suggest!
Related Topics
How to Change a Global Variable from Within a Function
How to Find the Owner of a File or Directory in Python
Multiple Instances of Python Running Simultaneously Limited to 35
Maltparser Giving Error in Nltk
Python Tkinter: Attach Scrollbar to Listbox as Opposed to Window
Trouble Importing Tabulate in Python 3.4
Process Dies, If It Is Run via Paramiko Ssh Session and with "&" in the End
What Does -≫ Mean in Python Function Definitions
Overriding Special Methods on an Instance
Python Script to List Users and Groups
Default Buffer Size for a File on Linux
How to Make Python Script Press 'Enter' When Prompted on Shell
Making Python Script Accessible System Wide