{"record":{"id":"3097d37fe6396132","repo":"pypa/pip","slug":"can-not-combine-user-and-target","errorCode":null,"errorMessage":"Can not combine '--user' and '--target'","messagePattern":"Can not combine '--user' and '--target'","errorType":"exception","errorClass":"CommandError","httpStatus":null,"severity":"error","filePath":"src/pip/_internal/commands/install.py","lineNumber":374,"sourceCode":"                \"to avoid mixing pip logging output with JSON output.\"\n            ),\n        )\n\n    @contextlib.contextmanager\n    def pip_version_check(self, options: Values, args: list[str]) -> Iterator[None]:\n        # Skip the self-version check when pip itself is a requirement. The\n        # running pip may be replaced mid-command, and the upgrade prompt\n        # is redundant.\n        if any(_arg_refers_to_pip(arg) for arg in args):\n            yield\n            return\n        with super().pip_version_check(options, args):\n            yield\n\n    @with_cleanup\n    def run(self, options: Values, args: list[str]) -> int:\n        if options.use_user_site and options.target_dir is not None:\n            raise CommandError(\"Can not combine '--user' and '--target'\")\n\n        # Check whether the environment we're installing into is externally\n        # managed, as specified in PEP 668. Specifying --root, --target, or\n        # --prefix disables the check, since there's no reliable way to locate\n        # the EXTERNALLY-MANAGED file for those cases. An exception is also\n        # made specifically for \"--dry-run --report\" for convenience.\n        installing_into_current_environment = (\n            not (options.dry_run and options.json_report_file)\n            and options.root_path is None\n            and options.target_dir is None\n            and options.prefix_path is None\n        )\n        if (\n            installing_into_current_environment\n            and not options.override_externally_managed\n        ):\n            check_externally_managed()\n","sourceCodeStart":356,"sourceCodeEnd":392,"githubUrl":"https://github.com/pypa/pip/blob/f399c3718970b1b0e2478dac5296eb62679a9b86/src/pip/_internal/commands/install.py#L356-L392","documentation":"Raised by `pip install` when both --user and --target are supplied. InstallCommand.run at install.py:373 checks this first because the two flags imply mutually exclusive installation locations (user site-packages vs. an arbitrary target directory).","triggerScenarios":"Invoking `pip install --user --target ./libs <pkg>` — options.use_user_site is True and options.target_dir is not None, hitting install.py:373-374.","commonSituations":"User combines flags from different recipes, or scripts assume pip will pick one. Common when migrating a setup from per-user installs to a target-dir layout.","solutions":["Choose one: drop --user (use --target alone) or drop --target (use --user alone).","For bundling into a local directory use `pip install --target ./libs <pkg>`.","For installing to your user site use `pip install --user <pkg>`."],"exampleFix":"// before\npip install --user --target ./libs numpy\n// after\npip install --target ./libs numpy","handlingStrategy":"validation","validationCode":"# Reject combining --user and --target before invoking pip install\ndef install_opts_ok(use_user, target_dir):\n    if use_user and target_dir is not None:\n        raise ValueError(\"--user and --target are mutually exclusive\")\n    return True\n","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never pass both --user and --target in the same invocation.","Centralize install option assembly in one helper that enforces exclusivity."],"tags":["cli","pip-install","argument-validation","mutually-exclusive-flags","usage-error"],"backgroundTag":null,"analyzedSha":"f399c3718970b1b0e2478dac5296eb62679a9b86","analyzedAt":"2026-08-08T23:01:42.227Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}