{"id":"a083e384f4defd07","repo":"python-poetry/poetry","slug":"current-python-version-given-is-not-allowed-by","errorCode":null,"errorMessage":"Current Python version ({given}) is not allowed by the project ({expected}).","messagePattern":"Current Python version \\((.+?)\\) is not allowed by the project \\((.+?)\\)\\.","errorType":"validation","errorClass":"InvalidCurrentPythonVersionError","httpStatus":null,"severity":"error","filePath":"src/poetry/utils/env/env_manager.py","lineNumber":390,"sourceCode":"        if self._env is not None and not force:\n            return self._env\n\n        cwd = self._poetry.file.path.parent\n        env = self.get(reload=True)\n\n        if not env.is_sane():\n            force = True\n\n        supported_python = self._poetry.package.python_constraint\n        create_venv = self._poetry.config.get(\"virtualenvs.create\")\n\n        if (env.is_venv() and not force) or not create_venv:\n            # Already inside a virtualenv.\n            current_python = Version.parse(\n                \".\".join(str(c) for c in env.version_info[:3])\n            )\n            if not supported_python.allows(current_python):\n                raise InvalidCurrentPythonVersionError(\n                    self._poetry.package.python_versions,\n                    str(current_python),\n                    note=(\n                        'Please change python executable via the \"env use\" command.'\n                        if create_venv\n                        else \"Poetry cannot switch to a compatible Python version because\"\n                        \" virtualenv creation is disabled.\"\n                    ),\n                )\n            return env\n\n        in_project_venv = self.use_in_project_venv()\n        venv_prompt = self._poetry.config.get(\"virtualenvs.prompt\")\n\n        specific_python_requested = python is not None\n        if not python:\n            python = Python.get_preferred_python(\n                config=self._poetry.config, io=self._io","sourceCodeStart":372,"sourceCodeEnd":408,"githubUrl":"https://github.com/python-poetry/poetry/blob/92b74dcfe348d0e01e14d40d6c1fa47a4ee04a54/src/poetry/utils/env/env_manager.py#L372-L408","documentation":"Raised as InvalidCurrentPythonVersionError in EnvManager.create_venv when Poetry is already inside a virtualenv (or virtualenv creation is disabled via virtualenvs.create=false) and the currently active Python version is not allowed by the project's python constraint in pyproject.toml. Because Poetry cannot transparently switch interpreters in this state, it aborts.","triggerScenarios":"Running 'poetry install' (or any operation triggering create_venv) while already inside an activated venv whose Python version violates [tool.poetry.dependencies] python = ...; OR with virtualenvs.create=false and the system python is incompatible. Branch: env_manager.py:384-399.","commonSituations":"User activated a py3.10 venv then ran poetry install on a project requiring python >=3.11; CI image's default python is too old; virtualenvs.create=false set globally and the system python doesn't match; downstream of a conda activate.","solutions":["Deactivate the current venv ('deactivate') and let Poetry create a compatible one.","Switch to a compliant interpreter: 'poetry env use python3.11' (or whichever the constraint allows).","Loosen the python constraint in pyproject.toml if the current version is actually fine.","If virtualenvs.create=false, either re-enable it or install a system python that satisfies the constraint."],"exampleFix":"// before - inside a py3.9 venv, project needs >=3.11\n# (poetry install) -> InvalidCurrentPythonVersionError\n\n// after\ndeactivate\npoetry env use python3.11\npoetry install","handlingStrategy":"validation","validationCode":"from poetry.core.constraints.version import Version\n\ndef current_python_allowed(poetry, env) -> bool:\n    current = Version.parse(\".\".join(str(c) for c in env.version_info[:3]))\n    return poetry.package.python_constraint.allows(current)\n\n# before triggering create_venv while inside a venv:\nif env.is_venv() and not current_python_allowed(poetry, env):\n    raise SystemExit(\"activate a compatible python first or run 'poetry env use <ver>'\")","typeGuard":"from poetry.utils.env.python.exceptions import InvalidCurrentPythonVersionError, PythonVersionError\n\ndef is_invalid_current_python(e: Exception) -> bool:\n    return isinstance(e, InvalidCurrentPythonVersionError) or (\n        isinstance(e, PythonVersionError) and str(e).startswith(\"Current Python version\")\n    )","tryCatchPattern":"from poetry.utils.env.python.exceptions import InvalidCurrentPythonVersionError\n\ntry:\n    manager.create_venv()\nexcept InvalidCurrentPythonVersionError as e:\n    # cannot switch here - user must deactivate or env use\n    raise SystemExit(f\"{e}\\nHint: deactivate, then 'poetry env use <compatible-python>'\")","preventionTips":["Keep [tool.poetry.dependencies] python constraint aligned with your active interpreter.","Deactivate venvs before 'poetry install' to let Poetry pick a compliant interpreter.","If setting virtualenvs.create=false, ensure the system python satisfies the project constraint.","In CI, provision an image whose default python matches the project's min version."],"tags":["python-constraint","create-venv","activation","pyproject"],"analyzedSha":"92b74dcfe348d0e01e14d40d6c1fa47a4ee04a54","analyzedAt":"2026-08-04T20:33:34.072Z","schemaVersion":2}