4

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.

jpeg
  • 2,372
  • 4
  • 18
  • 31
Noah
  • 43
  • 5
  • A tip for running an executable that seems to do nothing: Open the command line, cd to the directory containing the application (good practice), and run the executable there. Do you see any output from the program or does it immediately go to a new line? – Timothy Jannace Oct 17 '18 at 18:27
  • @Timothy Jannace I forgot to add it in the post, i tried it and I get immediately s new line, I will add it above – Noah Oct 18 '18 at 04:03
  • May this link can help you : https://stackoverflow.com/questions/44491802/unable-to-run-python-programs-from-windows-10-cmd – Mohamad TAGHLOBI Oct 18 '18 at 04:55
  • @Mohamad the script itself is working as I stated above, my problem is the executable – Noah Oct 18 '18 at 08:54
  • The setup.py script you've posted above looks correct accordign to their tutorial. What is the exact command you are using to build the executable? – Timothy Jannace Oct 18 '18 at 17:54
  • I am using the "build" command – Noah Oct 19 '18 at 13:52
  • Try compiling with `base = "Console"`. – Nouman Oct 19 '18 at 13:59
  • tried it but it is still the same – Noah Oct 19 '18 at 14:10
  • @Noah I think you should rollback the edits you made to your code: you should leave the code as it was when you asked the question in order that future readers can retrace what the problems were. – jpeg Oct 19 '18 at 19:04

2 Answers2

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.

jpeg
  • 2,372
  • 4
  • 18
  • 31
  • Thanks for the advice, it now works with the cmd, is there a way to execute it with out using the cmd? – Noah Oct 19 '18 at 13:51
  • @Noah If I understand correctly your comment, when you run your executable without using the cmd (i.e. 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. What you then need is something to tell your script to wait until you press a key before finishing. You can add `input("Press Enter to continue...")` at the end of your script for this purpose, see [this question](https://stackoverflow.com/q/983354/8516269). – jpeg Oct 19 '18 at 14:11
  • Thank you, that works as intended, thank you very much Can you add it to your answer so I can oficially accept it? – Noah Oct 19 '18 at 16:32
  • @Noah great that it works. I have edited my answer accordingly. – jpeg Oct 19 '18 at 18:02
0

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.

Nouman
  • 6,947
  • 7
  • 32
  • 60