* Re: python :session and org_babel_python_eoe
2011-02-10 17:46 python :session and org_babel_python_eoe Myles English
@ 2011-02-10 18:45 ` Dan Davison
0 siblings, 0 replies; 2+ messages in thread
From: Dan Davison @ 2011-02-10 18:45 UTC (permalink / raw)
To: Myles English; +Cc: emacs-orgmode
Myles English <mylesenglish@gmail.com> writes:
> Hello,
>
> Is this behaviour expected?:
>
> #+begin_src python :session
> import sys
> #+end_src
>
> #+results:
>
> But in the python interpreter there is this:
>
> """
>>>> import sys
>
>
> open('/home/myles/tmp/scratch/python-4938gzQ', 'w').write(str(_))
>
>
> 'org_babel_python_eoe'
>>>> >>> >>> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> NameError: name '_' is not defined
>>>> >>> >>> 'org_babel_python_eoe'
Hi Myles,
Firstly, I'm aware that python sessions are a little rough round the
edges in places. I've recently started to use them quite a bit and would
like to improve them, so any reports of problems you encounter will be
appreciated.
Yes, this is expected, although we could do without it. Since you did
not specify :results, you are in the default "value" mode, which
attempts to return to the org buffer the last value computed, using the
special variable _. However, _ does not yet exist as you haven't
computed anything.
You could do
#+begin_src python :session
import sys
None
#+end_src
Or you could use :results output here, which will result in a cleaner
session buffer in this particular case.
I wonder whether it would make sense to support :results nil, meaning "I
know in advance that I want the result to be nil, so don't try to
retrieve any results for me". That may be useful in order to execute a
src block that would return an extremely large result -- one which you
really don't want emacs to try to deal with at all. Perhaps the value
of :results could even be allowed to be a lisp form in general, in
addition to "output" and "value" and "silent".
The open(...).write(str(_)) is org-babel internals -- that's how Org
gets the value out of python and back to the org buffer. Ideally, it
would not be exposed to the user -- but so far R is the only babel
language where we've hidden it.
So the solution in terms of babel development could be
1. Test for the existence of _ before attempting to write it. Perhaps
using locals().get('_') is None
and
2. Don't echo that stuff to the python session buffer
Also, the 'org_babel_python_eoe', shouldn't really be there -- it only
makes sense in :results output mode.
Dan
>>>>
> """
>
> The sys module did indeed import correctly though. And that file
> (/home/myles/tmp/scratch/python-4938gzQ) is empty.
>
> Versions:
> - Python 2.7.1 (r271:86832, Jan 6 2011, 11:45:30) [GCC 4.5.2] on
> linux2
> - orgmode from git 7473e3 Wed Feb 9 17:30
> - GNU Emacs 23.2.1(x86_64-unknown-linux-gnu)
>
> It looks related to the issue, described as being fixed, in this
> thread: ("org-babel :session breaks returning things with python?")
> http://news.gmane.org/find-root.php?message_id=%3c871v669swp.fsf%40gmail.com%3e
>
> Thanks,
>
> Myles
>
> _______________________________________________
> Emacs-orgmode mailing list
> Please use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
^ permalink raw reply [flat|nested] 2+ messages in thread