Python has become one of the most popular languages for application development. It and JavaScript seem to be the major contenders for high-level work including working with AI.
(require 'ob-python)(describe-variable 'org-babel-python-command)
Python can manage local environments with dependencies using its
built-in venv functionality. Within a notebook this will be
supported through creating the relevant directory, and then
updating blocks to use the python within that directory.
uv may offer an easier approach for some of this…I’m currently reading
a couple python books that I hope cover that (but am not particularly
concerned either way).
Creating under /tmp seems like a reasonable practice by default.
Ocassionally something may get messed up, where recreating the venv is the brute force fix.
python3 -m pydoc
uv may offer an easier approach for some of this…I’m currently reading
a couple python books that I hope cover that (but am not particularly
concerned either way).
Creating under /tmp seems like a reasonable practice by default.
Ocassionally something may get messed up, where recreating the venv is the brute force fix.
This invokes the host python.
${host_py} -m venv ${venv}tree ${venv}Dependencies can be installed within the venv by invoking pip with
the venv python.
${venv_py} -m pip install tiktokenThis currently is a bit stuck in the middle of Packaging Up Behaviors, so the idea for now would be to typically just provide all of the header args.
The function defined here can be used to produce the values which can be invoked to insert the appropriate value.
(defun mw/pyvenv-header-args (host_py venv)
"Produce a value for header args with the provided host_py and venv variabels."
(let (venv-py (format "%s/bin/python" venv))
(insert (string-join
(list
(format ":var host_py=\"%s\"" host_py)
(format ":var venv=\"%s\"" venv)
(format ":var venv_py=\"%s\"" venv-py)
(format ":python \"%s\"" venv-py)
" ")))))This setup can be facilitated through the defining some common
variables on a file/subtree level…which are attached to this
Sample heading as an example.
This will rely on file local variables defined typically in the first line.
echo host_py: ${host_py}
echo venv: ${venv}
echo venv_py: ${venv_py}(org-entry-get (point) "header-args")echo host_py: ${host_py}
echo venv: ${venv}
echo venv_py: ${venv_py}python3 -m pydoc
python3 -m pydoc help
help('modules')
import os
return os.environimport subprocess
help(subprocess)