You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
PS1 is exported when a virtual environment is activated #111501
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.
I already tried removing this in #105279. I still hope cpython will reconsider, but I have since chosen to make my own venv script. There's absolutely nothing to a venv beyond an entry to PATH, so it's really not hard to do better than upstream.
If anyone needs a reference, I use [1] which mimics "workon" from virtualenv_wrapper, and replicate the prompt like [2]
Activity
rpigott commentedon Dec 17, 2023
I already tried removing this in #105279. I still hope cpython will reconsider, but I have since chosen to make my own venv script. There's absolutely nothing to a venv beyond an entry to PATH, so it's really not hard to do better than upstream.
If anyone needs a reference, I use [1] which mimics "workon" from virtualenv_wrapper, and replicate the prompt like [2]
[1] https://git.sr.ht/~brocellous/dotfiles/tree/ad05ed7e79953cb0217d335ff14ba32d4d4094e5/item/.local/share/zsh/pyenv/workon
[2] https://git.sr.ht/~brocellous/dotfiles/tree/ad05ed7e79953cb0217d335ff14ba32d4d4094e5/item/.hostconfig/rdtw/zshrc#L4
pythongh-111501: Do not export PS1 in venv activate
pythongh-111501: Do not export PS1 in venv activate
gh-111501: venv: do not export PS1 in activate (#105279)
pythongh-111501: venv: do not export PS1 in activate (python#105279)