In any python code I write that gets an error, it will show the error but the window will disappear right away and i can't see the error. It makes fixing codes really difficult. can anyone help me? (i have python 2.7 installed and with my programs i type them in notepad and save them as a .py file)
-
What IDE are you using? How are you running your program?Couchy– Couchy2014-04-29 02:40:18 +00:00Commented Apr 29, 2014 at 2:40
-
@yoyo311 Obviously he's simply double-clicking the .py file.ooga– ooga2014-04-29 02:40:44 +00:00Commented Apr 29, 2014 at 2:40
-
You could try using an IDE like pyscripter or spyder, which would also give you the benefit of syntax-highlighting etcBlorgbeard– Blorgbeard2014-04-29 02:46:55 +00:00Commented Apr 29, 2014 at 2:46
Add a comment
|
4 Answers
Open a command prompt and cd to the directory where your .py file is and type the name of the file there to run it.
6 Comments
Couchy
This only works if your computer recognizes .py as python file. If not you have to type
python file.py, and if this does not work you first have to set the python.exe location in PATH environment variable (assuming windows).ooga
@yoyo311 Of course. But I'm quite certain his computer recognizes .py files from the description of the problem.
mdlp0716
I can run the code fine, it just won't display any errors the code gets. it will show the error for a split second and the window will close.
Blorgbeard
@mdlp0716 this will fix that. Try it.
mdlp0716
@Blorgbeard how exactly do you do this? im not exactly sure what it means to cd to the directory where my .py files are
|
Open a command prompt, cd into the directory where the file is, and then do the following:
Redirect the standard error to a file as such:
python myscript.py > errorlog.txt 2>&1
Test:
myscript.py:
import time
time.sleep(2)
raise ValueError ('This should be redirected')
Running:
$ python myscript.py > errorlog.txt 2>&1
$
errorlog.txt after running:
Traceback (most recent call last):
File "myscript.py", line 5, in <module>
raise ValueError ('This should be redirected')
ValueError: This should be redirected
2 Comments
Blorgbeard
If OP was running his script from the command-line, he wouldn't have a problem anyway. He's double-clicking it in windows, so he can't redirect stderr
A.J. Uppal
Edited, to fit, added an information to the top like @ooga
I highly recommend using an IDE instead of notepad. It will make writing and debugging code much easier for you.
Here's a screenshot of pyscripter showing me exactly which line caused my error.
You can see how much easier it is to see what's going on.
