I want to take a literate programming approach to managing my library of code snippets with org-babel.

I have a ~/Code folder which I use to save various org-mode documents, each of which contains a number of library functions, embedded in org-babel code blocks.

My question is, how can I now #include or (require ') these functions in subsequent org-mode documents?

2 Answers 2


See https://orgmode.org/manual/Library-of-Babel.html#index-babel_002c-library-of where the call to org-babel-lob-ingest is documented, which is bound to C-c C-v i and then https://orgmode.org/manual/Evaluating-code-blocks.html#Evaluating-code-blocks where you have the #+CALL: syntax for calling remote code blocks.

Also have a look at the documentation at https://orgmode.org/worg/org-contrib/babel/intro.html and Config, examples and use cases of Library Of Babel and the related questions there.

  • 1
    could you please say what arguments you use with the #+CALL from a igested code src-block, I can't manage to get it to work and the documentation is very minimal. What I was trying to do is to define a Python class in one block and call it in another using #+CALL, tangling works, session works but with #+CALL I only manage to evaluate a result and not getting that class in the following src block. Commented Apr 2, 2018 at 18:15


First thing that comes to mind is :session <session_name> code block that share the same session name form part of the same namespace. You will need to open all the org files with concerning code and execute it to using the :session header argument (empty loads all code into default session or supply a session name).

#+src_begin sh :session my_session_01

2. Use :tangle to write all your code (of the same language) to a file. for example:

#+begin_src python :tangle my_code.py

All the code in an org file can be written with one command, C-c C-v f M-x org-babel-tangle-file

Example in python

First code block:

#+BEGIN_SRC python :results output :tangle ~/tangled.py

a = 'ape'
b = 'bat'


Second code block:

#+BEGIN_SRC python :results output :tangle ~/tangled.py

def tangled(x, y):
    return x + y

print(tangled(a, b))

running python tangled.py in bash gives:

[adam@adam-pc ~]$ python tangled.py 

Which is what's expected from the two pieces joint.

Loading the tangled file to a new src code in another org file will be dependent on language. I used a general cumbersome method in this case of Python, I normally will have added an __init__.py file in that folder and that would have made importing much more streamline, I rather have in this case a simple to read code, since it's not about the specifics of any language therefore this is the code that includes the import plus the result:

#+BEGIN_SRC python :results output org drawer 
import sys
import os
from tangled import *
print(tangled('eggs',' and spam')



>>> eggs and spam


I am sure this can be improved with a header argument. perhaps in :properties:

I will look at that and edit accordingly.

header arguments in org-babel

the org manual - headers

Edit 1:

In case the org file code blocks are of the same language you can tangle globally using the following property to influence the entire buffer:

#+PROPERTY: header-args :tangle yes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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