{"record":{"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/f399c3718970b1b0e2478dac5296eb62679a9b86/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 only supports in-place addition of these two types; anything else is a programming error.","triggerScenarios":"Writing env += some_list or env += some_string where the operand is not a Distribution or Environment instance.","commonSituations":"Accidentally passing a raw list of paths or requirement strings to an Environment via +=; refactoring that changes the operand type.","solutions":["Wrap the item(s) in the correct type: add Distribution objects individually, or build an Environment first.","If adding multiple distributions, loop and call env.add(dist) instead of env += list."],"exampleFix":"// before\nenv += [dist1, dist2]  # list, not allowed\n// after\nfor d in [dist1, dist2]:\n    env.add(d)","handlingStrategy":"type-guard","validationCode":"if not isinstance(other, (Distribution, Environment)):\n    raise TypeError(f'Cannot add {type(other)} to Environment')\nenv += other","typeGuard":"import pkg_resources\ndef is_addable_to_env(o) -> bool:\n    return isinstance(o, (pkg_resources.Distribution, pkg_resources.Environment))","tryCatchPattern":null,"preventionTips":["Only += Distribution or Environment objects.","Loop and call env.add() for collections of distributions."],"tags":["pkg-resources","type-error","environment"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}