Posts: 15
Threads: 2
Joined: Aug 2018
Hi!
What I know is that you can use pyinstaller or py2exe to compile a python script into an executable.
However what I want to know is that is it possible to write a python script that is able to create an EXE?
Like you can create text files with python but I want my python script to make a standalone executable.
Thanks
Posts: 5,151
Threads: 396
Joined: Sep 2016
You can just automate the required commands needed for your script to create exes. However that computer running that script would need python and any 3rd party libs, as well as pyinstaller or py2exe to be able to build the exes
Recommended Tutorials:
Posts: 15
Threads: 2
Joined: Aug 2018
Well yes I can do that but what I really want is the python code that can produce an EXE, if that is actually possible.
Posts: 1,034
Threads: 16
Joined: Dec 2016
(Aug-21-2018, 02:19 PM)ahmed_mokhles Wrote: Well yes I can do that but what I really want is the python code that can produce an EXE, if that is actually possible.
an exe from what? from your py file?
You can use python to call the 3rd party libs.
this is an example for cxfreeze (mysetup.py)
from cx_Freeze import setup, Executable, build
base = None
myname = "rotateVideo2"
desc = "rotateVideo2"
vers = "1.4"
executables = [Executable("/home/brian/Dokumente/python_files/rotateVideo2.py", base=base, targetName="rotateVideo2")]
build_exe_options = {"packages": ["idna"],
"include_files": [],
"excludes": ["PyQt4,matplotlib"],
"includes": [],
"optimize": 2,
"build_exe": "/tmp/rotateVideo2",
}
setup(
name = "rotateVideo2",
options = {"build_exe": build_exe_options},
version = vers,
description = desc,
executables = executables
) then i can do
python mysetup.py build
and it generates the application from my python file.
Posts: 5,151
Threads: 396
Joined: Sep 2016
(Aug-21-2018, 02:19 PM)ahmed_mokhles Wrote: Well yes I can do that but what I really want is the python code that can produce an EXE, if that is actually possible. yes that is what i meant. You use subprocess to call system commands to call python to build the exe
Recommended Tutorials:
Posts: 7,324
Threads: 123
Joined: Sep 2016
Aug-21-2018, 03:11 PM
(This post was last modified: Aug-23-2018, 09:15 AM by snippsat.)
(Aug-21-2018, 02:19 PM)ahmed_mokhles Wrote: Well yes I can do that but what I really want is the python code that can produce an EXE, if that is actually possible. Well that what't Pynstaller dos it create a stand alone .exe file.
Py2exe is not updated anymore(only up to Python 3.4),so don't use it.
# coin_toss.py
import random
import time
class Coin:
def __init__(self):
self.sideup = "Heads"
def toss(self):
if random.randrange(2) == 0:
self.sideup = "Heads"
else:
self.sideup = "Tails"
def toss_result():
my_coin = Coin()
print(f"This side is up: {my_coin.sideup}")
print("I am tossing the coin...")
time.sleep(4)
my_coin.toss()
print(f"This side is up: {my_coin.sideup}")
input('Press Enter to exit')
if __name__ == "__main__":
toss_result() # Make from command line cmd(i use cmder)
(coin_env) E:\div_code\flatten\coin_env
λ pyinstaller --onefile coin_toss.py
# cd into dist
(coin_env) E:\div_code\flatten\coin_env
λ cd dist
# One stand alone exe file in dist folder
(coin_env) E:\div_code\flatten\coin_env\dist
λ ls
coin_toss.exe*
# Test
(coin_env) E:\div_code\flatten\coin_env\dist
λ coin_toss.exe
This side is up: Heads
I am tossing the coin...
This side is up: Heads
Press Enter to exit
Posts: 1,034
Threads: 16
Joined: Dec 2016
But you have to keep in mind, if you use --onefile the app starts much slower than using directory.
Posts: 7,324
Threads: 123
Joined: Sep 2016
(Aug-21-2018, 03:47 PM)Axel_Erfurt Wrote: But you have to keep in mind, if you use --onefile the app starts much slower than using directory. Not so much that it would bather anyone for a reasonable sized program.
coin_toss.exe --> 0.1977 sec
mspaint.exe --> 0.1330 sec Most Windows user will say that mspaint start immediately.
For lager program it can look into directory approach.
Posts: 15
Threads: 2
Joined: Aug 2018
(Aug-21-2018, 03:06 PM)Axel_Erfurt Wrote: (Aug-21-2018, 02:19 PM)ahmed_mokhles Wrote: Well yes I can do that but what I really want is the python code that can produce an EXE, if that is actually possible.
an exe from what? from your py file?
You can use python to call the 3rd party libs.
this is an example for cxfreeze (mysetup.py)
from cx_Freeze import setup, Executable, build
base = None
myname = "rotateVideo2"
desc = "rotateVideo2"
vers = "1.4"
executables = [Executable("/home/brian/Dokumente/python_files/rotateVideo2.py", base=base, targetName="rotateVideo2")]
build_exe_options = {"packages": ["idna"],
"include_files": [],
"excludes": ["PyQt4,matplotlib"],
"includes": [],
"optimize": 2,
"build_exe": "/tmp/rotateVideo2",
}
setup(
name = "rotateVideo2",
options = {"build_exe": build_exe_options},
version = vers,
description = desc,
executables = executables
) then i can do
python mysetup.py build
and it generates the application from my python file.
This seems to be the most relevant reply. Anyway, I don't understand what's happening in the code so I installed cx freeze and I run "python filename.py build" but it says:
Error: ImportError: No module named 'idna'
Please can you explain what this is supposed to do and what is causing the error? Thanks
Posts: 1,034
Threads: 16
Joined: Dec 2016
it was an example from my app, you can remove it.
change the line
build_exe_options = {"packages": ["idna"]
to
build_exe_options = {"packages": []
and test again.
I wrote a GUI for cxfreeze - it is on github
(but tested in Linux only)
|