Python Forum
Python 3.6 to executable
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python 3.6 to executable
#1
Hi everyone

I've been coding a program for a friend that I would like that friend to be able to use smoothly. I thus should be able to convert it to an executable. This, I am able to do using pyinstaller (which supports Python 3.6). My program generally goes as follows: I use a while loop to be able to repeat the program as much as the user likes to, and then I ask some simple input from the user, after which the big part of the program runs. It all works smoothly in my IDLE (Thonny), and it does also when running the .py file through cmd. But when I use pyinstaller to create an executable and then run the executable, I get the first part of the program working, but as soon as I enter the answer to the last input question, the cmd window flashes some chunk of text en closes immediately. I figured this is because of the modules I use in the program (for example random and time and readlines and stuff), and this idea was confirmed by looking at the warn file that pyinstaller (I guess) had installed. It reported of numerous modules that were unfound running the program. This is all actually just background information and may be irrelevant to my question, which goes as follows: What is the best way to send a friend a program I coded? By 'best' I mean it being as easy as possible for a friend to just simply run the program.

Thanks in advance!
Reply
#2
(Jan-09-2018, 10:05 PM)DeGerlash Wrote: the cmd window flashes some chunk of text en closes immediately. I figured this is because of the modules I use in the program (for example random and time and readlines and stuff)
No it will always close cmd window after program is finish,if start bye double-clicking.
Start cmd first then start .exe window will stay.
There are some solution the simplest is adding this line last.
input('--- Press <Enter> to exit ---')
(Jan-09-2018, 10:05 PM)DeGerlash Wrote: What is the best way to send a friend a program I coded? By 'best' I mean it being as easy as possible for a friend to just simply run the program.
If he/she is a Windows user and don't have Python installed and maybe not so technically minded,
then .exe is the simplest.
If have Python installed then you could make wheel.
Then all is packed in that wheel file and install is pip install wheel_name.whl.
Reply
#3
Yes, I figured that solution and put it in my code, the program should always wait for the user to close it, that's how I coded it.

And yes, I do understand that a .exe file would be the easiest way to transfer it to a friend, but I'm looking for a solution to incoorporate the imported modules within the .exe file. Does anyone have experience with that?

This is the 'warn'-file I get in the 'build'-map.

Quote:missing module named resource - imported by posix, C:\Users\Adriaan\Bureaublad\EnglishWordGenerator2.py
missing module named posix - imported by os, C:\Users\Adriaan\Bureaublad\EnglishWordGenerator2.py
missing module named _posixsubprocess - imported by subprocess, C:\Users\Adriaan\Bureaublad\EnglishWordGenerator2.py
missing module named org - imported by pickle, C:\Users\Adriaan\Bureaublad\EnglishWordGenerator2.py
missing module named readline - imported by cmd, code, pdb, C:\Users\Adriaan\Bureaublad\EnglishWordGenerator2.py
excluded module named _frozen_importlib - imported by importlib, importlib.abc, C:\Users\Adriaan\Bureaublad\EnglishWordGenerator2.py
missing module named _frozen_importlib_external - imported by importlib._bootstrap, importlib, importlib.abc, C:\Users\Adriaan\Bureaublad\EnglishWordGenerator2.py
missing module named _winreg - imported by platform, C:\Users\Adriaan\Bureaublad\EnglishWordGenerator2.py
missing module named _scproxy - imported by urllib.request
missing module named java - imported by platform, C:\Users\Adriaan\Bureaublad\EnglishWordGenerator2.py
missing module named 'java.lang' - imported by platform, C:\Users\Adriaan\Bureaublad\EnglishWordGenerator2.py, xml.sax._exceptions
missing module named vms_lib - imported by platform, C:\Users\Adriaan\Bureaublad\EnglishWordGenerator2.py
missing module named termios - imported by tty, C:\Users\Adriaan\Bureaublad\EnglishWordGenerator2.py, getpass
missing module named grp - imported by shutil, tarfile, C:\Users\Adriaan\Bureaublad\EnglishWordGenerator2.py
missing module named pwd - imported by posixpath, shutil, tarfile, http.server, webbrowser, C:\Users\Adriaan\Bureaublad\EnglishWordGenerator2.py, netrc, getpass
missing module named _dummy_threading - imported by dummy_threading, C:\Users\Adriaan\Bureaublad\EnglishWordGenerator2.py
missing module named 'org.python' - imported by copy, C:\Users\Adriaan\Bureaublad\EnglishWordGenerator2.py, xml.sax
Reply
#4
Quote:This is the 'warn'-file I get in the 'build'-map.
Just ignore the warnings you get in middle unfortunately it's normal with pyinstaller,the message you last get last is most important.
Output:
48961 INFO: checking EXE 48961 INFO: Building EXE because out00-EXE.toc is non existent 48962 INFO: Building EXE from out00-EXE.toc 48963 INFO: Appending archive to EXE C:\1\shu_env\dist\sh.exe 48978 INFO: Building EXE from out00-EXE.toc completed successfully.
If get error here like don't find module,then most fix it.

