Skip to content

PS1 is exported when a virtual environment is activated #111501

Open

Description

@romkatv

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

Activity

  1. rpigott commented on Dec 17, 2023

    @rpigott
    Contributor

    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

  2. added a commit that references this issue on Sep 14, 2024
  3. added
    stdlibStandard Library Python modules in the Lib/ directory
    topic-venvRelated to the venv module
    on Jul 29, 2025
  4. added a commit that references this issue on Jun 5, 2026
  5. added a commit that references this issue on Jun 22, 2026
  6. added a commit that references this issue on Jul 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-venvRelated to the venv moduletype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions