emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [Babel] Strange bug in org-babel with python
@ 2009-11-10 23:26 Darlan Cavalcante Moreira
  2009-11-12  0:44 ` Dan Davison
  0 siblings, 1 reply; 3+ messages in thread
From: Darlan Cavalcante Moreira @ 2009-11-10 23:26 UTC (permalink / raw)
  To: org-mode Mailinglist


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.

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Babel] Strange bug in org-babel with python
  2009-11-10 23:26 [Babel] Strange bug in org-babel with python Darlan Cavalcante Moreira
@ 2009-11-12  0:44 ` Dan Davison
  2009-11-12  2:58   ` Darlan Cavalcante Moreira
  0 siblings, 1 reply; 3+ messages in thread
From: Dan Davison @ 2009-11-12  0:44 UTC (permalink / raw)
  To: Darlan Cavalcante Moreira; +Cc: org-mode Mailinglist

Darlan Cavalcante Moreira <darcamo@gmail.com> 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Babel] Strange bug in org-babel with python
  2009-11-12  0:44 ` Dan Davison
@ 2009-11-12  2:58   ` Darlan Cavalcante Moreira
  0 siblings, 0 replies; 3+ messages in thread
From: Darlan Cavalcante Moreira @ 2009-11-12  2:58 UTC (permalink / raw)
  To: Dan Davison; +Cc: org-mode Mailinglist


Hi Dan,

Thank you for the solution and specially for the explanation.  The
real problem was not understanding what was wrong. But with the
explanation in the other thread (that you also added in worg) thinks
make sense now.

- Darlan


At Wed, 11 Nov 2009 19:44:34 -0500,
Dan Davison <davison@stats.ox.ac.uk> wrote:
> 
> Darlan Cavalcante Moreira <darcamo@gmail.com> 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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-11-12  2:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-10 23:26 [Babel] Strange bug in org-babel with python Darlan Cavalcante Moreira
2009-11-12  0:44 ` Dan Davison
2009-11-12  2:58   ` Darlan Cavalcante Moreira

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).