11

So I've been experimenting with numpy and matplotlib and have stumbled across some bug when running python from the emacs inferior shell.

When I send the py file to the shell interpreter I can run commands after the code executed. The command prompt ">>>" appears fine. However, after I invoke a matplotlib show command on a plot the shell just hangs with the command prompt not showing.

>>> plt.plot(x,u_k[1,:]);
[<matplotlib.lines.Line2D object at 0x0000000004A9A358>]
>>> plt.show();

I am running the traditional C-python implementation. under emacs 23.3 with Fabian Gallina's Python python.el v. 0.23.1 on Win7.

A similar question has been raised here under the i-python platform: running matplotlib or enthought.mayavi.mlab from a py-shell inside emacs on windows

UPDATE: I have duplicated the problem on a fresh instalation of Win 7 x64 with the typical python 2.7.2 binaries available from the python website and with numpy 1.6.1 and matplotlib 1.1.0 on emacs 23.3 and 23.4 for Windows.

There must be a bug somewhere in the emacs shell.

Community
  • 1
  • 1
octi
  • 1,470
  • 1
  • 17
  • 28
  • Also, If I run the same commands in the python environment started from the windows command shell everything works fine. So it's just the python inferior from emacs that's giving me trouble. – octi Feb 01 '12 at 19:25
  • Have you tried using `ansi-term` or `eshell` I remember reading that some applications don't like the IO redirection that `M-x shell` provides. For an overview see this [article on alternative shells](http://www.masteringemacs.org/articles/2010/11/01/running-shells-in-emacs-overview/). – Devin M Feb 01 '12 at 22:20
  • how would I specify in emacs for python to run within the eshell or ansi-term rather than the shell invoked via M-x shell? I didn't see a place where the tutorial specified that. – octi Feb 02 '12 at 20:01
  • So I tried with python-mode.el also and the same behavior is occuring. So maybe there's something wrong with my emacs inferior shell? – octi Feb 03 '12 at 23:49

5 Answers5

4

I think there are two ways to do it.

  1. Use ipython. Then you can use -pylab option. I don't use Fabian Gallina's python.el, but I guess you will need something like this:

    (setq python-shell-interpreter-args "-pylab")
    

    Please read the documentation of python.el.

  2. You can manually activate interactive mode by ion

    >>> from matplotlib import pyplot as plt
    >>> plt.ion()
    >>> plt.plot([1,2,3])
    [<matplotlib.lines.Line2D object at 0x20711d0>]
    >>>
    
tkf
  • 2,990
  • 18
  • 32
  • 1
    so the above piece of code works but when I try to do print('hello') right after the command prompt hangs. Can anyone duplicate my issue? – octi Feb 03 '12 at 19:05
  • That's strange... Maybe unleavened, but do you start python with same option? You can check it by printing `sys.argv` in both emacs shell and windows shell. – tkf Feb 03 '12 at 21:31
  • Does print work after you close the window? I mean, (1) execute the code above, (2) execute `print("hello")` but nothing is printed, (3) close matplotlib window, and then you see "hello", right? – tkf Feb 04 '12 at 00:19
  • And what do you mean by "started from the windows command shell"? You start some program that opens python prompt or you open windows command prompt and type `python` and hit enter to start the python shell? If the former is true, maybe there is some magic going on which I don't know because I don't use windows... – tkf Feb 04 '12 at 00:29
  • Windows COmmand prompt> type python > then send the commands to the interpreter. No wierd behavior here. As for the emacs shell, even if I close the window the prompt doesn't appear. Moreover, even if I don't invoke the show command after invoking plot, the command prompt hangs/ doesn't appear. The interpreter's are the same. The shells are different. – octi Feb 05 '12 at 18:26
  • So how about starting windows shell inside of Emacs by `M-x shell` then start python i the shell, as Devin M in the comments of your question? // Anyway, you may want to send a bug report to the author of python.el. – tkf Feb 06 '12 at 06:15
  • Did so and no dice. Had to start python with the -i argument and again after invoking plot, the next command hangs. I switched to python-mode.el and it still behaves the same. – octi Feb 06 '12 at 15:25
  • What happens if you run python in un-buffered mode, using ``python -u``? If it solves the problem, maybe putting `(setenv "PYTHONUNBUFFERED" "x")` in your emacs setting helps. Relevant questions: http://stackoverflow.com/questions/2881346/emacs-python-running-python-shell-in-line-buffered-vs-block-buffered-mode; http://stackoverflow.com/questions/107705/python-output-buffering – tkf Feb 06 '12 at 16:18
  • Tried with no luck. I can't invoke python -u from the emacs shell because it ALSO hangs. I can only do -i. Tried using the setenv in the initfile but the issue still persists. I succesfully duplicated the issue on two additional Win7 platforms. There must be something wrong with the emacs build for windows. I put the version names in the update above. – octi Feb 06 '12 at 19:21
  • How about putting `(add-hook 'python-mode-hook (lambda () (setq process-connection-type nil)))` Maybe you knew, but I just found this : http://mail.python.org/pipermail/python-mode/2010-April/000822.html // Also, you can check default process-connection-type by ` v process-connection-type RET`. – tkf Feb 06 '12 at 20:23
  • No dice. Still same behavior. It looks pretty persistent – octi Feb 07 '12 at 19:20
  • Did you try ipython and ipython.el? From the link you posted in the question it works in that setting http://stackoverflow.com/questions/4701607/running-matplotlib-or-enthought-mayavi-mlab-from-a-py-shell-inside-emacs-on-wind – tkf Feb 07 '12 at 22:31
  • I'm used to the C implementation of Python and my porject heavily relies on libraries that are based on python C-bindings that my group and I developed. Switching to ipython would be most undesireable. – octi Feb 13 '12 at 20:08
