pythons

which interpreter a folder runs in, and the environment a child of it wants

Select the Python interpreter for a folder and build the environment for its child processes.

use_app sets the application name and environment-variable prefix used by Kunda defaults.


source

app_env

def app_env(
    name, default:NoneType=None
):

What the environment holds for env_name(name), asked now rather than at import.


source

env_name

def env_name(
    name
):

name under the host’s prefix, so MAX_KERNELS is KUNDA_MAX_KERNELS by default.


source

app_name

def app_name():

What the host calls itself: kunda, until use_app says otherwise.


source

use_app

def use_app(
    name:str='kunda', env_prefix:str='KUNDA_'
):

Name the host application, so what kunda reads and what it says are spelled the host’s way.

VENV_PYTHONS lists common virtual-environment interpreter paths in lookup order.

VENV_PYTHONS[:4]
('.venv/bin/python',
 '.venv/Scripts/python.exe',
 'venv/bin/python',
 'venv/Scripts/python.exe')

BUNDLE_ONLY lists interpreter variables inherited from a frozen application.


source

clean_env

def clean_env():

This process’s environment, safe to hand to a child.


source

strip_bundle

def strip_bundle(
    env, frozen:NoneType=None
):

Strip py2app/py2exe bundle vars from env; no-op outside a bundle.

strip_bundle removes frozen-application interpreter variables. Other environments are unchanged.

env = {'PYTHONHOME': '/App.app/Contents/Resources', 'PATH': '/usr/bin', 'HOME': '/Users/me'}
strip_bundle(dict(env), frozen=True)
{'PATH': '/usr/bin', 'HOME': '/Users/me', 'PYTHONUTF8': '1'}

The returned environment enables UTF-8 mode.

test_eq(strip_bundle(dict(env), frozen=False), env)             # nothing claimed, nothing changed
out = strip_bundle(dict(env), frozen=True)
assert not (set(BUNDLE_ONLY) & set(out)), 'every redirection name is gone'
test_eq(out['PATH'], '/usr/bin')                                # and nothing else is touched
test_eq(out['PYTHONUTF8'], '1')

clean_env applies strip_bundle to the current process environment.


source

venv_env

def venv_env(
    python:NoneType=None, env:NoneType=None
):

env (this process’s, by default) with python’s virtual environment in front of it.

venv_env puts the selected virtual environment first on PATH and sets VIRTUAL_ENV.

e = venv_env('/repo/.venv/bin/python', env={'PATH': '/usr/bin'})
e['VIRTUAL_ENV'], e['PATH']
('/repo/.venv', '/repo/.venv/bin:/usr/bin')

source

nearest_marked

def nearest_marked(
    start, markers, stop:NoneType=None
):

Walk up from start to stop, return (dir, marker) for the first dir holding a marker.

nearest_marked walks from start toward its parents. It returns the first directory containing one of markers.

d, hit = nearest_marked(root/'repo'/'src'/'pkg', ['.git'])
d.name, hit.name
('repo', '.git')

source

nearest_python

def nearest_python(
    start, stop:NoneType=None
):

The nearest conventional venv interpreter at or above start, not searched past stop.


source

project_python

def project_python(
    roots:tuple=()
):

The first conventional project-local virtualenv interpreter among roots, or None.

The lookup helpers use the same marker and path rules.

outer = mkvenv(root/'repo')
nearest_python(root/'repo'/'src'/'pkg').replace(str(root), '/proj')   # the temp root stands in for a checkout
'/proj/repo/.venv/bin/python'

The nearest virtual environment wins. A nested checkout can use its own interpreter.


source

python_for

def python_for(
    cwd:NoneType=None, stop:NoneType=None, roots:tuple=(), default:NoneType=None
):

Pick interpreter for cwd: walk up to stop, then default, then first venv in roots; None means use current.

python_for returns the interpreter used for processes started from a folder. stop limits the parent search.


source

find_pythons

def find_pythons(
    roots:tuple=(), current:NoneType=None, this_label:str='this one'
):

Kernel launch options: this interpreter (current) and all reachable venvs.current is resolved by python_for and may be deeper than standard search.Listing enables picker selection.

find_pythons returns interpreters for a picker. It removes duplicate paths.

rows = find_pythons([root/'repo'], current=str(inner))
[r['label'] for r in rows]
['this one', 'src/.venv', 'repo/.venv', 'python3 on PATH', 'python on PATH']