Prerelease This site documents an alpha of libtmux. Its structure, URLs and APIs are subject to change.

Python API

Python documentation

libtmux.Session.from_env

Python

View as Markdown

Module
libtmux
Declared in
Session
Package
libtmux
Source
src/libtmux/session.py
Other languages
Ruby Lua TypeScriptRust Go Java .NET C++ Swift
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.

Parameters
Returns

Session

Raises
  • NotInsideTmux – When $TMUX or $TMUX_PANE is unset or malformed.

  • TmuxObjectDoesNotExist – When the pane named by $TMUX_PANE is gone.

  • LibTmuxException – When the server named by $TMUX is unreachable.

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

Discussed in Environment

In other ports The session this process is running inside

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 ...)
const current = await Session.fromEnv();
current.name;
use libtmux::{Pane, Session};
let session = server.new_session("locating-session").await?;
let pane = session.panes().await?.remove(0);
// Standing in for the environment tmux gives a process it starts.
let found = Session::from_env_value(server, Some(pane.id().as_ref())).await?;
assert_eq!(found.expect("the session exists").id(), session.id());

0 declared, 0 inherited

Esc

Type to search.