Skip to content

Latest commit

 

History

History
1013 lines (881 loc) · 34.4 KB

File metadata and controls

1013 lines (881 loc) · 34.4 KB

python

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.

Configuring for Babel

(require 'ob-python)
(describe-variable 'org-babel-python-command)

Setting up a Virtual Environment

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.

Help

pydoc

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.

Create the venv

This invokes the host python.

${host_py} -m venv ${venv}
tree ${venv}

Installing dependencies (w/ pip)

Dependencies can be installed within the venv by invoking pip with the venv python.

${venv_py} -m pip install tiktoken

Conventions

Current Guidance

This 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)
	      " ")))))

Sample

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}

Render the Property

(org-entry-get (point) "header-args")

Try Function

Nested

echo host_py: ${host_py}
echo venv: ${venv}
echo venv_py: ${venv_py}

Help

Help

pydoc

python3 -m pydoc

python3 -m pydoc help

List Available Modules

help('modules')

Env

Print Environment

import os

return os.environ

Processes

subprocess

import subprocess
help(subprocess)