{"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/d7d0d0a39494e28ec1c407bd0680e4a4d1067791/src/pip/_internal/commands/install.py#L356-L392","documentation":"pip raises this CommandError in InstallCommand.run() when both --user and --target (the -t/--target option) are supplied simultaneously. The two flags specify mutually exclusive installation destinations: --user installs to the per-user site-packages directory while --target installs to an arbitrary directory. Because they both dictate where packages are written, pip refuses to guess which one takes precedence.","triggerScenarios":"Invoking pip install with both --user and --target <dir> on the command line, or having 'user = true' in a pip.conf while also passing --target on the CLI. The check at install.py:373 fires immediately when options.use_user_site is truthy and options.target_dir is not None.","commonSituations":"Copy-pasting a long pip install command from a guide that used --target into an environment where --user is set in a config file; CI scripts that layer --user for non-root installs while also using --target to stage wheels; build tooling (e.g. tox, nox) that injects --target for a staging dir while the user explicitly adds --user.","solutions":["Remove either --user or --target from the command — decide which destination you actually want.","If you want packages in a specific directory, drop --user and keep --target <dir> (use --upgrade to refresh existing entries).","If you want a user-level install, drop --target and keep --user.","Check your pip.conf files (global, user, site) for a stray 'user = true' setting that silently combines with your --target flag."],"exampleFix":"# before\npip install --user --target ./libs requests\n# after\npip install --target ./libs requests","handlingStrategy":"validation","validationCode":"# Validate before invoking pip install\nimport sys, subprocess\n\nuser_flag = '--user' in sys.argv\ntarget_flag = any(a in ('-t','--target') or a.startswith('--target=') for a in sys.argv)\nif user_flag and target_flag:\n    print('ERROR: --user and --target are mutually exclusive', file=sys.stderr)\n    sys.exit(2)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never combine --user with --target/--prefix in scripts; pick one destination explicitly.","Audit pip.conf for 'user = true' before relying on --target.","Wrap pip calls in a validation helper that rejects mutually exclusive flags."],"tags":["pip","install","cli-argument-conflict","user-target"],"analyzedSha":"d7d0d0a39494e28ec1c407bd0680e4a4d1067791","analyzedAt":"2026-08-04T20:55:04.259Z","schemaVersion":2}