Skip to content

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

Merged
FFY00 merged 1 commit into
python:mainfrom
rpigott:noexport
Jun 22, 2026
Merged

gh-111501: venv: do not export PS1 in activate#105279
FFY00 merged 1 commit into
python:mainfrom
rpigott:noexport

Conversation

@rpigott

@rpigott rpigott commented Jun 4, 2023

Copy link
Copy Markdown
Contributor

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
Copy Markdown

Most changes to Python require a NEWS entry.

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

@ghost

ghost commented Jun 4, 2023

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.
CLA signed

@bedevere-bot

Copy link
Copy Markdown

Most changes to Python require a NEWS entry.

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

@vsajip

vsajip commented Jun 4, 2023

Copy link
Copy Markdown
Member

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

@rpigott

rpigott commented Jun 4, 2023

Copy link
Copy Markdown
Contributor Author

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

vsajip commented Jun 4, 2023

Copy link
Copy Markdown
Member

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

romkatv commented Jun 17, 2024

Copy link
Copy Markdown

#111501 explains why exporting PS1 is incorrect.

@rpigott

rpigott commented Jun 17, 2024

Copy link
Copy Markdown
Contributor Author

Updated to resolve conflicts.

@michaelmaitland

Copy link
Copy Markdown

Kindly pinging

@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
@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Apr 13, 2026
@gaborbernat

Copy link
Copy Markdown
Contributor

You're right on the facts: it's been there since the original PEP 405 activate (2012), virtualenv mirrors it, and this is the first report. None of those bear on correctness — romkatv's issue body is the why, with the typeset -p PS1 repro (declare -x appears after activate) and the broken bash prompt under zsh -f.

On compatibility: removing the export changes exactly one observable thing — PS1 no longer enters the child environment. The shell that sourced activate is unaffected (assignment alone drives its prompt), and deactivate restores as before. The only thing a child loses is a prompt that was already wrong for it and lingered past deactivate, so there's nothing dependable to preserve.

On "virtualenv does this too": agreed, and it's the same bug there — I've opened pypa/virtualenv#3158 to remove it on that side in parallel so the two stay aligned. For the record, bash is the only CPython activator that exports its prompt variable (activate.csh uses set prompt, fish/ps1 use functions).

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 requested a review from FFY00 as a code owner June 5, 2026 17:57
@github-actions github-actions Bot removed the stale Stale PR or inactive for long period of time. label Jun 11, 2026

@FFY00 FFY00 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the rationale described.

@FFY00 FFY00 merged commit 5049eb1 into python:main Jun 22, 2026
56 checks passed
@rpigott rpigott deleted the noexport branch June 22, 2026 16:22
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.

7 participants