0

I am testing the possibility of including python code in org-mode files in emacs. As it is my first time with I ran into an error that probably it is easy to fix, but I cannot see the solution. I want to define some onstants and functions and use them later in the file. If I do this witho only one function there is no problem, as in this simple example:

* Initialization block containing constants and function definition
#+NAME: init_block 
#+BEGIN_SRC python :session 
  # Constants 
  a_constant = 287.
  #
  # 
  ##########################
  def m_T(E_val):
    import numpy as np
    A_val = 6.1094
    return A_val*np.log(E_val/A_val)
#+END_SRC

#+RESULTS: init_block



* Example

#+BEGIN_SRC python  :session   :results output
print "Hello, world! This is my zero trial ", a_constant
print m_T(10.)
#+end_src

#+RESULTS:
: Hello, world! This is my zero trial  287.0
: 3.01044670959

However, when I try to include the definition of two functions errors pop up:

* Initialization block containing constants and function definition
#+NAME: init_block 
#+BEGIN_SRC python :session b
  # Constants 
  a_constant = 287.
  #
  # 
  ##########################
  def m_T(E_val):
    import numpy as np
    A_val = 6.1094
    return A_val*np.log(E_val/A_val)
  #################
  b_constant = 287.
  #################
  def m_E(T_val):
    import numpy as np
    A_val = 6.1094
    return A_val*np.exp(T_val/A_val)
  #################
  c_constant = 287.
  ##########################
#+END_SRC

#+RESULTS: init_block



* Example

#+BEGIN_SRC python  :session b  :results output
print "Hello, world! This is my zero trial ", a_constant, b_constant, c_constant
print m_T(10.)
print m_E(12.3)
#+end_src

#+RESULTS:
#+begin_example
Hello, world! This is my zero trial  287.0
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'b_constant' is not defined
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'm_T' is not defined
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'm_E' is not defined
#+end_example

I would appreciate any help to understand what am I doing wrong in this case.

0

Your problem reminds me why I hate Python (I very seldom give such a statement about a programming language). Whitespace is syntactically relevant in Python. Your problem is superfluous indention.

You can switch to the python console for session b with C-x b *b* RET. There you also get the error messages of the python interpreter.

Maybe you should use rather org-edit-special (C-c ', it est C-c and a quote character) in python source blocks to avoid indention problems. With point in a source block org-edit-special translates to org-edit-src-code.

The following org file works:

* Initialization block containing constants and function definition
#+NAME: init_block
#+BEGIN_SRC python :session b
# Constants
a_constant = 287.
#
#
##########################
def m_T(E_val):
 import numpy as np
 A_val = 6.1094
 return A_val*np.log(E_val/A_val)

#################
b_constant = 287.
#################
def m_E(T_val):
 import numpy as np
 A_val = 6.1094
 return A_val*np.exp(T_val/A_val)

#################
c_constant = 287.
##########################
#+END_SRC

#+RESULTS: init_block

* Example

#+BEGIN_SRC python  :session b  :results output
print "Hello, world! This is my zero trial ", a_constant, b_constant, c_constant
print m_T(10.)
print m_E(12.3)
#+end_src

#+RESULTS:
: Hello, world! This is my zero trial  287.0 287.0 287.0
: 3.01044670959
: 45.7466956505
  • Thanks for your answer and for the tip about the Python console. I use Python but, I agree with you, compared to other, more flexible, languages the indentation can be a real problem. And when you code directly in emacs this is easily controlled, but not in source blocks in org-mode. – Currix Jan 26 '18 at 13:23
  • @Currix I added a comment on using org-edit-special for editing source code blocks. – Tobias Jan 26 '18 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.