Python Forum
How can I make a python script create an EXE? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How can I make a python script create an EXE? (/thread-12350.html)

Pages: 1 2 3


How can I make a python script create an EXE? - ahmed_mokhles - Aug-21-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


RE: How can I make a python script create an EXE? - metulburr - Aug-21-2018

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


RE: How can I make a python script create an EXE? - ahmed_mokhles - Aug-21-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.


RE: How can I make a python script create an EXE? - Axel_Erfurt - Aug-21-2018

(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.


RE: How can I make a python script create an EXE? - metulburr - Aug-21-2018

(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


RE: How can I make a python script create an EXE? - snippsat - Aug-21-2018

(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



RE: How can I make a python script create an EXE? - Axel_Erfurt - Aug-21-2018

But you have to keep in mind, if you use --onefile the app starts much slower than using directory.


RE: How can I make a python script create an EXE? - snippsat - Aug-21-2018

(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.


RE: How can I make a python script create an EXE? - ahmed_mokhles - Aug-21-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


RE: How can I make a python script create an EXE? - Axel_Erfurt - Aug-21-2018

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)

[Image: 44155887-bae0a224-a0ae-11e8-98e3-7bf613720032.png]