Exmaple in this post build a .exe with Pandas(has a lot of dependencies).
There was a error last that has to fixed,added a module with hiddenimport.
Also in that post use virtual environment to have better control of dependencies.
Reply
#5
Could you explain more in detail how I should do that then?
Here's the python code:
from time import time
import random
key=input("Press any key and then enter to start ... ")
while key!="":
    alphabet=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
    distribution=[0,0,1,6,26,52,85,122,140,140,126,101,75,52,32,20,10,6,3,2,1,1,0,0,0,0]
    word=""
    counter=0
    wordtype=""
    chance=0
    timeonbatteryseconds=0
    timeonACseconds=0
    runlengthcalculator=0
    x=0
    y=0
    timescounter=0
    howmanywords=0

    def howmany(wordlength):
        howmanywords=0
        if language=="dutch" or language=="Dutch" or language=="nl" or language=="NL" or language=="nederlands" or language=="Nederlands" or language=="Nl":
            file = open("DutchWords.txt", "r")
            for element in file.readlines():
                if len(str(element))==int(wordlength)+1:
                    howmanywords+=1
            return howmanywords
        if wordtype=="common":
            file = open("CommonEnglishWords.txt", "r")
            for element in file.readlines():
                if len(str(element))==int(wordlength)+1:
                    howmanywords+=1
            return howmanywords
        if wordtype=="all":
            file = open("AllEnglishWords.txt", "r")
            for element in file.readlines():
                if len(str(element))==int(wordlength)+1:
                    howmanywords+=1
            return howmanywords

    def isValid(word):
        if language=="dutch" or language=="Dutch" or language=="nl" or language=="NL" or language=="nederlands" or language=="Nederlands" or language=="Nl":
            file = open("DutchWords.txt", "r")
            if word in file.read():
                return True
            else:
                return False
        if wordtype=="common":
            file = open("CommonEnglishWords.txt", "r")
            if word in file.read():
                return True
            else:
                return False
        if wordtype=="all":
            file = open("AllEnglishWords.txt", "r")
            if word in file.read():
                return True
            else:
                return False


    language=input("What language do you wish to proceed in? (dutch/english) ")

    if language=="" or language=="choose" and language!="dutch" and language!="Dutch" and language!="nl" and language!="NL" and language!="nederlands" and language!="Nederlands" and language!="Nl" and language!="english" and language!="English" and language!="Eng" and language!="eng":
        decider=random.randint(0,1)
        if decider==0:
            language="English"
            print("I'm gonna go with English.")
        if decider==1:
            language="Dutch"
            print("Laten we nederlands nemen.")
        
    if language=="english" or language=="English" or language=="Eng" or language=="eng":
        wordtype=input("What words do you want to generate? (common/all) ")
        if wordtype=="choose" or wordtype=="":
            decider=random.randint(0,1)
            if decider==0:
                wordtype="common"
                print("Let's only generate common words.")
            if decider==1:
                wordtype="all"
                print("Let's generate all words!")
        wordlength=input("How many characters do you want the word to have? ")
    if language=="dutch" or language=="Dutch" or language=="nl" or language=="NL" or language=="nederlands" or language=="Nederlands" or language=="Nl":
        wordlength=input("Hoelang moet het woord zijn? ")
                
    if wordlength=="choose" or wordlength=="kies" or wordlength=="kies maar" or wordlength=="kies zelf" or wordlength=="goh kies maar zelf" or wordlength=="goh kies maar" or wordlength=="aan u de keuze" or wordlength=="":
        x=random.randint(0,999)
        wordlength=7
        if x==0:
            wordlength=1
        for element in distribution:
            if x in range(y+1,y+element):
                wordlength=timescounter
                break
            timescounter+=1
            y+=element
            
        chance=float(((howmany(wordlength))/(26**wordlength))*100)
        timeonbatteryseconds=float(((26**int(wordlength))*1.5)*(10**(-9)))
        timeonACseconds=float(((26**int(wordlength))*3.6)*(10**(-9)))
            
        if language=="dutch" or language=="Dutch" or language=="nl" or language=="NL" or language=="nederlands" or language=="Nederlands" or language=="Nl":
            print("Laten we "+str(wordlength)+" letters nemen.")
            print("De kans dat dit me lukt is "+str(chance)+"%, want er zijn "+str(howmany(wordlength))+" nederlandse woorden met "+str(wordlength)+" letters.")
            #print("Aan netstroom zou dit ongeveer "+str(timeonACseconds)+" seconden duren.")
            #print("Op batterijstroom duurt dit waarschijnlijk echter "+str(timeonbatteryseconds)+" seconden. Maar ondertussen al weer minder natuurlijk!")
            print("Aan het genereren...")
        if language=="english" or language=="English" or language=="Eng" or language=="eng":
            if wordtype=="common":
                print("The chance of me pulling this off is "+str(chance)+"%, as there are "+str(howmany(wordlength))+" common English words with "+str(wordlength)+" characters.")
            if wordtype=="all":
                print("The chance of me pulling this off is "+str(chance)+"%, as there are "+str(howmany(wordlength))+" English words with "+str(wordlength)+" characters.")
            print("I'm gonna go for "+str(wordlength)+" characters.")
            #print("On AC power, this should take around "+str(timeonACseconds)+" seconds.")
            #print("On battery power, this'll probably take "+str(timeonbatteryseconds)+" seconds. But now already a lot less of course!")
            print("Generating...")
    else:
        wordlength=int(wordlength)
        
        chance=float(((howmany(wordlength))/(26**wordlength))*100)
        timeonbatteryseconds=float((26**int(wordlength))/(1.5*(10**9)))
        timeonACseconds=float((26**int(wordlength))/(3.6*(10**9)))
        
        if language=="dutch" or language=="Dutch" or language=="nl" or language=="NL" or language=="nederlands" or language=="Nederlands" or language=="Nl":
            print("De kans dat dit me lukt is "+str(chance)+"%, want er zijn "+str(howmany(wordlength))+" nederlandse woorden met "+str(wordlength)+" letters.")
            #print("Aan netstroom zou dit ongeveer "+str(timeonACseconds)+" seconden duren.")
            #print("Op batterijstroom duurt dit waarschijnlijk echter "+str(timeonbatteryseconds)+" seconden. Maar ondertussen al weer minder natuurlijk!")
            print("Aan het genereren...")
        if language=="english" or language=="English" or language=="Eng" or language=="eng":
            if wordtype=="common":
                print("The chance of me pulling this off is "+str(chance)+"%, as there are "+str(howmany(wordlength))+" common English words with "+str(wordlength)+" characters.")
            if wordtype=="all":
                print("The chance of me pulling this off is "+str(chance)+"%, as there are "+str(howmany(wordlength))+" English words with "+str(wordlength)+" characters.")
            #print("On AC power, this should take around "+str(timeonACseconds)+" seconds.")
            #print("On battery power, this'll probably take "+str(timeonbatteryseconds)+" seconds. But now already a lot less of course!")
            print("Generating...")
            
    starttime=time()

    while counter<=wordlength:
        word=word+alphabet[random.randint(0,25)]
        
        if len(word)==wordlength:
            if isValid("\n"+word+"\n")==True:
                if language=="english" or language=="English" or language=="Eng" or language=="eng":
                    print("The generated word is: "+word)
                if language=="dutch" or language=="Dutch" or language=="nl" or language=="NL" or language=="nederlands" or language=="Nederlands" or language=="Nl":
                    print("Het gecreëerde woord is: "+word)
            else:
                word=""
                counter=0
        counter=counter+1
        runlengthcalculator=runlengthcalculator+1
        
    endtime=time()
    timetaken=endtime-starttime
    if language=="dutch" or language=="Dutch" or language=="nl" or language=="NL" or language=="nederlands" or language=="Nederlands" or language=="Nl":
        print("Dit heeft ongeveer "+str(round(timetaken,2))+" seconden geduurd.")
        key=input("Druk op eender welke toets en dan enter om een ander woord te creëren, druk enter om af te sluiten ... ")
    if language=="english" or language=="English" or language=="Eng" or language=="eng":
        print("This took around "+str(round(timetaken,2))+" seconds.")
        key=input("Press any key and then enter to generate another word, press enter to close ... ")
    counter=0
