# libtmux.Session.from_env

- **Module:** libtmux.Session
- **Package:** libtmux
- **Language:** Python
- **Kind:** method
- **Source:** https://github.com/tmux-python/libtmux/blob/036c521c4b83ce6e434eb50afe2a0d08a6a05e46/src/libtmux/session.py#L203
- **Page:** https://libtmux.org/reference/py/libtmux-session-from_env/

```
libtmux.Session.from_env(cls, env: t.Mapping[str, str] | None = None) -> Session
```

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
:meth:`Pane.from_env` fetches is stamped with the session tmux
considers canonical for that pane's window.

## Parameters

- `cls`
- `env` (t.Mapping[str, str] | None) — Environment to read. Defaults to :data:`os.environ`.

## Returns

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

## Example

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

## Example

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

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

## Example

```python
>>> Session.from_env(env)
Session($1 ...)
```
