python-poetry/poetry · error · InvalidCurrentPythonVersionError

Current Python version ({given}) is not allowed by the proje

Error message

Current Python version ({given}) is not allowed by the project ({expected}).

What it means

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.

Source

Thrown at src/poetry/utils/env/env_manager.py:390

        if self._env is not None and not force:
            return self._env

        cwd = self._poetry.file.path.parent
        env = self.get(reload=True)

        if not env.is_sane():
            force = True

        supported_python = self._poetry.package.python_constraint
        create_venv = self._poetry.config.get("virtualenvs.create")

        if (env.is_venv() and not force) or not create_venv:
            # Already inside a virtualenv.
            current_python = Version.parse(
                ".".join(str(c) for c in env.version_info[:3])
            )
            if not supported_python.allows(current_python):
                raise InvalidCurrentPythonVersionError(
                    self._poetry.package.python_versions,
                    str(current_python),
                    note=(
                        'Please change python executable via the "env use" command.'
                        if create_venv
                        else "Poetry cannot switch to a compatible Python version because"
                        " virtualenv creation is disabled."
                    ),
                )
            return env

        in_project_venv = self.use_in_project_venv()
        venv_prompt = self._poetry.config.get("virtualenvs.prompt")

        specific_python_requested = python is not None
        if not python:
            python = Python.get_preferred_python(
                config=self._poetry.config, io=self._io

View on GitHub (pinned to 92b74dcfe3)

Solutions

  1. Deactivate the current venv ('deactivate') and let Poetry create a compatible one.
  2. Switch to a compliant interpreter: 'poetry env use python3.11' (or whichever the constraint allows).
  3. Loosen the python constraint in pyproject.toml if the current version is actually fine.
  4. If virtualenvs.create=false, either re-enable it or install a system python that satisfies the constraint.

Example fix

// before - inside a py3.9 venv, project needs >=3.11
# (poetry install) -> InvalidCurrentPythonVersionError

// after
deactivate
poetry env use python3.11
poetry install
Defensive patterns

Strategy: validation

Validate before calling

from poetry.core.constraints.version import Version

def current_python_allowed(poetry, env) -> bool:
    current = Version.parse(".".join(str(c) for c in env.version_info[:3]))
    return poetry.package.python_constraint.allows(current)

# before triggering create_venv while inside a venv:
if env.is_venv() and not current_python_allowed(poetry, env):
    raise SystemExit("activate a compatible python first or run 'poetry env use <ver>'")

Type guard

from poetry.utils.env.python.exceptions import InvalidCurrentPythonVersionError, PythonVersionError

def is_invalid_current_python(e: Exception) -> bool:
    return isinstance(e, InvalidCurrentPythonVersionError) or (
        isinstance(e, PythonVersionError) and str(e).startswith("Current Python version")
    )

Try / catch

from poetry.utils.env.python.exceptions import InvalidCurrentPythonVersionError

try:
    manager.create_venv()
except InvalidCurrentPythonVersionError as e:
    # cannot switch here - user must deactivate or env use
    raise SystemExit(f"{e}\nHint: deactivate, then 'poetry env use <compatible-python>'")

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of python-poetry/poetry@92b74dcfe3 (2026-08-04). Data as JSON: /data/errors/a083e384f4defd07.json. Report an issue: GitHub.