From mboxrd@z Thu Jan 1 00:00:00 1970 From: Juan Reyero Subject: Re: Buffer-wide definitions in org-babel Date: Thu, 8 Oct 2009 11:22:58 +0200 Message-ID: <55bd243d0910080222q34a7ac79g26a7f9358d806e62@mail.gmail.com> References: <55bd243d0910070720i3085af58xc30cf14011d6375a@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Return-path: Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1MvpDG-0007DP-Ho for emacs-orgmode@gnu.org; Thu, 08 Oct 2009 05:23:06 -0400 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1MvpDA-00077m-CV for emacs-orgmode@gnu.org; Thu, 08 Oct 2009 05:23:04 -0400 Received: from [199.232.76.173] (port=57721 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1MvpDA-00077Z-2R for emacs-orgmode@gnu.org; Thu, 08 Oct 2009 05:23:00 -0400 Received: from mail-fx0-f205.google.com ([209.85.220.205]:62250) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1MvpD9-0008Pj-Lc for emacs-orgmode@gnu.org; Thu, 08 Oct 2009 05:22:59 -0400 Received: by fxm1 with SMTP id 1so4912585fxm.31 for ; Thu, 08 Oct 2009 02:22:58 -0700 (PDT) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Eric, Thanks a lot for your quick response. I have tried your suggestion and it does work, but it behaves in an unexpected way when I do some minor modifications. Please see below. On Wed, Oct 7, 2009 at 8:48 PM, Eric Schulte wrote= : >> I am trying to define buffer-wide initializations in org-babel, so that = I can import a python module once >> and then use its exported symbols in all the code chunks throughout the = buffer. =A0Is there a way to do it? >> =A0I have tried all the obvious approaches and none seems to work. =A0(M= y hope was that I could define a >> :session and then use it in every chunk, but python doesn't like it). > > I believe you are on the right track by trying to use sessions. =A0The > following works for me > > --8<---------------cut here---------------start------------->8--- > ** persistent python > #+begin_src python :session :results silent > =A0import types > #+end_src > > #+begin_src python :session > =A0types.FunctionType > #+end_src > > #+resname: > : function > --8<---------------cut here---------------end--------------->8--- This works, but see what happens with this (no previous src chunks): #+begin_src python :session :results output 2 #+end_src #+resname: : 2 : 2 (expected nothing, which is what I get if I remove the :session). #+begin_src python :session :results value 2 #+end_src #+resname: : 0 This is how my python buffer looks like after processing this last chunk in a fresh session: --8<---------------cut here---------------start--------------->8--- >>> import emacs; print '_emacs_out ()' /Applications/Emacs.app/Contents/Resources/etc/emacs.py:24: DeprecationWarning: the sets module is deprecated from sets import Set 2 _ 'org_babel_python_eoe' 2 2 >>> >>> >>> _ 2 >>> 'org_babel_python_eoe' 'org_babel_python_eoe' >>> --8<---------------cut here---------------end--------------->8--- But, interestingly, if I return a string instead of a number, as you do in your example, it works: #+begin_src python :session :results value "2" #+end_src #+resname: : 2 It is not related to a previous chunk messing up the python interpreter. I have moved to the git version, and it still behaves like this. I am using python 2.6.1. > Of if you grab the latest version of Org-mode from the git repo you can > set the session type in a headline property which would be more similar > to the file-wide behavior that you described. This is cool. It would also be great if you could define a default interpreter (I suspect most usage will involve a single interpreter per buffer). Thanks again! jm --=20 http://juanreyero.com/blog