python-poetry/poetry · error · RuntimeError

Package cannot be installed in the current environment

Error message

Package {link.url} cannot be installed in the current environment {self._env.marker_env}

What it means

Raised by Executor._download_link_archive at src/poetry/installation/executor.py:759-767 when get_cached_archive_for_link(link, strict=False, env=...) returns None. With strict=False the cache is allowed to fall back to the original link, so a None result means the original archive itself is not valid for the current environment (no compatible candidate). RuntimeError naming the URL and the marker env.

Solutions

  1. Verify and switch to the interpreter the lock was resolved with: `poetry env info`, `poetry env use <python>`.
  2. Regenerate the lock for the current env: `poetry lock --regenerate` then install.
  3. Ensure an sdist is available so Poetry can build for the platform, or add a compatible source.
  4. Check markers in pyproject.toml (python = "...") match the active interpreter.

Example fix

# before: lock made under py3.11, now in py3.9
$ poetry install
RuntimeError: Package ... cannot be installed in the current environment ...

# after
$ poetry env use python3.11
$ poetry install
Defensive patterns

Strategy: validation

Validate before calling

from poetry.utils.env import EnvManager

env = EnvManager.get_default_env()
for link in links:
    if link.is_wheel:
        from poetry.installation.wheel import Wheel
        if not Wheel(link.filename).is_supported_by_environment(env):
            continue
    # ensure at least one link is env-compatible before install
    break
else:
    raise ValueError('No artifact compatible with the current env')

Try / catch

try:
    executor.run(operations)
except RuntimeError as e:
    if 'cannot be installed in the current environment' in str(e):
        raise SystemExit('Use `poetry env use` to switch to the locked interpreter.') from e
    raise

Prevention

When it happens

Trigger: Installing a package whose only artifacts (cached or freshly downloaded) are incompatible with the current env's markers — e.g. a wheel only built for a different Python/OS, or an sdist that needs a build the env cannot satisfy. Occurs during executor.run when materializing the archive.

Common situations: Locked dependency resolved for a different interpreter than the one currently active; a wheel-only package on an unsupported platform; env markers (python_version, sys_platform) excluding every artifact.

Related errors


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

Appendix: source

Thrown at src/poetry/installation/executor.py:764

        # Get original package for the link provided
        download_func = functools.partial(self._download_archive, operation)
        original_archive = self._artifact_cache.get_cached_archive_for_link(
            link, strict=True, download_func=download_func
        )

        # Get potential higher prioritized cached archive, otherwise it will fall back
        # to the original archive.
        archive = self._artifact_cache.get_cached_archive_for_link(
            link,
            strict=False,
            env=self._env,
        )
        if archive is None:
            # Since we previously downloaded an archive, we now should have
            # something cached that we can use here. The only case in which
            # archive is None is if the original archive is not valid for the
            # current environment.
            raise RuntimeError(
                f"Package {link.url} cannot be installed in the current environment"
                f" {self._env.marker_env}"
            )

        if archive.suffix != ".whl":
            message = (
                f"  <fg=blue;options=bold>-</> {self.get_operation_message(operation)}:"
                f"{format_build_wheel_log(package, self._env)}"
            )
            self._write(operation, message)

            name = operation.package.name
            archive = self._chef.prepare(
                archive,
                output_dir=original_archive.parent,
                config_settings=self._build_config_settings.get(name),
                build_constraints=self._build_constraints.get(name),
            )

View on GitHub (pinned to 92b74dcfe3)