I'm trying hard to like IPython Notebook, but maybe because I'm so used to writing code in vi and executing it at the command line I'm finding some of its defaults challenging. Can anything be done (perhaps in a configuration file somewhere) about the following?
I'd like %hist to output line numbers by default without having to remember the -n and without having to set up an alias every time.
How do I set %automagic to "off" by default to stop IPython polluting my namespace with its un-percented magics and shell commands? I know I can use the --no-import-all option with --pylab option: is there an equivalent --no-automagic option?
It drives me mad that I'm never quite sure what is the status of the objects bound to my variable names: changing and running a cell beneath the one I'm using can alter an object I'm referring to in the current cell. To avoid this, I've got into the habit of using Run All or Run All Above, but that sometimes repeats lengthy calculations and reimports stuff I'd rather not bother with: can I flag some cells to be not-rerun by Run All?
Can I get vi-style key-bindings for editing cells?
Why does IPython notebook hang my browser if the kernel is using lots of memory? I thought they were separate processes with the kernel just reporting back its results.
(Please try to ask one question per question - SO is designed to work that way. However, I'm not feeling so mean that I'd refuse to answer)
I don't think the behaviour of %hist is configurable, sorry.
Set c.InteractiveShell.automagic = False in your config files.
There has been some discussion of a %%cache cell magic that could avoid re-running long running cells by storing values computed in that cell. It has not been implemented yet, though.
Yes: https://github.com/ivanov/ipython-vimception
It shouldn't hang just because of kernel memory use - if your code is producing a lot of output, though, that can hang the browser because adding lots of things to the DOM gums it up.
Related
Being in the process of switching from Matlab to Python/IPython/Spyder I ran into the following issue.
In a Matlab script (and I actually did not notice this until I switched to Python) there is no warning if you are using a variable which you did not define in that script.
What I kept doing all the times in Matlab was to have, say, 2 (or more) scripts which I would then execute one after the other. The second script would typically use variables defined in the first, but would not give me any warning.
In Spyder I noticed that the situation is different. In my hypothetical second script, all the variables which are not defined in the second script itself give me a warning (undefined name '...'), which is not nice too see...
Another typical example, would be the following: I have a main script but there is something in this script I wanna look at better. So, without touching the script, I would create another file where I would copy paste a few lines from the script to play a little with them.
In this new file I would be using variables which are in the console already but that are techically unknown to the file itself, so would give me a warning.
So I guess I am simply asking if there a way to suppress this kind of warnings.
Or there might be a deeper question of whether there s a more pythonic way of working, but there I am really not sure about...
I'm doing experience with my first small applications, essentially data viewers based on Pandas and matplotlib, using PyQt for the GUI part.
What I find now difficult is to understand what goes wrong in my code, because the error does not get propagated to the iPython console I launch my script from.
It simply won't do what's expected, but there is no information as to 'why'.
To fix ideas, let's say I have a button that should plot a certain curve to the canvas. If there's a fail in the indexing operation of the data, therefore nothing can be plotted, then nothing will appear on the canvas, but I'll get no traceback that actually index so-and-so wasn't to be found.
Using the debugger proves quite cumbersome, too.
I had a situation where, while running my main(), I could interact with the IPython shell and do things like:
main.my_plot_function()
from which I would get a standard output, and see what is wrong. Although sub-optimal, this did the trick.
I had to reset Spyder this morning (wasn't launching on Windows), and since then, when I launch my script, the console is unresponsive. So I can't do `main.my_plot_function()' anymore.
Generally speaking, is there a way to instruct Spyder or the console that I want to see what's going on in the background? Some "verbose" switch?
I am not sure what you mean by wanting to know what's going on in the background. I assume that you wish to know at many points in the code, what the variable types and values are and/or where the current point of execution is.
There are two options:
1) Use print statements wherever you need to know what's going on. For example, if you have a plot function, simply put some print statements inside the function to print out the sizes of the lists/arrays being plotted etc. You can also look for useful functions in this regard, i.e., type() to print out the type of a variable to make sure it is what you think it is, print(locals()) to print names and values of all local variables etc.
2) Use pdb to introduce break points and run your main script from the command line. This will stop the script execution where you want and from the pdb console, you can inspect the data-structures. There are of course other debuggers you can use, such as pudb (with a basic GUI and some extra features than pdb).
There is no general "verbose" mode in spyder or any other Python IDE I know of.
I am new to Python, have some experience in MatLab and r. My question is: Is it possible to run part of the code in .py block by block (line by line)?
In r or Matlab, I can first have some data and variables loaded in the memory first. Then experimentally I can run a line or two to try out the syntax... this is particularly useful for new learners I believe. I know there is something called the iPython which can execute Python code line by line however this is not what I am after. Thanks.
Since ipython has already been discounted, I'm not sure this answer will be better. But I will tell you the two things that I do.
I drop into the debugger at the point where I want to "try out" something, so the code will run up to that point, and then drop me into the debugger. You do this simply by inserting this code at that point:
import pdb; pdb.set_trace()
Once you've done what needs to be done, you can either press q to quit, or c to continue running the process.
I use the -i option to python. This enters interactive mode at the end of your python code. This is useful if you want to set up a bunch of data structures, and try out some code on it, instead of typing all of it into a python shell first. (that might be why you rejected ipython?)
I think what you need is a debugger.
You can use the pydev plugin for Eclipse which has a debugger.
Another option is pdb as already suggested but it's not very easy to use.
I'm working on a python project that spans across multiple files and directories. Here's my workflow:
Run main python script
Main script calls some functions in other files
Functions in other files/directories execute
In the middle of execution, there is a bug in one of the functions, but I find the bug only after the main script finishes. Sometimes, there may not be a bug, but rather some parameter that needs tweaking.
I go back and fix the bug/make the necessary tweaks and re-run the main program and this time it executes fine.
Obviously, this workflow is terribly inefficient as considerable amount of code (that runs prior to the buggy function) gets re-executed. What would be ideal is to run the program in ipython and after discovering the issue and making the necessary changes, restart from the place where the buggy function executions starts and not from the beginning. I'm not sure how to achieve this and any help would be much appreciated.
I know how to rerun lines from ipython history (%rerun) and how to ensure autoreload of changed files in ipython, but in this case, I can't really type out the lines of code into ipython. Writing unit tests may not always be feasible, so I need an alternate solution. My use case is something similar to setting a "breakpoint" and then re-executing code past the breakpoint multiple times so as to avoid re-executing the code prior to the breakpoint more than once, while ensuring that all the necessary variables (until that stage) are correctly populated. One final condition is that I may not be able to use an IDE and vim is the only editor available across all the environments I work with.
You could start writing test cases for every function and try to debug each function separately instead of the whole program/script.
There is a python unittest-module: https://docs.python.org/3.4/library/unittest.html and a lot of tutorials like (just an example): http://docs.python-guide.org/en/latest/writing/tests/
It seems annoying writing tests but thinking about test cases gives deeper understanding of "How should the function behave if...".
You could use the code module to include "breakpoints" in your code
import code
# ... later in your program add
code.interact(local=locals()) # enter python interpreter at this point (ctrl+D to continue execution)
This is absolutely frustrating, but I am not sure if the following is an issue only on my machine or with IDLE in general.
When attempting to print a long list in the shell, and that could happen by accident while debugging, the program crushes and you have to restart it manually.
Even worse, if you have a few editor windows open, it always spawns a few sub-processes, and each of these has to be manually shut down from the task manager.
Is there any way to avoid that?
I am using Python 3, by the way.
The Squeezer extension addresses this problem. Instead of displaying the long text (which is the source of the slow-down), Squeezer captures the output and displays a button instead. You have the option of expanding the contents or viewing it in Notepad.
Squeezer is included in IdleX, which you may find useful.
It seems tk needs to write a lot of data in the shell frame, this takes a lot of time and when the list is long then it becomes unresponsive.
I did:
>>a = range(n)
>>print(list(a))
It was OK for n = 100 or n = 1000. After that point things started being slow. For n as low as 10000, moving the page up and down becomes very, very slow.
I suspect that there is no solution. For small values of n probably it would help to clear() the window but unfortunately idle have not implemented any method to clear the shell. I dont understand why because it should not be difficult...
Edit 2012:
During the last months IDLE behavior and functionality got many important improvements through the collection of extensions and plugins provided by IdleX .
Accidental printing of high amounts of data is not a problem anymore. When IdleX detects such a situation it shows a warning indicating the existence of the data instead of printing it. Righ-clicking on the warning produces a preview of the data, not in idle shell but on the system text editor (notepad in windows). In this way the shell doesnt get cluttered with data nor slow down.