I recently started using Poetry for Python package management, and one small annoyance showed up immediately: the shell prompt inside the virtual environment is unnecessarily long.
For example, if the project is named test-poetry, after running poetry shell the prompt may look like this:
(test-poetry-FvrREBVp-py3.6) knktc@knktc-rmbp test_poetry %
Poetry includes both an encoded suffix and the Python version in the virtual environment name, and then uses that full directory name as the prompt prefix. On a small screen, that takes up a surprising amount of space.
Since Poetry environments are still activated through bin/activate, the easiest fix is to find that file and adjust the prompt manually.
From the project directory, as long as pyproject.toml is present, run:
1 | poetry env info -p |
This prints the path to the current virtual environment, for example:
1 | (test-poetry-FvrREBVp-py3.6) knktc@knktc-rmbp test_poetry % poetry env info -p |
Then open the bin/activate file in that environment:
1 | vim /Users/knktc/Library/Caches/pypoetry/virtualenvs/test-poetry-FvrREBVp-py3.6/bin/activate |
Around line 68, you should see the logic that sets PS1. It uses the environment directory name as the prompt prefix:
1 | if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then |
You can replace that line with a shorter, fixed project name:
1 | if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then |
Save the file, then reactivate the environment:
1 | knktc@knktc-rmbp test_poetry % poetry shell |
Much better.