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.

4
  • 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. Commented Feb 1, 2012 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. Commented Feb 1, 2012 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. Commented Feb 2, 2012 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? Commented Feb 3, 2012 at 23:49

5 Answers 5

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>]
    >>>
    
Sign up to request clarification or add additional context in comments.

13 Comments

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?
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.
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?
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...
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.
|
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

Comments

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.

4 Comments

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) [<matplotlib.lines.Line2D object at 0x0000000004B87128>] >>> print('hello')
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'].
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?
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.
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>() }"

1 Comment

The objective here is to not hang the inferior shell but actually stop it from hanging:P Is that what you intended to say?
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 %*

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.