2

I am using python 3 on Win10 and running my code by opening a Command Prompt window and typing the file location. However, the window closes as soon as the program terminates, and before I can read any errors. Edit: This happens whether or not the program has errors. Thank you.

3
  • 1
    Run it as python name_of_yourscript.py to execute in the same console context. Commented Jul 17, 2018 at 14:51
  • 2
    Curious. I'm using Windows 10 too, and my command prompt stays open, whether or not the program terminated due to an exception, and whether or not I ran it with python myscript.py or just myscript.py. Commented Jul 17, 2018 at 14:52
  • @zwer When I do that, I recieve the error 'python' is not recognized as an internal or external command, operable program or batch file. Commented Jul 17, 2018 at 15:00

2 Answers 2

3

Solution 1:

I just saw your comment:

When I do that, I recieve the error 'python' is not recognized as an internal or external command, operable program or batch file

It looks like you haven't specified the path to the python executable: you need to add the python executable path to your Window's PATH variable. You can see how to do that here: Add Python to the PATH Environmental Variable (‘python’ is not recognized as an internal or external command)

Solution 2:

You can use input("enter to exit") at the end of your python code to keep the program alive. It would exit once you press enter.

You could also surround your code in a try except statement and place thr input() in the except to prevent the program from exiting when there are errors, but like @Kevin mentioned in the comments, this would catch run time errors but not syntax errors.

Solution 3:

You can write errors or anything information you want to a file such as log.txt for example, and then read that log file once the code finishes running e.g. how to write to a file in Python

Sign up to request clarification or add additional context in comments.

3 Comments

input() won't execute if the script is terminating earlier than that due to an exception. Doesn't seem like an effective way to see errors.
I agree, it is a solution to keep the window open only if there are no errors. I guess he could surround his code with a try-except and place input() in the except
That ought to work on runtime errors, sure, but it won't do much good against SyntaxErrors, which get raised before any try block could be entered.
2

FWIW, I have several Python versions on my Windows system, so I don't want to add any Python directories to my path permanently.

Code for each version is in a separate folder (e.g. 'py37'), with a subfolder for each project e.g. 'myProject'. In py37, there's a batch file called pyEnv.bat with this content:

@echo off
path=%path%;C:\Python37\;C:\Python37\Scripts\
cd.
cmd

In Windows explorer, I head over to the project folder I want to work in, click in the address bar and type ..\pyEnv. That launches a DOS-box, in which I now can do python myproject.py. You can see print() output, errors, and so on.

You can up-arrow to try different modules, having typed them first.

Once you quit the DOS-box, your path is back to normal again.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.