Skip to content

gh-111501: venv: do not export PS1 in activate #105279

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rpigott
Copy link
Contributor

@rpigott rpigott commented Jun 4, 2023

PS1 is a global parameter. It does not need (and should not be) exported to the users environment.

@rpigott rpigott requested a review from vsajip as a code owner June 4, 2023 05:48
@bedevere-bot
Copy link

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

@ghost
Copy link

ghost commented Jun 4, 2023

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-bot
Copy link

Most changes to Python require a NEWS entry.

Please add it using the blurb_it web app or the blurb command-line tool.

@vsajip
Copy link
Member

vsajip commented Jun 4, 2023

Is there some discussion somewhere about this? What problem is being solved here?

@rpigott
Copy link
Contributor Author

rpigott commented Jun 4, 2023

Not recently that I'm aware, but a quick look turns up [1].

Exporting PS1 isn't really relevant to venv's purpose, and it is incorrect in the case that a different subshell is started, since prompt expansion differs between shells. The only sense in exporting something is if you mean for processes to inherit the value, and PS1 is a parameter special to current interactive shell. For example, in zsh my PS1 is currently:

$ print $PS1
%F{cyan}[%~% ]%(?.%F{green}.%F{red})%B%(!.#.$)%b%f 

Obviously, that isn't meaningful to a bash shell. Personally I notice this when starting zsh -f which is similar to bash --norc, since the prompt of the parent shell is unintentionally preserved, even after calling deactivate.

[1] https://unix.stackexchange.com/questions/247585/to-export-or-not-to-export-bash-ps1-variable


TBH, I think there is an even better argument to be made that the PS1 modifications should be dropped altogether. It's strangely invasive and really unnecessary since VIRTUAL_ENV is already set and exported.

An interested user could surely just include VIRTUAL_ENV in PS1 in a way that expands to nothing if it is unset. Or, if they prefer, include it only in zsh's RPROMPT, or include it only in the terminal window title, or include it somewhere in the middle of their normal prompt, add colors, etc.... Not to mention that the current method preculdes modifying the prompt persistently in any way while a venv is active, since PS1 will be completely overwritten again when the venv is deactivated. You can always craft some prompt modifying functions that the user can slap in postactivate if they want the original behavior back, or just place them in postactivate by default if you don't want to be disruptive.

@vsajip
Copy link
Member

vsajip commented Jun 4, 2023

I'm more worried about backward compatibility when considering changes in this area. It's been like this for a long time (and AFAIK virtualenv does this too) and no-one has really brought it up before. I certainly don't want to deal with any support work if the behaviour change affects existing users. Note that activate isn't really needed to use a venv - it's more of a convenience thing.

@romkatv
Copy link

romkatv commented Jun 17, 2024

#111501 explains why exporting PS1 is incorrect.

@rpigott
Copy link
Contributor Author

rpigott commented Jun 17, 2024

Updated to resolve conflicts.

@michaelmaitland
Copy link

Kindly pinging

PS1 is a parameter special to the current interactive shell. It does not
need to be exported to the environment. Exporting PS1 is not useful, and
will break different shells spawned by the current interactive shell.
@rpigott rpigott changed the title venv: do not export PS1 in activate gh111501: venv: do not export PS1 in activate Sep 14, 2024
@StanFromIreland StanFromIreland changed the title gh111501: venv: do not export PS1 in activate gh-111501: venv: do not export PS1 in activate Jul 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants