# pythons


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

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

[`use_app`](https://vedicreader.github.io/kunda/pythons.html#use_app)
sets the application name and environment-variable prefix used by Kunda
defaults.

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/kunda/blob/main/kunda/pythons.py#L31"
target="_blank" style="float:right; font-size:smaller">source</a>

### app_env

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

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

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/kunda/blob/main/kunda/pythons.py#L27"
target="_blank" style="float:right; font-size:smaller">source</a>

### env_name

``` python
def env_name(
    name
):
```

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

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/kunda/blob/main/kunda/pythons.py#L23"
target="_blank" style="float:right; font-size:smaller">source</a>

### app_name

``` python
def app_name():
```

*What the host calls itself: `kunda`, until
[`use_app`](https://vedicreader.github.io/kunda/pythons.html#use_app)
says otherwise.*

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/kunda/blob/main/kunda/pythons.py#L18"
target="_blank" style="float:right; font-size:smaller">source</a>

### use_app

``` python
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.

``` python
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.

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/kunda/blob/main/kunda/pythons.py#L49"
target="_blank" style="float:right; font-size:smaller">source</a>

### clean_env

``` python
def clean_env():
```

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

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/kunda/blob/main/kunda/pythons.py#L42"
target="_blank" style="float:right; font-size:smaller">source</a>

### strip_bundle

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

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

[`strip_bundle`](https://vedicreader.github.io/kunda/pythons.html#strip_bundle)
removes frozen-application interpreter variables. Other environments are
unchanged.

``` python
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.

``` python
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`](https://vedicreader.github.io/kunda/pythons.html#clean_env)
applies
[`strip_bundle`](https://vedicreader.github.io/kunda/pythons.html#strip_bundle)
to the current process environment.

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/kunda/blob/main/kunda/pythons.py#L54"
target="_blank" style="float:right; font-size:smaller">source</a>

### venv_env

``` python
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`](https://vedicreader.github.io/kunda/pythons.html#venv_env)
puts the selected virtual environment first on `PATH` and sets
`VIRTUAL_ENV`.

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

    ('/repo/.venv', '/repo/.venv/bin:/usr/bin')

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/kunda/blob/main/kunda/pythons.py#L66"
target="_blank" style="float:right; font-size:smaller">source</a>

### nearest_marked

``` python
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`](https://vedicreader.github.io/kunda/pythons.html#nearest_marked)
walks from `start` toward its parents. It returns the first directory
containing one of `markers`.

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

    ('repo', '.git')

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/kunda/blob/main/kunda/pythons.py#L90"
target="_blank" style="float:right; font-size:smaller">source</a>

### nearest_python

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

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

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/kunda/blob/main/kunda/pythons.py#L84"
target="_blank" style="float:right; font-size:smaller">source</a>

### project_python

``` 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.

``` python
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.

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/kunda/blob/main/kunda/pythons.py#L96"
target="_blank" style="float:right; font-size:smaller">source</a>

### python_for

``` python
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`](https://vedicreader.github.io/kunda/pythons.html#python_for)
returns the interpreter used for processes started from a folder. `stop`
limits the parent search.

------------------------------------------------------------------------

<a
href="https://github.com/vedicreader/kunda/blob/main/kunda/pythons.py#L109"
target="_blank" style="float:right; font-size:smaller">source</a>

### find_pythons

``` python
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`](https://vedicreader.github.io/kunda/pythons.html#python_for)
and may be deeper than standard search.Listing enables picker
selection.*

[`find_pythons`](https://vedicreader.github.io/kunda/pythons.html#find_pythons)
returns interpreters for a picker. It removes duplicate paths.

``` python
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']
