# libtmux.Server.from_env

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

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

Return the tmux server this process's pane is attached to.

Reads the socket path out of ``$TMUX``, which tmux exports into every
pane it spawns as ``"<socket_path>,<server_pid>,<session_id>"``. Only
the socket path is used: the pid and the session id are frozen at pane
spawn, and the session id goes stale as soon as the pane's window is
moved between sessions. Costs no tmux subprocess -- it is a pure read
of the environment.

## Parameters

- `cls`
- `env` (t.Mapping[str, str] | None) — Environment to read. Defaults to :data:`os.environ`, which is what
a process running inside a pane wants; pass an explicit mapping to
resolve on behalf of another pane.

## Returns

:class:`Server`
    Server bound to the socket named by ``$TMUX``. Not verified to be
    alive -- use :meth:`Server.is_alive` for that.

## Raises

- `NotInsideTmux` — When ``$TMUX`` is unset, empty, or not shaped like tmux's triple.

## Example

```python
>>> from libtmux.server import Server as TmuxServer  # doctest: +HIDE
>>> socket_path = server.cmd(  # doctest: +HIDE
...     "display-message", "-p", "-t", pane.pane_id, "#{socket_path}"
... ).stdout[0]
>>> env = {  # doctest: +HIDE
...     "TMUX": f"{socket_path},1,0",
...     "TMUX_PANE": pane.pane_id,
... }
```

## Example

```python
>>> TmuxServer.from_env(env)
Server(socket_path=...)
```

## Example

```python
>>> TmuxServer.from_env(env).is_alive()
True
```

## Example

Outside a pane there is no server to name:

```python
>>> try:
...     TmuxServer.from_env({})
... except exc.NotInsideTmux as e:
...     print(e)
Not inside a tmux pane: $TMUX is unset or empty
```
