In the following link you can find the following simple example:
#+begin_src python :session :results file
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(3,2))
plt.plot([1,3,2])
fig.tight_layout()
plt.savefig('myfig.pdf')
'myfig.pdf' # return this to org-mode
#+end_src
I have the problem that the string that contains the file name is not returned. Thus, I don't get something like:
#+RESULTS:
[[file:images/myfig.pdf]]
Here is what I can see in the session's buffer:
Process Python finished
Python 3.4.3 (default, Feb 27 2015, 00:22:26)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> >>> >>> r/folders/kz/1c2cxn1x60n_t5p2j1p02b180000gn/T/py6499_X0''', 'exec'));
import matplotlib
>>> import matplotlib
matplotlib.use('Agg')
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt
fig=plt.figure(figsize=(3,2))
fig=plt.figure(figsize=(3,2))
plt.plot([1,3,2])
plt.plot([1,3,2])
fig.tight_layout()
fig.tight_layout()
plt.savefig('myfig.pdf')
plt.savefig('myfig.pdf')
'myfig.pdf' # return this to org-mode
'myfig.pdf' # return this to org-mode
open('/var/folders/kz/1c2cxn1x60n_t5p2j1p02b180000gn/T/babel-64998Up/python-6499xhD', 'w').write(str(_))
open('/var/folders/kz/1c2cxn1x60n_t5p2j1p02b180000gn/T/babel-64998Up/python-6499xhD', 'w').write(str(_))
'org_babel_python_eoe'
'org_babel_python_eoe'
>>> [<matplotlib.lines.Line2D object at 0x10bc987f0>]
>>> >>> 'myfig.pdf'
>>> >>> 9
>>> >>> >>> 'org_babel_python_eoe'
>>>
However, the following works as expected:
#+begin_src python :session :results file
'./myfig.pdf' # return this to org-mode
#+end_src
#+RESULTS:
[[file:./myfig.pdf]]
What is the problem here? How can I debug it? It seems like matplotlib
causes some problem.
Edit:
Same org file behaves as expected on a Linux box. The problem described above happens on Mac. So, probably it has something to do with the installed python versions. Still, I don't know how to debug it...
Edit 2 (Debugging steps):
Ok, I'm trying to break the steps. Consider the complete example:
#+BEGIN_SRC python :session :results file
import matplotlib #1
matplotlib.use('Agg') #2
import matplotlib.pyplot #3
fig=plt.figure(figsize=(3,2)) #4
plt.plot([1,3,2]) #5
fig.tight_layout() #6
plt.savefig('myfig.png') #7
'myfig.png' #8 return this to org-mode
#+END_SRC
If I comment lines 1-7, then the results block is correct:
#+RESULTS:
[[file:myfig.png]]
Now, I start to add the lines in their order. Each time I exit the interpreter, so it will be a "fresh" run.
With lines 1,8: The result is correct (as can be found above) With lines 1-2,8: Still correct With lines 1-3,8: Result is incorrect!
#+RESULTS:
[[file:]]
Therefore, if I understand correctly, this is not a problem of the interpreter, but something else. The version of python I use is (as indicated by import sys; sys.version
invoked from the interactive interpreter that was started by babel):
'3.4.3 (default, Feb 27 2015, 00:22:26) \n[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.56)]'
Furthermore, matplotlib.__version__
yields: `1.4.3'
Maybe this additional information can help someone solve this issue?