Python script to executable with cx_Freeze, exe does nothing

时光毁灭记忆、已成空白 提交于 2021-01-04 04:13:32

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/52861349/python-script-to-executable-with-cx-freeze-exe-does-nothing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!