Products > Programming
[SOLVED] Python application distributed as a single standalone executable file
RoGeorge:
Is it possible to pack a python script/application + Python interpreter in a single file that will just run on another machine that does not have Python already installed?
To be more precise:
- I have a python script I made, called 'instrument.py', running fine on my dev machine
- I want to run this script on another clean machine, that does not have Python installed, and doesn't have internet
- preferably would be a "instrument.exe" file that is self contained, and just run on any other clean machine
- could be a different file for each OS, i.e. an "instrument.exe" for windows and another file "instrument" for Linux would be just fine
Is it possible to distribute a Python application as a single executable file? How?
[SOLVED]
--- Code: ---# install pip
sudo apt-get update
sudo apt-get install python-pip
# install PyInstaller
pip install pyinstaller
# turn a python script into a standalone executable file
pyinstaller my_python_code.py --onefile --clean
# to run the standalone executable file
./dist/my_python_code
--- End code ---
mariush:
You can make an installer for your application, a single executable.
It will ask user for the destination folder and then extract the files there.
See InnoSetup for example: http://www.jrsoftware.org/isinfo.php
or the more basic (interface wise) NSIS : https://nsis.sourceforge.io/Main_Page
As for making the script an exe, afaik Python is an interpreted language.
You can bundle your script with the minimal stuff required for the script to run (some kind of python runtime) and you can make a sort of very small executable (50-100 KB) which simply starts that runtime with your script as parameter.
I don't know how to do it, but a good example of an application made using Python is calibre: https://calibre-ebook.com/
The author has a detailed build instructions here - https://github.com/kovidgoyal/build-calibre - but it's a really complicated application with lots of dependencies, so I'm not sure how the actual executables are created by the build process.
Googling results in a recommendation for PyInstaller, maybe this would be enough for your needs:
official page: https://www.pyinstaller.org/
some articles:
https://ourcodeworld.com/articles/read/273/how-to-create-an-executable-exe-from-a-python-script-in-windows-using-pyinstaller
https://datatofish.com/executable-pyinstaller/
RoGeorge:
I've already tried PyInstaller, and it worked only for the native Python installation. If I'm using Python virtual environments, or conda, PyInstaller fails.
Will look at the other methods you mentioned, thank you.
In the meantime I learned how to build Python from sources. This way I can end up with a Python contained in a single folder, and can run it from that folder. Additional modules can be installed with pip locally, in the built Python. Thus, I can end up with everything in a single folder, zip that folder and unzip it later to another machine, and it should run without any installation. If launched by a local script that defines PYTHONPATH environment variable, it should run in that folder only, without going to other eventual Python installations that might coexist on the same machine.
Not sure if this method is the proper way to do it.
mariush:
I was considering learning Python but I've decided to learn Go (Golang) first : https://golang.org/
It's quite easy to learn and has built in concurrency stuff that makes it easier to make applications that do lots of things at the same time.
On top of that, it compiles everything statically as native windows or linux executable, without any runtimes required (makes the actual exe a bit big because of this though, around 500 KB after compressing it with upx)
The biggest downside is that it doesn't have any native GUI libraries or functions, by default you can make only command prompt applications.
There are packages which makes it possible to use qtWidgets, gtk , and other things and create windows and menus and stuff.
It's also super trivial to use the built in web server to simply create a local web server serving a single website using javascript and css and html and serve the files from within the executable and javascript in browser can exchange packets of data with the server through websockets or json or other means.
There's also packages that work like electronJS (basically a miniature web browser that creates your application window)
SiliconWizard:
A minimal Python runtime will still be gigantic, right? Do you have any figure to get an idea?
Navigation
[0] Message Index
[#] Next page
Go to full version