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 <schulte.e...@gmail.com> 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. Is there a way to do it? >> I have tried all the obvious approaches and none seems to work. (My 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. The > following works for me > > --8<---------------cut here---------------start------------->8--- > ** persistent python > #+begin_src python :session :results silent > import types > #+end_src > > #+begin_src python :session > types.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 -- http://juanreyero.com/blog _______________________________________________ Emacs-orgmode mailing list Remember: use `Reply All' to send replies to the list. Emacs-orgmode@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-orgmode