# libtmux.Session.from_env

- **Module:** libtmux
- **Package:** libtmux
- **Language:** Python
- **Kind:** method
- **Source:** https://github.com/tmux-python/libtmux/blob/38e368c11117fb4aeb2f082d552cd4f210eae06a/src/libtmux/session.py#L188
- **Page:** https://libtmux.org/en/py/stable/reference/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.

## Examples

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

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

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

## Parameters

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

## Returns

:class:`Session`

## Raises

- [`NotInsideTmux`](/en/py/stable/reference/libtmux-exc-notinsidetmux/): When ``$TMUX`` or ``$TMUX_PANE`` is unset or malformed.
- [`TmuxObjectDoesNotExist`](/en/py/stable/reference/libtmux-exc-tmuxobjectdoesnotexist/): When the pane named by ``$TMUX_PANE`` is gone.
- [`LibTmuxException`](/en/py/stable/reference/libtmux-exc-libtmuxexception/): When the server named by ``$TMUX`` is unreachable.
- [`ValueError`](https://docs.python.org/3/library/exceptions.html#ValueError): When the resolved pane carries no ``session_id``. Surfaces a clear
error under ``python -O``, where an ``assert`` would be stripped.

## In other ports

### The session this process is running inside

- TypeScript: [`session.Session.fromEnv`](/en/ts/latest/reference/session-session-fromenv/) (method)
- Rust: [`session.Session.from_env`](/en/rs/latest/reference/session-session-from_env/) (method)
- C#: [`LibTmux.Session.FromEnvironmentAsync`](/en/csharp/latest/reference/libtmux-session-fromenvironmentasync/) (method)
