romkatv · GitHub

Bug report

Bug description:

  • Expected: When a virtual environment is (de)activated, PS1 is not exported.
  • Actual: When a virtual environment is (de)activated, PS1 is exported.

PS1 is an internal shell parameter. Exporting it causes all child processes to inherit it. This becomes a problem if a child process happens to be a shell of another kind (e.g., the parent is bash and the child is zsh), which recognizes PS1 as a special parameter but with incompatible syntax. In general, internal shell parameters should not be exported.

# Create a virtual environment.
bash-5.0$ python3 -m venv .env
# Verify that PS1 is not exported.
bash-5.0$ typeset -p PS1
declare -- PS1="\\s-\\v\\\$ "
# Activate the virtual environment.
bash-5.0$ source .env/bin/activate
# PS1 is now exported. This is unexpected.
(.env) bash-5.0$ typeset -p PS1
declare -x PS1="(.env) \\s-\\v\\\$ "
# Start zsh.
(.env) bash-5.0$ zsh -f
# Observe that prompt is broken because it's from bash.
(.env) \s-\v\$

As far as I can tell, removing the two export PS1 lines from .env/bin/activate is enough to fix this issue.

CPython versions tested on:

3.8

Operating systems tested on:

Linux

Linked PRs

Read the original on github.com ↗