Skip to content
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

Friendly PS1 prompt when activating virtual environment #1332

Closed
borice opened this issue Aug 21, 2022 · 2 comments
Closed

Friendly PS1 prompt when activating virtual environment #1332

borice opened this issue Aug 21, 2022 · 2 comments
Labels
⭐ enhancement Improvements for existing features ❓ help wanted Extra attention is needed

Comments

@borice
Copy link

borice commented Aug 21, 2022

Thanks for the great work on PDM - I just discovered it and would like to switch to it from Poetry, but am missing a much-needed feature:

It would be very helpful for situations when working on several Python projects, each with its own virtual environment, to be able to easily tell which virtual environment is currently activated in the various Terminal tabs. The easiest way (and most popular way) is to modify PS1 when a venv is activated to indicate the name of the venv. While this might work for centrally-managed venvs, many of us prefer to use venv.in_project = True. Unfortunately, in this scenario, the source .venv/bin/activate results in a generic (.venv) prefix added to PS1 which isn't very helpful since it's the same across all projects and can't be used to tell them apart.

Poetry has found a way to make this work, for venvs created by Poetry, by modifying the .venv/bin/activate at the time of venv creation to set PS1 in line 66

  63   │ if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
  64   │     _OLD_VIRTUAL_PS1="${PS1-}"
  65   │     if [ "xproject-name-py3.10" != x ] ; then
  66   │         PS1="(project-name-py3.10) ${PS1-}"
  67   │     else
  68   │         PS1="(`basename \"$VIRTUAL_ENV\"`) ${PS1-}"
  69   │     fi
  70   │     export PS1
  71   │ fi

which, for the above, results in a (project-name-py3.10) PS1 prefix. The "project-name-py3.10" PS1 prefix was the result of virtualenvs.prompt = "{project_name}-py{python_version}" setting in Poetry config, where {project_name} is the actual name of the project set in pyproject.toml (and asked for when doing poetry init -- incidentally I think this is a good thing to add to pdm init as well) and {python_version} is the actual python version used in the venv.

To fully explain what Poetry is doing I should mention that they store the actual prompt value in .venv/pyvenv.cfg like so:

...
prompt = project-name-py3.10
@frostming
Copy link
Collaborator

Good to have, care to implement it via a PR?

@frostming
Copy link
Collaborator

Implemented by #1390

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
⭐ enhancement Improvements for existing features ❓ help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants