have ipython open editor at an error in a script - python

When running a script in Ipython (with %run) and there is an error in the script, is it possible to open an editor at the line of the error or have an open editor jump to that line?

Automatically this is not happening, although one could implement something like that. The question is, what would be the correct file to open? The backtrace will reveal all possible files in which the functions raised the error. In a large project, it would not be clear in which file the error is, is it with the caller or callee who made the error?
Most advanced IDEs e.g. eclipse with pydev allow direct jumps to the lines and function calls, so it is covered there.
on an ipython level you could do
%edit file_named_in_error.py

Related

How can I debug external python package in PyCharm

I have downloaded python package from the web (e.g., github).
The usage is via CLI, for example:
python functionality.py opt1 opt2
Since I want to understand what is going on under the hood in functionality.py
I want to be able to debug it line by line.
How can I achieve that using PyCharm?
Sorry for not writing a comment, but I don't have the right.
You could use the package in a simple script (import it). Afterwards, press CTRL+LEFT CLICK on the function/class that you have used in order to see the source code. Now you can put breakpoints my clicking on the space near the line number. The red point indicates that a breakpoint is put.
You can run your script in debug mode using the debugger from the menu up right (second icon) and see what's happening.

How to save the output of an IPython console to a file in Spyder?

I have a bug in my program :(
The problem is that:
My .py code is long, and takes ages to run
I don't know where the bug is
The good news is that I have a lot of print() in my py file, so I can potentially know where the bug lives.
The bad news is that my bug makes my computer crash, so there is no way for me to look at the output of the ipython console and see what went wrong.
How can I have the output be written to disk while the program runs? So that I can still open the file after reboot to understand what happened before the crash?
This question is different from Redirect stdout to a file in Python?, because I need
continuous writing to file
something to use from within Spyder
Many thanks!
Very interesting question! Fortunately IPython has the right magic for you. It's called %logstart (please follow the link for the full documentation).
To start using it and save the input and output of all your commands, just type in an IPython console
In[1]: %logstart -o
and that will record your session from that moment on into a file called ipython_log.py placed in your current directory.
%logstart is very flexible, so you can select a different file to save to, and also how you save your session (either pure Python or as IPython commands).

How do I handle errors in Ren'py

I'm making a game using Ren'py (based on python) and most errors aren't shown, especially the errors in python code. Is there a possibility to check for possible errors at compile time and how do I get where some errors occur? If there are errors the game normally doesn't run or breaks at the a errors appearance without a message.Is there maybe a file, where they are written in or something like that? Or do I have to debug using logs everywhere?
Compile errors will be shown when you first try to compile the RPY files. They will be put in "errors.txt" in your project directory. Most errors are not found at compile-time, however, and will only show up once you encounter them at run-time.
You can use Lint to check for some common errors (It's called "Check Script (Lint)"), but mostly you'll have to playtest to ensure there are no errors. Errors during playback should pop up a gray screen showing the error and traceback with the option to Ignore, Rollback, or Quit. Is this screen not showing up for you?
If you're looking for some kind of intellisense like you have for some languages, where as you write the code the IDE shows errors, then it doesn't exist. You have to launch the game so that the code is compiled, just then Ren'py will show you errors. You can see them in the editor or in the errors.txt that Ren'Py creates. To test you python code you can launch the game and type Shift + O to open the console.

PyCharm debug jump to

http://i.stack.imgur.com/8LzEj.png
Hi,
I am using PyCharm 2.7.3, the newest version to this date
I am writing a simple script called hour4.py
I want to debug that script. So I hit Alt+Shift+F9 to open the debug script dialoague and then I choose from the list hour4.py. This is where it all goes wrong.
Instead of PyCharm debuggin my script hour4.py it jumps over to pydevd.py and starts debugging that one instead. In the bottom of my screen it shows its debugging hour4.py but its clearly not.
Thanks for anything that helps
Open Run | View Breakpoints and disable the Python Exception Breakpoints:
You also need to ensure that your script has no syntax errors, otherwise it will fail to load and you will step into pydevd.py instead.

I'm new to Python and managed to crash IDLE after adding a line of code to my program

I'm working on a small game and, in the interest of full disclosure, I've learned some other languages before but this is only my second day learning Python.
What I was trying to do was simple enough: I was trying to generate a random integer between two integer values (e.g. random(a,b)). I looked around to see if there was an existing function that I could use, and I found information about a function called "randint". So I added a line of code to my program that looked something like:
value = randint(1,15)
I received an error that randint was undefined. So then I looked further and saw someone state that I needed to import the random library using the following line of code:
import random
The moment I refreshed (F5) IDLE crashed. So then I figured "welp, that was wrong" and I went to open IDLE and make the change. I was able to open IDLE, but I can't open the file. In fact, I can't open any of the (three) files that I've made. I opened the file in Notepad++ and removed the offending lines (both randint and import random), but IDLE still crashes whenever I try to open a file. I rebooted my laptop for lack of a better idea, but there was no change in behavior.
Details: Windows 7 x64 w/ all updates, Python 3.2.3.
...help? Also, what did I do? :-/
Received a solution from another forum:
"If you want to try diagnosing the problem, run the Python command line and then enter the line:
from idlelib import idle
That will launch idle, and you should get a traceback in the command prompt if something goes wrong."
When I did this it opened IDLE and produced an error (displayed in terminal) when I tried to open the file. Rather than crashing, I received a prompt (pop-up, not in terminal) to save the file. I found this odd because I had just opened it and hadn't made any changes. I let it save the file and then success! Now I can use IDLE to open any of my files again. Hope this helps someone else who encounters this issue :)
I'm not sure if this is helpful, but based on my experience, IDLE was never reliable enough for me. Practice using the command-line Python instead.
Go to the command prompt by running cmd
cd into your file's directory
type python yourfilename.py

Categories