libtmux Reference MCP Search
On this page

libtmux.Session.from_env

View as Markdown

Module
libtmux
Declared in
Session
Package
libtmux
Source
src/libtmux/session.py
from_env ( cls , env : t.Mapping[str, str] | None = None ) Session
methodclassmethod [source]
methodclassmethod [source]
from_env

Return the session this process's pane belongs to.

The session id baked into $TMUX is frozen at pane spawn and goes stale the moment the pane's window is moved or linked elsewhere, so it is never read. tmux is asked instead: the pane row that Pane.from_env fetches is stamped with the session tmux considers canonical for that pane's window.

Examples

>>> socket_path = server.cmd(  # doctest: +HIDE
...     "display-message", "-p", "-t", pane.pane_id, "#{socket_path}"
... ).stdout[0]

Even a deliberately wrong session id in $TMUX cannot mislead it:

>>> env = {"TMUX": f"{socket_path},1,999", "TMUX_PANE": pane.pane_id}
>>> Session.from_env(env).session_id == session.session_id
True
>>> Session.from_env(env)
Session($1 ...)
Parameters
Returns

Session

Raises
  • NotInsideTmuxWhen $TMUX or $TMUX_PANE is unset or malformed.

  • TmuxObjectDoesNotExistWhen the pane named by $TMUX_PANE is gone.

  • LibTmuxExceptionWhen the server named by $TMUX is unreachable.

  • ValueErrorWhen the resolved pane carries no session_id. Surfaces a clear error under python -O, where an assert would be stripped.

Discussed in Environment

0 declared, 0 inherited

Esc

Type to search.