{"id":"a508e58fbe87642c","repo":"pypa/pip","slug":"can-t-add-r-to-environment","errorCode":null,"errorMessage":"Can't add %r to environment","messagePattern":"Can't add %r to environment","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"src/pip/_vendor/pkg_resources/__init__.py","lineNumber":1308,"sourceCode":"        to the `installer` argument.\"\"\"\n        return installer(requirement) if installer else None\n\n    def __iter__(self) -> Iterator[str]:\n        \"\"\"Yield the unique project names of the available distributions\"\"\"\n        for key in self._distmap.keys():\n            if self[key]:\n                yield key\n\n    def __iadd__(self, other: Distribution | Environment):\n        \"\"\"In-place addition of a distribution or environment\"\"\"\n        if isinstance(other, Distribution):\n            self.add(other)\n        elif isinstance(other, Environment):\n            for project in other:\n                for dist in other[project]:\n                    self.add(dist)\n        else:\n            raise TypeError(\"Can't add %r to environment\" % (other,))\n        return self\n\n    def __add__(self, other: Distribution | Environment):\n        \"\"\"Add an environment or distribution to an environment\"\"\"\n        new = self.__class__([], platform=None, python=None)\n        for env in self, other:\n            new += env\n        return new\n\n\n# XXX backward compatibility\nAvailableDistributions = Environment\n\n\nclass ExtractionError(RuntimeError):\n    \"\"\"An error occurred extracting a resource\n\n    The following attributes are available from instances of this exception:","sourceCodeStart":1290,"sourceCodeEnd":1326,"githubUrl":"https://github.com/pypa/pip/blob/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_vendor/pkg_resources/__init__.py#L1290-L1326","documentation":"Raised by Environment.__iadd__ (the += operator) when the right-hand operand is neither a Distribution nor an Environment. Environment is a dict-like collection of project_name -> list[Distribution]; it only knows how to merge in another whole environment or a single distribution. Any other type (str, Requirement, list, tuple) is rejected.","triggerScenarios":"Writing `env += obj` or calling `env.__iadd__(obj)` where obj is a Requirement, a project-name string, a list of distributions, or any non-Distribution/non-Environment object.","commonSituations":"Treating Environment like a flat list and trying to `+= [dist1, dist2]` or `+= some_requirement`. Also confusing it with working_set.add(), which does accept a Distribution (but not arbitrary objects either).","solutions":["If adding a single distribution, use `env.add(dist)` instead of `env += dist` (only `+=` is type-restricted; add() takes a Distribution).","If adding multiple, loop: `for d in dists: env.add(d)` or merge a second Environment built from them.","Pass an Environment instance on the right side of += (e.g. `env1 += env2`)."],"exampleFix":"// before\nenv += [dist_a, dist_b]   # TypeError: Can't add [...] to environment\n\n// after\nfor d in (dist_a, dist_b):\n    env.add(d)","handlingStrategy":"type-guard","validationCode":"from pkg_resources import Environment, Distribution\n\ndef safe_iadd(env, other):\n    if isinstance(other, Distribution):\n        env.add(other)\n    elif isinstance(other, Environment):\n        env += other\n    else:\n        raise TypeError('Can only add Distribution or Environment, got %r' % type(other))\n    return env","typeGuard":"from pkg_resources import Environment, Distribution\n\ndef is_env_addable(x):\n    return isinstance(x, (Distribution, Environment))","tryCatchPattern":"try:\n    env += other\nexcept TypeError as e:\n    if 'Can\\'t add' in str(e):\n        # coerce or report\n        raise\n    raise","preventionTips":["Use env.add(dist) for single distributions instead of +=.","Build a second Environment and merge with += for bulk adds.","Never pass Requirements, strings, or lists to Environment via +=."],"tags":["pkg-resources","environment","type-error","operator-overloading"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}