Activation
Activation puts the environment’s binary directory at the front of PATH
and records the environment in VIRTUAL_ENV, so python and pip resolve
to the environment without any absolute paths. tn-venv generates scripts
for seven activation mechanisms in one pass.
Generated scripts
File |
Shell |
Activation command |
|---|---|---|
|
bash, zsh, and other POSIX sh |
|
|
cmd.exe |
|
|
cmd.exe |
|
|
PowerShell / pwsh |
|
|
fish |
|
|
csh, tcsh |
|
|
Nushell |
|
|
no shell — in-process |
see below |
On Windows the environment directory is Scripts; elsewhere it is bin.
activate.bat and deactivate.bat are only generated on Windows; every
other script is generated on every platform.
All scripts share the same semantics:
VIRTUAL_ENVis set to the environment directory and exported.The environment’s binary directory is prepended to
PATH; the previous value is stashed sodeactivatecan restore it exactly.PYTHONHOMEis unset if set (and restored bydeactivate).Unless
VIRTUAL_ENV_DISABLE_PROMPTis non-empty, the shell prompt is prefixed with(<prompt>)andVIRTUAL_ENV_PROMPTis set.deactivate(a shell function, ordeactivate.baton cmd) reverses all of the above, including restoring the previous prompt.
Nested activation is safe: activating environment B while A is active
stacks the changes, and deactivate returns to A’s state.
Choosing which scripts to generate
$ tn-venv .venv --activators powershell,batch
$ tn-venv .venv --activators all
$ tn-venv .venv --activators default,-nushell,-csh
The value is comma-separated and repeatable. Accepted names are bash,
batch, powershell, fish, csh, nushell, python, plus the two
pseudo-names all and default. A leading - removes a name from the
default set. Unknown names are rejected with exit code 2 and the list of
valid names.
The prompt
By default the prompt is the environment’s folder name. Override it with:
$ tn-venv .venv --prompt "api-dev"
A custom prompt is also recorded in pyvenv.cfg (see
pyvenv.cfg reference), which is how python -m venv --upgrade
learns to keep it. To suppress prompt modification at activation time,
without changing the scripts, set VIRTUAL_ENV_DISABLE_PROMPT to any
non-empty value before sourcing.
activate_this.py
activate_this.py activates an environment inside an already-running
Python process — the classic virtualenv mechanism, kept for
compatibility with embedding tools and long-lived application servers:
activate_this = "/path/to/.venv/bin/activate_this.py"
with open(activate_this) as f:
code = compile(f.read(), activate_this, "exec")
exec(code, {"__file__": activate_this})
It prepends the environment’s binary directory to PATH, sets
VIRTUAL_ENV and VIRTUAL_ENV_PROMPT, adds the environment’s
site-packages via site.addsitedir(), and rewrites sys.prefix (keeping
the old value in sys.real_prefix). It cannot un-import already-loaded
modules, so it is only safe to use early in a process’s lifetime.
Warning
On POSIX systems the POSIX-shell script must be sourced, not executed:
source .venv/bin/activate. Executing it in a subshell activates the
subshell only and is silently useless — the scripts detect nothing; this is
inherent to how shells work, not a tn-venv limitation.