{"id":"03ba3ecfbde5820f","repo":"python-poetry/poetry","slug":"invalid-template-string-in-virtualenvs-prompt-se","errorCode":null,"errorMessage":"Invalid template string in 'virtualenvs.prompt' setting: {e}","messagePattern":"Invalid template string in 'virtualenvs\\.prompt' setting: (.+?)","errorType":"console","errorClass":"PoetryConsoleError","httpStatus":null,"severity":"error","filePath":"src/poetry/utils/env/env_manager.py","lineNumber":459,"sourceCode":"            venv = venv_path\n        else:\n            name = self.generate_env_name(name, str(cwd))\n            name = f\"{name}-py{python.minor_version.to_string()}\"\n            venv = venv_path / name\n\n        if venv_prompt is not None:\n            try:\n                venv_prompt = venv_prompt.format(\n                    project_name=self._poetry.package.name or \"virtualenv\",\n                    python_version=python.minor_version.to_string(),\n                )\n            except KeyError as e:\n                raise PoetryConsoleError(\n                    f\"Invalid template variable '{e.args[0]}' in 'virtualenvs.prompt' setting.\\n\"\n                    f\"Valid variables are: {{project_name}}, {{python_version}}\"\n                ) from e\n            except ValueError as e:\n                raise PoetryConsoleError(\n                    f\"Invalid template string in 'virtualenvs.prompt' setting: {e}\"\n                ) from e\n\n        if not venv.is_dir():\n            if create_venv is False:\n                self._io.write_error_line(\n                    \"<fg=black;bg=yellow>\"\n                    \"Skipping virtualenv creation, \"\n                    \"as specified in config file.\"\n                    \"</>\"\n                )\n\n                return self.get_system_env()\n\n            if venv.is_file():\n                self._io.write_error_line(\n                    f\"<warning>{venv} is not a virtual environment but a file. Removing it.</warning>\"\n                )","sourceCodeStart":441,"sourceCodeEnd":477,"githubUrl":"https://github.com/python-poetry/poetry/blob/92b74dcfe348d0e01e14d40d6c1fa47a4ee04a54/src/poetry/utils/env/env_manager.py#L441-L477","documentation":"Raised as PoetryConsoleError in EnvManager.create_venv when formatting the virtualenvs.prompt config value raises a ValueError - i.e. the template string is structurally malformed for str.format, most commonly unbalanced curly braces.","triggerScenarios":"Configuring 'virtualenvs.prompt' with a string containing an odd number of braces, e.g. \"{project_name\" (missing close) or a lone '}' - anything str.format rejects with ValueError. Caught at env_manager.py:458-461.","commonSituations":"User wants a literal brace in the prompt but forgets to escape it as {{ or }}; truncation/copy-paste drops a brace; a config file edit leaves an unterminated placeholder.","solutions":["Balance the braces or escape literal ones: use {{ for a literal '{' and }} for '}'.","Reconfigure with a well-formed template: 'poetry config virtualenvs.prompt \"({project_name})\"'.","Reset to default: 'poetry config --unset virtualenvs.prompt'.","Validate the string with Python first: '\"...\".format(project_name=\"x\", python_version=\"3.11\")'."],"exampleFix":"// before\npoetry config virtualenvs.prompt \"{project_name\"\n# ValueError: unmatched brace -> PoetryConsoleError\n\n// after\npoetry config virtualenvs.prompt \"{project_name}-py{python_version}\"","handlingStrategy":"validation","validationCode":"def prompt_template_parses(tpl: str) -> bool:\n    try:\n        tpl.format(project_name=\"x\", python_version=\"3.11\")\n        return True\n    except (KeyError, ValueError):\n        return False\n\n# before setting config:\nassert prompt_template_parses(new_prompt), \"malformed template string\"","typeGuard":"from poetry.console.exceptions import PoetryConsoleError\n\ndef is_invalid_prompt_string(e: Exception) -> bool:\n    return isinstance(e, PoetryConsoleError) and \"Invalid template string\" in str(e)","tryCatchPattern":"from poetry.console.exceptions import PoetryConsoleError\n\ntry:\n    manager.create_venv()\nexcept PoetryConsoleError as e:\n    if \"Invalid template string\" in str(e):\n        run_cli([\"poetry\", \"config\", \"--unset\", \"virtualenvs.prompt\"])\n        manager.create_venv()\n    else:\n        raise","preventionTips":["Balance all braces in the prompt template; escape literals as {{ }}.","Test the template with Python's str.format before configuring it.","Keep the template simple: '{project_name}-py{python_version}' covers most needs.","Reset with 'poetry config --unset virtualenvs.prompt' if unsure."],"tags":["config","virtualenvs-prompt","template","formatting","syntax"],"analyzedSha":"92b74dcfe348d0e01e14d40d6c1fa47a4ee04a54","analyzedAt":"2026-08-04T20:33:34.072Z","schemaVersion":2}