brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: python -m venv --prompt . shows the project name instead of (.venv)

Python
~/tips/tip-python-venv-prompt

python3 -m venv --prompt . .venv

Shows the project name instead of .venv in your shell prompt when a venv is active.

Call every virtual environment .venv, like most guides tell you to, and every activated shell greets you with the same (.venv) in front of the prompt. With a few terminal tabs open, that label tells me nothing. The --prompt option of venv changes the label without renaming the folder.

cd ~/projects/invoice-bot
python3 -m venv --prompt . .venv
source .venv/bin/activate

The dot is special: it means “use the name of the current folder”. After activating, your prompt starts with (invoice-bot) instead of (.venv). You can also pass any text you like, such as --prompt api-dev. Without the option, venv uses the name of the environment’s own folder, which is exactly why you see .venv everywhere. On Windows you activate with .venv\Scripts\Activate.ps1 and get the same label in PowerShell.

venv also stores the name in pyvenv.cfg as prompt = 'invoice-bot', and the activate script exports it as VIRTUAL_ENV_PROMPT. That’s handy if you use a custom shell prompt that draws the venv name itself.

The caveat: the label is baked in when the environment is created. venv fills it into the activation scripts at that moment, so --prompt . uses the folder name of that day. Rename the project folder later and the old name stays. To change it on an existing environment, I’d save the packages with pip freeze > requirements.txt, delete .venv, create it again with the new prompt and reinstall. With a requirements file that takes a minute, and it’s cleaner than editing the generated scripts by hand.

Further reading: venv, creation of virtual environments (Python docs)

More Python tips import asyncio from enum import StrEnum, auto except* ValueError as eg: import pytest pytest --maxfail=2 All Python tips (107) All topics

Link