emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] Problem with python session


From: William Henney
Subject: Re: [O] Problem with python session
Date: Thu, 6 Oct 2016 12:54:49 -0500

Hi Florian

I can reproduce your problem.  This is (arguably) a bug in ob-python when using the vanilla python interpreter together with the :session argument.  You can work around it by putting a blank line after the for-loop in your second code block.  

I say that it is arguable that this is a bug or not since you would have exactly the same error if you were to literally type your code block in at the python interactive prompt.  That is, you have to give a second newline in order to close the loop and return to the top-level prompt.  However, it is admittedly confusing to have different behavior with and without the ":session" argument. 

I had never come across this bug myself, since I use ob-ipython for interactive python sessions (https://github.com/gregsexton/ob-ipython)   

Here is a minimal example that shows the problem.

Cheers

Will

* Test of ob-python in session mode with vanilla python interpreter

** FAILS: Without blank line after indented loop
#+BEGIN_SRC python :session *ob-python session* 
for x in 1, 2:
    pass
x
#+END_SRC

#+RESULTS:

An error message appears in the =*ob-python session*= buffer, which can be visited via =C-c C-v C-z= with point inside the code block. 

#+BEGIN_EXAMPLE
>>> 'org_babel_python_eoe'
>>> 'org_babel_python_eoe'
>>> for x in 1, 2:
...     pass
... x
  File "<stdin>", line 3
    x
    ^
SyntaxError: invalid syntax
#+END_EXAMPLE

** SUCCEEDS: With blank line after indented loop
#+BEGIN_SRC python :session *ob-python session* 
for x in 1, 2:
    pass

x
#+END_SRC

#+RESULTS:
: 2

** SUCCEEDS: Without using a session 
#+BEGIN_SRC python :return x
for x in 1, 2:
    pass
x
#+END_SRC

#+RESULTS:
: 2

** SUCCEEDS: Using ob-ipython instead of ob-python
#+BEGIN_SRC ipython :session
for x in 1, 2:
    pass
x
#+END_SRC

#+RESULTS:
: 2


On Thu, Oct 6, 2016 at 7:41 AM, Florian Lindner <address@hidden> wrote:
Hello,

I have an org file:

* Overview of available basis functions
#+BEGIN_SRC python :session generateBFpics :exports results :results file
  import matplotlib.pyplot as plt
  import numpy as np

  def set_plotoptions():
      plt.xlabel("x")
      plt.ylabel("$\phi(x)$")
      plt.grid()


  np.seterr(invalid='ignore')

  x = np.linspace(-3, 3, 1000)

  plt.plot(x,  np.log(abs(x))*np.power(x, 2))
  plt.suptitle("Thin Plate Splines")
  plt.title("$\phi(|x|) = \log(x) \cdot x^2$")
  set_plotoptions()
  plt.savefig("bf-tps.pdf")
  plt.close()
  "bf-tps.pdf"
#+END_SRC

#+RESULTS:
[[file:bf-tps.pdf]]

#+BEGIN_SRC python :session generateBFpics :exports results :results file
  for shape in [1, 2, 3, 4]:
      plt.plot(x, np.power(shape, 2) + np.power(x,2), label = "s = %i" % shape)
  plt.suptitle("Multi Quadrics")
  plt.title("$\phi(|x|) = s^2 + x^2$")
  plt.legend()
  set_plotoptions()
  plt.savefig("bf-multiquadrics.pdf")
  plt.close()
  "bf-multiquadrics.pdf"
#+END_SRC

#+RESULTS:
[[file:bf-multiquadrics.pdf]]



Both PDFs are generated. But only the first one has the content I expect, the othe one is an empty plot (it's a plot,
yes, but empty axes.

When I copy these pieces of code into on .py file it works just great. To my understanding that just how session mode works.

What could be the problem here?

Thanks,
Florian





--

  Dr William Henney, Instituto de Radioastronomía y Astrofísica,
  Universidad Nacional Autónoma de México, Campus Morelia

reply via email to

[Prev in Thread] Current Thread [Next in Thread]