2

You can use different back-end:

matplotlib.use('TkAgg')
import matplotlib.pyplot as plt 

Other GUI backends:

  • TkAgg
  • WX
  • QTAgg
  • QT4Agg

If you are using Elpy run your code using C-u C-c C-c

Mahyar
  • 21
  • 2
0

Well after a tremendous amount of time and posting the bug on the matplotlib project page and the python-mode page I found out that supplying the arguments console --matplotlib in ipython.bat will do the trick with matplotlib 1.3.1 and ipython 1.2.0

This is what I have in my iphython.bat

@python.exe -i D:\devel\Python27\Scripts\ipython-script.py console --matplotlib %*

octi
  • 1,470
  • 1
  • 17
  • 28
0

I think that this might have something to do with the behavior of the show function:

matplotlib.pyplot.show(*args, **kw)

When running in ipython with its pylab mode, display all figures and return to the ipython prompt.

In non-interactive mode, display all figures and block until the figures have been closed; in interactive mode it has no effect unless figures were created prior to a change from non-interactive to interactive mode (not recommended). In that case it displays the figures but does not block.

A single experimental keyword argument, block, may be set to True or False to override the blocking behavior described above.

I think your running into the blocking behavior mentioned above which would result in the shell hanging. Perhaps try running the function as: plt.show(block = False) and see if it produces the output you expect. If this is still giving you trouble let me know and I will try and reproduce your setup locally.

Devin M
  • 9,636
  • 2
  • 33
  • 46
  • So I experimented with the suggestions but I am still getting weird behaviours. So the command prompt comes back up until I invoke the matplotlib.pyplot.plot function. After that it just hangs `>>> print('hello')` `hello` `>>> plt.ion()` `>>> print('hello')` `hello` `>>> plt.plot(x,psi **2)` `[]` `>>> print('hello')` – octi Feb 02 '12 at 19:54
  • Are you sure you are running same version of python with same PYTHONPATH environment variable in the shell outside of Emacs and from Emacs? You can check the path by `print sys.path`. After checking that, maybe checking what backend you are using helps. Just do `print matplotlib.rcParams['backend']`. – tkf Feb 03 '12 at 11:14
  • I did so both in the Windows shell and emacs shell and the same PYTHONPATH is used. doing the rcParams both return TkAgg. Does this mean anything? – octi Feb 03 '12 at 18:57
  • I thought maybe you are loading different python modules (which occurs when you have differnt PYTHONPATH) and using different backend. If the backend does not support interactive mode, your python shell hangs. – tkf Feb 03 '12 at 21:22
0

I think I have found an even simpler way to hang the inferior shell but only when pdb is invoked. Start pdb by supplying 'python' as the program to run.

Try this code:

print "> {<console>(1)<module>() }"
Ken
  • 1
  • 1
    The objective here is to not hang the inferior shell but actually stop it from hanging:P Is that what you intended to say? – octi Mar 21 '12 at 22:18