简体   繁体   中英

Python script to executable with cx_Freeze, exe does nothing

I have decided for practice purposes, I'd write a Passwordgenerator and make it an executable.
My script is running as it is intended, and the compiling works as well, but when I run the exe file, nothing happens. I run a Windows 10 system and use Python 3.6.x and I am not a beginner of python itself.

I looked up various pages on the internet, but I found nothing that helped me on that problem, my first problem was that the compiling didn't work but I already found that solution.

Edit: I tried to run the exe with the cmd and I get no output, instead I get a new line.

This is the setup code:

import sys
from cx_Freeze import setup, Executable

build_exe_options = {"excludes": ["tkinter"]}
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(name="Password",
      version="1.0",
      description="Generates a password made of 20 characters",
      options={"build_exe": build_exe_options},
      executables=[Executable("pass.py", base=base)])

And this is my program:

import random
import string

for i in range(20):
   k = random.choice(string.ascii_letters)
   j = random.randint(0, 9)
   z = random.randint(1, 2)
   if z == 1:
      x = k
   if z == 2:
      x = j
   print(x, end=" ")

I am grateful for any kind of insight.

Remove the two lines

if sys.platform == "win32":
   base = "Win32GUI"

from your setup script and it should work.

base = "Win32GUI" tells cx_Freeze not to start a console window and should be used only if the main application starts a GUI (eg with PySide, PyQt, Tk, ...). It presumably also redirects the standard output away from the console if you run the executable from an already started console. In your case you have a console-based application and you thus want a console to be started and to receive the standard output. This behavior is partially explained in the cx_Freeze documentation .

Now if you run your executable without using the cmd (eg by double-clicking it in Windows-Explorer), it starts a console window, prints the output there, and closes the console immediately when the execution is finished. In your example script, you would like to have the time to read the output before the console closes, so what you need then is something to tell your script to wait before finishing, for example until you press a key. You can add

input("Press Enter to continue...")

at the end of your script for this purpose, see How do I make python to wait for a pressed key .

Add wait after the code so it doesn't finish immediately.

import random
import string

for i in range(20):
   k = random.choice(string.ascii_letters)
   j = random.randint(0, 9)
   z = random.randint(1, 2)
   if z == 1:
      x = k
   if z == 2:
      x = j
   print(x, end=" ")

import time
time.sleep(5)   #<-- Sleep for 5 seconds

You can also use my Python Executable maker .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM