10

File associations on my machine (winxp home) are such that a python script is directly opened with the python interpreter. If I double click on a python script a console window runs and every thing is fine - as long as there is no syntax error in the script.

In that case the console window opens up for a moment but it is closed immediately. Too fast to read the error message.

Of course their would be the possibility to manually open a console window and to execute the script by typing python myscript.py but I am sure that there is a more convenient (i.e. "double click based") solution.

2
  • possible duplicate of Prevent command prompt from closing automatically (CS Script) Commented Jun 13, 2010 at 11:07
  • 5
    For future visitors, this is not a duplicate. Keeping the window open and keeping the window open after an error are two different things. When there's an error, your attempts to keep the window open (e.g. input()) will be ignored. Enter this question. Commented May 16, 2018 at 9:13

4 Answers 4

15

Make a batch file:

C:\Python26\python.exe %1 IF %ERRORLEVEL% NEQ 0 PAUSE 

Use that as your file association instead of python.exe directly. This will only cause the PAUSE statement to execute if python.exe returns an error

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

3 Comments

Works like I wanted it. Thanks
Is there a way to make this work with drag & drop? My python script process the folder that is dropped on it. When there's an error, I usually try to capture (screenshot) the command prompt and then examine the error. This doesn't always work. Window closes too quickly or I can't time the capture (in a long script). So, can I drop a folder on the bat file and make the bat file pass it (the folder path) to python script as an argument?
How do I associate a filetype with the batch script? Windows 10 only wants to allow registered apps to associate.
1

The canonical way to run a command in a command prompt window that doesn't close is

cmd /k "your command" 

Comments

1

The way that I do it is i right click the script that I saved from notepad to a .py file, then i click edit with IDLE, This is an editing thingy, but you can also run modules from it

Comments

0

I've left a comment at rossipedia's answer about a similar situation, but I doubt it's gonna get noticed in a 8 year old post. So, I've experimented myself.

I have a python script that processes a folder that's dropped on it. As I add new lines, sometimes I get syntax errors, and I can't view them because the command prompt window closes too quickly. In order to keep the window open after an error, I had to modify rossipedia's code. I needed a way to pass a path to the bat file (drop a folder on it) and make the bat pass that path to the python script. The following does that:

debug.bat:

@echo off REM debug.bat and myscript.py should be in the same dir, otherwise use absolute path for the python script C:\Users\%username%\AppData\Local\Programs\Python\Python36-32\python.exe "%~dp0\myscript.py" %1 IF %ERRORLEVEL% NEQ 0 PAUSE 

myscript.py:

import sys print("printing dropped file path:") print(sys.argv[1]) input() 

If I get an error, I just use the debug.bat to view the error, and it works the same (drop a folder on it).

Update:

In my actual python script, I have this line:

sys.path.append("modules") 

This doesn't work when I use debug.bat. "modules" is a folder that's in the same folder with the python script. When the script is called with debug.bat, the current working directory changes to the dropped file's directory. So, to get the path to "modules", I had to use absolute path:

sys.path.append(os.path.dirname(os.path.abspath(__file__)) + "\\modules") 

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.