I used the recent version pyinstaller with the option --onefile to create one stand alone file of my python script. On my Mac it works just fine if I open the file in the terminal (bash shell), but in the Linux bash I get the following error
bash: ./myprog: cannot execute binary file
Is there something I am missing here?
pyinstaller creates an executable that will work on the machine it is run on. So if you run pyinstaller on Windows, it creates an executable for Windows. Same for Mac, Linux, etc, so I'd try running pyinstaller on your Linux box to produce a working executable for that environment. Mac executables are not Linux executables.
This is because (as I understand it) the underlying Python includes platform-specific implementations of certain things. For instance, the os module has a bunch of conditional, platform-dependent imports that will be bundled into the executable. Since it only has access to whatever binaries are available on the platform pyinstaller is running on, it can't produce a version for other platforms.
Linux checks magic number of executable file,
magic number of a Linux executable file starts with "DLE elf"
execute "od -c YUPUR_FILE" and see result
Related
My platform is Windows 10 and Python 3.9. There is another computer(Windows server 2008R2) without Python. So I'd like to use pyinstaller in my computer and use .exe on the other computer.
I tried simple script print("hello") and used pyinstaller -F myscript.py
.exe works on my computer, but failed on the other computer.
Error
error loading python dll ~ python39.dll
Should I use Python 3.8? Or what should I do?
The problem is that Pyinstaller does not create fully standalone executables, it creates dependencies (E.g. this python39.dll), so this python39.dll should be on the computer which is running this executable. Because python is already installed on your computer, python39.dll is already there and everything works fine. The problem is that machine that you're running this program on probably won't have it.
To fix this there are several solutions:
Install python 3.9 on targets' machine (But in this case you don't need to create an executable)
Include python39.dll with your program
For second solution just create a folder and move your executable into it as well as this python39.dll library. Windows will find it because it's in the same directory where this executable is. You can get this library from c:\Windows\System32 folder (Or where all DLL's are stored on your system) and then just copy it into folder with your executable. After that ship not just executable but this folder with library included.
#Stepan wrote in comments that you can also include this library right in your executable by adding --add-binary "path\to\python39.dll" to your command when compiling. The final command will look like this:
pyinstaller -F --add-binary "c:\Windows\System32\python39.dll" myscript.py
Check if the Python version is compatible with the windows version you are trying to use. I was having this problem with an exe I did using Python 3.10. Did it again with Python 3.7 and it worked.
In such cases it could be a solution to use something like auto-py-to-exe wrap for pyistaller: it knows better which option to set for py converting :)
Also, from my exp, in some cases you should modify yout already normally working from terminal Py code before pyinstaller: for example replace exit() with sys.exit() and so on.
I've made a simple python application that I want to distribute for beta testing, I am on a windows computer and my beta testing Operating Systems include windows, mac, and ubuntu.
My question is how would I compile the program to a standalone application on Mac and Linux (Windows is already sorted)
From my experience, you cannot create a standalone version of a Python program for a different OS then the one you're using for compiling.
I'm a Mac user, and when I want to generate a Windows Executable, I use a Windows that I installer at my VM, and compile from there. The same for Linux.
In any case, I use pyinstaller lib to generate an executable, which is really simple:
1. Install the lib: pyinstaller.readthedocs.io/en/v3.3.1/installation.html
2. On your terminal, walk to the folder where is your .py file
3. Just type pyinstaller --onefile filename.py
4. 2 new folders will be created at the folder, dist and build. So just open the dist folder to find your executable
Here you see I use the --onefile parameter, and I find best, because without it, your dist folder will generate a folder full of content from each dependency and other aspects, and it's not neat to share this with users. With --onefile the users will only find one executable file.
I have made a python file and converted it to executable file with pyinstaller in Ubuntu, now I want to run the same executable file in windows, how would I do that?
Oups... Programs exists in different formats, normally text (.py for Python), byte code (.pyc) for Python and native executable formats. Text is normally portable across any architecture except for possible charset conversions. Byte code is normally portable, I am sure for Java, less for Python because I have never used it. But native executable formats (true executable in Linux or .exe files on Windows) are dedicated to one single architecture.
So a program generated by pyinstaller on Ubuntu can only be used on a Linux system with same system libraries as yours (read Ubuntu same version, even if it should also run on some other Linux flavours), but definitely not on Windows.
You have to install pyinstaller on windows, and run pyinstaller on windows (From the OS that you want to build your exe file)
I have a python script I run using Cygwin and I'd like to create a clickable icon on the windows desktop that could run this script without opening Cygwin and entering in the commands by hand. how can I do this?
The simplest solution will be creating batch file containing command like:
c:\python27\python.exe c:\somescript.py
With this solution you will have to have installed python interpreter. If you need more portable solution you can try for e.g. py2exe and bundle python scripts into executable file, able to run without requiring a Python installation.
This comes straight from Python Docs (https://docs.python.org/3.3/using/windows.html):
3.3.5. Executing scripts without the Python launcher
Without the Python launcher installed, Python scripts (files with the extension .py) will be executed by python.exe by default. This executable opens a terminal, which stays open even if the program uses a GUI. If you do not want this to happen, use the extension .pyw which will cause the script to be executed by pythonw.exe by default (both executables are located in the top-level of your Python installation directory). This suppresses the terminal window on startup.
You can also make all .py scripts execute with pythonw.exe, setting this through the usual facilities, for example (might require administrative rights):
Launch a command prompt.
Associate the correct file group with .py scripts:
assoc .py=Python.File
Redirect all Python files to the new executable:
ftype Python.File=C:\Path\to\pythonw.exe "%1" %*
This tutorial shows how to create a batch file that runs a python script.
Note that many of the other answers are out of date - py2exe is out of support past python 3.4. More info here.
As an alternative to py2exe you could use the pyinstaller package with the onefile flag. This is a solution which works for python 3.x.
install pyinstaller via pip
Package your file to a single exe with the onefile flag
pyinstaller --onefile your_file.py
A better way (in my opinion):
Create a shortcut:
Set the target to
%systemroot%\System32\cmd.exe /c "python C:\Users\MyUsername\Documents\MyScript.py"
Start In:
C:\Users\MyUsername\Documents\
Obviously change the path to the location of your script. May need to add escaped quotes if there is a space in it.
The solution that worked like a charm for me >
From https://www.tutorialexample.com/convert-python-script-to-exe-using-auto-py-to-exe-library-python-tutorial/
pip install auto-py-to-exe
The GUI is available just by typing:
auto-py-to-exe
Then, I used this command to generate the desired output:
pyinstaller --noconfirm --onedir --windowed --icon "path/favicon.ico" "path/your_python_script"
Now I have my script as executable on taskbar
I was using c shell of unix. I have created a script in python with file extension .py.
Now I want to convert this .py file into an .exe file in unix's c shell.
I found that py2exe package will not work for csh, but there is freeze.py which I could use to convert says Google.
But I am unable to use this in my case. When I use python freeze.py hello.py it raises an error: freeze.py not found
Is there any other way to it in UNIX.
The simple answer is "No". Although there is an option: use WINE with PyInstaller.
PyInstaller is a program that converts (packages) Python programs into
stand-alone executables, under Windows, Linux, Mac OS X, Solaris and
AIX. Its main advantages over similar tools are that PyInstaller works
with any version of Python since 2.2, 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.