0

enter image description hereI'm working with Python 3.4.4 in Windows XP. When I run the .py file using double click on it, it only asks for user name & then terminates the command prompt suddenly. So I can't see final result. Here's my code:

from datetime import datetime
import time
from os import getcwd
import sys
name = input("Your name please: ")
print("Hello",name,",your system is:",sys.platform)
print("Your current work directory is:",getcwd())
print("Currenttime:",time.strftime("%a"),datetime.today(),time.strftime("%p"))
print("Thanks for trying. \u00a9 Gaz does Python")
input()

Can anyone help me, please? I've tried time.sleep() but that doesn't work too.

  • It's probably dying on an encoding error when printing U+00A9 using the default OEM codepage. – Eryk Sun May 12 '17 at 15:48
  • Possible duplicate of [How to keep a Python script output window open?](http://stackoverflow.com/questions/1000900/how-to-keep-a-python-script-output-window-open) – Josh Lee May 12 '17 at 15:54
  • Does it fail with an error when you run the script from the cmd shell? – Eryk Sun May 12 '17 at 17:23
  • no error. it just prompts for name, then flashes out like a blink. If I look closely, i can see the output while flashing out. – Md. Fakruddin Gazzali Fahim May 13 '17 at 02:42
  • I mean if you run it as `"python script.py"` after changing to the directory (`cd path\to\script`) in the cmd shell (i.e. command prompt). – Eryk Sun May 13 '17 at 11:26
  • tried, no improvement, maybe it's my OS problem or Python problem. I tried cx_freeze, py2exe, all fails. I thought python 3.x will be great, but python 2.7 can handle this. :( & thanks a lot @eryksun for your sincere help. :D – Md. Fakruddin Gazzali Fahim May 13 '17 at 15:13
  • You mentioned nothing about freezing the script to get an executable, just that you're double-clicking on a .py script, so let's not bring cx_freeze or py2exe into the problem. Please edit your question with the exact output from the command prompt (copy and paste) when you run `python script.py` for whatever the real name of "script.py" is. – Eryk Sun May 13 '17 at 15:22
  • @eryksun Thanks a lot. No error occurs if remove \u00a9 .... it runs smoothly... But i wanna add that copyright sign .... ;) I also wanna make that .py into an .exe with python 3.4.3 so that i can share my compiled program to people who don't have python installed ! – Md. Fakruddin Gazzali Fahim May 14 '17 at 05:53

1 Answers1

0

** EDIT **

Did not see the last input. What I said is invalid. The prompt is probably dying like @eryksun said.

** Original answer **

The simplest answer is to open cmd.exe manually, navigate to your file and then execute your script with "python yourScript.py"

A Windows prompt exits when no processes are left running. When you double click on your file, the CMD is"associated" with the execution and exits upon it's completion. By running it beforehand manually "without purpose", there is no need for the prompt to exit as "he have another purpose".

You could also hack your way out by using one last input at the end of the program. That will force the prompt to wait for this input. (The infamous "PRESS ENTER TO CONTINUE" for example )

Kuu Aku
  • 320
  • 2
  • 6
  • When you double click a Python file it should run python.exe, not cmd.exe. The console is conhost.exe, not cmd.exe (just like an X term isn't bash in Linux). python.exe (or py.exe) starts conhost.exe as a child process if it's run from Explorer. If it's run from another process that's already attached to a console (such as cmd.exe), then it usually inherits the parent's console, unless instructed to create a new console. – Eryk Sun May 12 '17 at 15:55
  • @kuu Aku @eryksun , i tried removing `\u00a9` & added `input( "PRESS ENTER TO CONTINUE")` .... but nothing happens :( – Md. Fakruddin Gazzali Fahim May 12 '17 at 16:44
  • Well. I tried it on without \u00a9 and with the final input, it works as expected. (I chose "meh" as name). Maybe try deleting lines (not the final input) until it works to see what's going on. You could also try/catch the imports and the inputs. – Kuu Aku May 13 '17 at 07:40
  • Thanks for your explanation on processes by the way @eryksun. – Kuu Aku May 13 '17 at 07:41
  • @kuu I also tried by deleting line 5,then it freezes. but I need the name, maybe in later cases! – Md. Fakruddin Gazzali Fahim May 13 '17 at 15:06
  • It freezes ? It stops at the last input or freeze in the middle on the program ? – Kuu Aku May 13 '17 at 15:53
  • Viewing your image, I think I've understood what's going on. The "f" is not defined is only happening in the windows prompt and not in Python shell. I think that's because the windows prompt is using Python 2, where the input gets evaluated. Try to print (sys.version_info) at the beginning. I'm sure there will be a difference between Python shell and Windows prompt. – Kuu Aku May 14 '17 at 08:14
  • 1
    In that case, repair the Python 3.4 installation to associate .py files with it. Hopefully this changes the `Python.File` file type that you're using (it might not if 2.7 was per user and 3.4 is per machine). If it's still the wrong association, try using the control panel's default programs app to change the .py association to "Python" (not "python.exe" and do not browse to manually associate python.exe). It should have the Python logo, and when chosen the info in the default programs list when .py is selected should say it's from the Python Software Foundation. – Eryk Sun May 14 '17 at 11:27
  • will try that soon :-) – Md. Fakruddin Gazzali Fahim May 18 '17 at 00:55
  • Thanks @kuuaku, your theory worked, now I'm able to run & freeze my python programs in 3.4.3. :-) – Md. Fakruddin Gazzali Fahim May 19 '17 at 12:26
  • Also thanks to @erysin for great explanation, found them so helpful! – Md. Fakruddin Gazzali Fahim May 19 '17 at 12:27