# libtmux.Window.from_env

- **Module:** libtmux
- **Package:** libtmux
- **Language:** Python
- **Kind:** method
- **Source:** https://github.com/tmux-python/libtmux/blob/38e368c11117fb4aeb2f082d552cd4f210eae06a/src/libtmux/window.py#L226
- **Page:** https://libtmux.org/en/py/stable/reference/libtmux-window-from_env/

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

Return the window containing the pane this process runs inside of.

Resolves through :meth:`Pane.from_env`, so the answer is the window
that *contains* the caller -- not the session's currently active
window, which may be a different one entirely.

## Examples

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

```python
>>> Window.from_env(env)
Window(@1 1:..., Session($1 ...))
```

The caller's window is reported even while another window is active:

```python
>>> _ = session.new_window(window_name="foreground", attach=True)
>>> Window.from_env(env).window_id == window.window_id
True
```

## Parameters

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

## Returns

:class:`Window`

## 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 ``window_id``. Surfaces a clear
error under ``python -O``, where an ``assert`` would be stripped.
