From mboxrd@z Thu Jan 1 00:00:00 1970 From: Dan Davison Subject: Re: [Babel] Strange bug in org-babel with python Date: Wed, 11 Nov 2009 19:44:34 -0500 Message-ID: <87pr7or2q5.fsf@stats.ox.ac.uk> References: <4af9f6c1.0504c00a.3ba1.31cc@mx.google.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1N8Nno-0002qC-4J for emacs-orgmode@gnu.org; Wed, 11 Nov 2009 19:44:44 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1N8Nnj-0002je-21 for emacs-orgmode@gnu.org; Wed, 11 Nov 2009 19:44:43 -0500 Received: from [199.232.76.173] (port=49453 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1N8Nni-0002jV-VN for emacs-orgmode@gnu.org; Wed, 11 Nov 2009 19:44:38 -0500 Received: from markov.stats.ox.ac.uk ([163.1.210.1]:50644) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1N8Nni-0002cb-G3 for emacs-orgmode@gnu.org; Wed, 11 Nov 2009 19:44:38 -0500 In-Reply-To: <4af9f6c1.0504c00a.3ba1.31cc@mx.google.com> (Darlan Cavalcante Moreira's message of "Tue, 10 Nov 2009 20:26:51 -0300") 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: Darlan Cavalcante Moreira Cc: org-mode Mailinglist Darlan Cavalcante Moreira writes: > Hello org-users, > > Today I was bitten by a weird behavior when executing some python > blocks with org-babel that really confused me, but after some trying > and error I was able to isolate the problem. > > When a block has an import statement such as > ,---- > | from some_module import * > `---- > it works with :results output, but not with :results value. Hi Darlan, Thanks for your helpful observations in this thread and the other one. I'm fairly happy with the solution we've arrived at, but the example you give above actually still won't work in :result value non-session. The reason is that in that scenario (and only that one), in order to compute the value of the block, org-babel wraps the code in a function and evaluates that function. So the rules are now: in ':results value' non-session mode, you have to write code that is valid inside a function, and it seems that in python that is not true for from some_module import * I only learned this when studying your post. It seems that it's just the import * which is a problem; explicitly importing individual components or named modules is fine. So unless anyone knows better, I guess the answer is, Don't Do That :) There's a working version of your example below that avoids import *, with the new necessary 'return' statements added. Dan -------------------------------------------------------- * dc #+begin_src python :tangle test :results silent def double_input(a): return a*2 #+end_src #+begin_src python :results value import test def times_four(a): return test.double_input(a)*2 if __name__ == '__main__': print "Value is %s" % times_four(10) return times_four(10) #+end_src #+resname: : 40 #+begin_src python :results value from test import double_input def times_four(a): return double_input(a)*2 if __name__ == '__main__': print "Value is %s" % times_four(10) return times_four(10) #+end_src #+resname: : 40 -------------------------------------------------------- > > The content of simple file to reproduce the problem is showed below > > --------------- Cut here ----------------------------------- > * Test Org-babel > > #+begin_src python :tangle test :results silent > def double_input(a): > return a*2 > #+end_src > > #+begin_src python :results value > import test > > def times_four(a): > return test.double_input(a)*2 > > if __name__ == '__main__': > print "Value is %s" % times_four(10) > times_four(10) > #+end_src > > #+begin_src python :results value > from test import * > > def times_four(a): > return double_input(a)*2 > > if __name__ == '__main__': > print "Value is %s" % times_four(10) > times_four(10) > #+end_src > --------------- End of cut --------------------------------- > > To reproduce the problem, tangle the file to create test.py from the > first block. Then executing the second block (which has no import *) > works with either :results output or :results value. > > However, the third block, (which uses import *) only works if executed > with :results output. I can't see the reason for this since the code > is right and works in a python buffer as expected. > > - Darlan > > > > > _______________________________________________ > 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