Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to see Python executable output in a cmd window?

I have written a script in python 3.5. I have made an .exe file of it using pyinstaller. The script has some print statements which are displayed in the console during execution. When i ran my script from command prompt i can see all the messages in my cmd but when i run .exe file from cmd than i do not see any msgs in command prompt. Is there any way to see msgs in command prompt from .exe file.

For example lets suppose below is my script named abc.py:

def sum():
   first = 5
   print('First number is {}'.format(first))
   second = 10
   result = first + second
   print('second number is {}'.format(second))
   print('Sum is = {}'.format(result))
 sum()

Now when i run this file from cmd like: python \pathto\abc.py, i can see all the messages in cmd. Now when i make .exe of this and now do something like \pathto\abc.exe from cmd then i do not see anything. Is there anyway to see ,msgs in a cmd from .exe file?

like image 967
umair butt Avatar asked Jan 22 '18 17:01

umair butt


2 Answers

First of all thanks for your support. I have found the solution.I was using: pyinstaller.exe --onefile --windowed myapp.py to generate my .exe file. From documentation i found out that --windowed prevents a console window from being displayed when the application is run. If you're releasing a non-graphical application (i.e. a console application), you do not need to use this option. So if you generate your exe with:

  pyinstaller.exe --onefile myapp.py 

and then run it via cmd, all the msgs will display in your cmd. Note: In this way you can also run your exe from cmd with command line arguments as well. For example suppose if in the above code variable:

first = sys.argv[1]

then running exe file from cmd like:

/exe/path 20

will also work. just keep in mind that sys.argv generates a string so in this case you need to convert 20 into integer.

like image 55
umair butt Avatar answered Sep 18 '22 05:09

umair butt


Your problem is that you are using --windowed. Use --console instead, and it should print like normal!

like image 24
Qwerty Avatar answered Sep 18 '22 05:09

Qwerty