From: John Kitchin <jkitchin@andrew.cmu.edu>
To: emacs-orgmode@gnu.org
Subject: odd/unexpected behavior with Python src blocks
Date: Sun, 28 Sep 2014 18:11:46 -0400 [thread overview]
Message-ID: <m2d2aftmxp.fsf@andrew.cmu.edu> (raw)
* Odd behavior in python src-blocks in org-mode
I found that some code blocks in Python execute due to apparently silent
errors! here is how to reproduce it with a vanilla emacs -q.
#+BEGIN_SRC emacs-lisp
(org-babel-do-load-languages
'org-babel-load-languages
'((emacs-lisp . t)
(python . t)))
(setq org-confirm-babel-evaluate nil)
#+END_SRC
#+RESULTS:
This block should raise an error (and does) because k is not defined.
#+BEGIN_SRC python
def f(y, x):
return k * y
print(f(1, 0))
#+END_SRC
#+RESULTS:
It raises this error.
#+BEGIN_EXAMPLE
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
File "<stdin>", line 2, in f
NameError: global name 'k' is not defined
#+END_EXAMPLE
However, this code block actually executes, and gives the wrong answer! If I open the source block in Python mode, it does not run without error.
#+BEGIN_SRC python :results output
from scipy.integrate import odeint
def f(y, x):
return k * y
print(odeint(f, 1, [0, 1]))
#+END_SRC
#+RESULTS:
: [[ 1.]
: [ 1.]]
Here is the correct answer.
#+BEGIN_SRC python :results output
from scipy.integrate import odeint
k = -1
def f(y, x):
return k * y
print(odeint(f, 1, [0, 1]))
#+END_SRC
#+RESULTS:
: [[ 1. ]
: [ 0.36787947]]
I am not sure why this happens, but it seems like incorrect behavior to
me.
--
-----------------------------------
John Kitchin
http://kitchingroup.cheme.cmu.edu
next reply other threads:[~2014-09-28 22:12 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2014-09-28 22:11 John Kitchin [this message]
2014-09-29 1:27 ` odd/unexpected behavior with Python src blocks Aaron Ecay
2014-09-29 15:56 ` Instructor account
2014-09-29 18:03 ` John Kitchin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.orgmode.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m2d2aftmxp.fsf@andrew.cmu.edu \
--to=jkitchin@andrew.cmu.edu \
--cc=emacs-orgmode@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).