visual-studio-codepython-venvps1python-3.13

Vscode PS1 missing parenthesis around venv


I have the opposite problem of this question:

Why are there double parentheses around my Python virtual environment in Visual Studio Code?

I am getting a prompt that shows NO parenthesis in my PS1 prompt.

I'm using the latest python extension v2024.14.0, and have also tried the pre-release.

I am getting this problem, consistently, on both MacOS (work) and Ubuntu (personal), with completely different setups and projects.

Here is my most simple setup, to reduce any other variables:

What I have tried

Step 1 - Simplify PS1

To reduce the chances of something strange happening, I have globally set my PS1 to the following:

PS1='\u:\w\$ '

Step 2 - Brand new vscode workspace

I ran rm -rf ~/.config/Code, completely destroying all local config and workspace settings.

I opened up vscode, completely clean, with no workspace/project.

I immediately clicked file > save workspace as, and saved it as: ~/test-bad-venv.code-workspace.

Then, I made sure that I had ABSOLUTELY NO EXTENSIONS ENABLED, except for the latest python extension: Python v2024.14.0 (by Microsoft)

I restarted vscode, and clicked "yes" to the "I trust the authors" dialog.

As proof, the ~/test-bad-venv.code-workspace file has the following contents:

{
    "folders": []
}

Step 3 - Create a venv

I create a new dir, ~/tmp, and add it to the project. I create a completely empty file called test.py, and open it up in vscode.

I open up the vscode console, for the first time, and run some simple info commands.

addison:~$ which python3.13
/usr/bin/python3.13
addison:~$ python3.13 --version
Python 3.13.0rc1
addison:~/tmp$ uname -a
Linux addison-ubuntu 5.15.0-119-generic #129-Ubuntu SMP Fri Aug 2 19:25:20 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
addison:~/tmp$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.4 LTS
Release:        22.04
Codename:       jammy

I run the following line, to create a venv:

python3.13 -m venv venv

And then select the venv interpreter, with ctrl+shift+p:

Python 3.13.0rc1 ('venv': venv) ./venv/bin/python

This immediately displays terribly when I open a new terminal in vscode (see how it says: venvaddison):

venvaddison:~/tmp$ which python3
/home/addison/tmp/venv/bin/python3

venvaddison:~/tmp$ python3 --version
Python 3.13.0rc1

venvaddison:~/tmp$ echo $PS1
\[\]venv\u:\w\$ \[\]

But it works fine in a normal terminal, anywhere else on the computer:

addison:~$ cd tmp/
addison:~/tmp$ source venv/bin/activate
(venv) addison:~/tmp$ 

Conclusion

This seems to only happen when I'm using python3.13, which I have installed with deadsnakes. (Edit - it happens randomly with python3.9 on Mac, and 3.10 for another guy online). Probably not version related?

There's no reason this should be a python problem, since running the source command manually myself always works fine - even in the vscode terminal.

Does anyone have a solution to this?

Issue #24097 on Github


Solution

  • Proper fix

    Add the following to your settings JSON in vscode:

    "python.experiments.optOutFrom": ["pythonTerminalEnvVarActivation"],
    

    And reload the application.

    Bad Hack

    I can see that in python3.13, within venv/bin/activate, it runs:

    VIRTUAL_ENV_PROMPT="venv"
    export VIRTUAL_ENV_PROMPT
    

    But in python3.10, it has:

    VIRTUAL_ENV_PROMPT="(venv) "
    export VIRTUAL_ENV_PROMPT
    

    You can fix this by editing venv/bin/activate to contain:

    # This is on about like 56, as of python3.13.
    # You can change this prompt to be whatever you want
    
    VIRTUAL_ENV_PROMPT="(venv) "
    

    And then do a hard reload of vscode. This will actually break the prompt outside of Vscode, as it will have ((venv)), but that's better than it missing completely.


    See my post on https://github.com/microsoft/vscode-python/pull/23201#issuecomment-2336543889