Reply
#6
Here a run,i must use virtual environment because pyinstaller give error on my messy OS python.
I use cmder,but command is the same in cmd except(ls which is dir(cmd)).
# Make enviroment
C:\1
λ python -m venv qustion_env

# cd in
C:\1
λ cd qustion_env

# Activate enviroment,will see (qustion_env)
C:\1\qustion_env
λ C:\1\qustion_env\Scripts\Activate

# Install pyinstaller
(qustion_env) C:\1\qustion_env
λ pip install pyinstaller
Collecting pyinstaller
Collecting future (from pyinstaller)
Collecting pefile>=2017.8.1 (from pyinstaller)
Collecting macholib>=1.8 (from pyinstaller)
  Using cached macholib-1.9-py2.py3-none-any.whl
Requirement already satisfied: setuptools in c:\1\qustion_env\lib\site-packages (from pyinstaller)
Collecting altgraph>=0.15 (from macholib>=1.8->pyinstaller)
  Using cached altgraph-0.15-py2.py3-none-any.whl
Installing collected packages: future, pefile, altgraph, macholib, pyinstaller
Successfully installed altgraph-0.15 future-0.16.0 macholib-1.9 pefile-2017.11.5 pyinstaller-3.3.1

# Install pypiwin32 needed bye pyinstaller
(qustion_env) C:\1\qustion_env
λ pip install pypiwin32
Collecting pypiwin32
  Using cached pypiwin32-220-cp36-none-win32.whl
Installing collected packages: pypiwin32
Successfully installed pypiwin32-220

# Your file is game.py
(qustion_env) C:\1\qustion_env
λ ls
Include/  Lib/  Scripts/  game.py  pip-selfcheck.json  pyvenv.cfg

# Run pyinstaller
(qustion_env) C:\1\qustion_env
λ pyinstaller --onefile game.py
.... a lot installing .....

# To the game game.exe and test it
(qustion_env) C:\1\qustion_env
λ cd dist
(qustion_env) C:\1\qustion_env\dist
λ ls
game.exe*

(qustion_env) C:\1\qustion_env\dist
λ game.exe
Press any key and then enter to start ... j
What language do you wish to proceed in? (dutch/english) english
What words do you want to generate? (common/all) common
How many characters do you want the word to have? 4
Traceback (most recent call last):
  File "game.py", line 120, in <module>
  File "game.py", line 28, in howmany
FileNotFoundError: [Errno 2] No such file or directory: 'CommonEnglishWords.txt'
So .exe work as it should,it stop because i don't have CommonEnglishWords.txt,
this is a file you have local on disk.
Reply
#7
So I need to run all that in cmd? I'm sorry I don't really understand, my English is not so good...
Reply
#8
You can try.
pyinstaller --onefile game.py
This is done from cmd in folder of where you have your file.

You can look at Python 3.6 and pip installation under Windows
This to make sure that python and pip command work from anywhere in cmd.
Example to make sure you have newest pyinstaller.
pip install -U pyinstaller
pip install -U pypiwin32
It don't work to make .exe then you follow my way over,
which is good exercise in command line stuff which is important in programming.
Reply
#9
It still doesn't work, just closes like before. Your way over doesn't seem to work as well, but I'm sure I'm doing something wrong. It says I have perfect pyinstaller and pypiwin32 on my pc!
Reply
#10
(Jan-12-2018, 01:41 PM)DeGerlash Wrote: It still doesn't work, just closes like before.
What do you mean do the .exe start and it close after finish?
If that's the case as mention before in last line.
input('--- Press <Enter> to exit ---')
Star cmd first(not double_click on .exe)then navigate to .exe and start it.
Doing it like this then cmd window stay open and you can see if there is a error message.
(Jan-12-2018, 01:41 PM)DeGerlash Wrote: Your way over doesn't seem to work as well, but I'm sure I'm doing something wrong.
I think you most do something that i don't do,every step i do is in my post and game.py is just copy and paste of your code.
It generate for me a working game.exe.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create Python "Executable" from PyCharm? Oliver 8 39,203 Jan-08-2018, 10:52 AM
Last Post: Oliver

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020