Repository: pypa/pip

Issue:
Should we print an error if --user and --target are used together?
> I wonder why we aren't also throwing if `--target` is provided.

No clue. Filing a new issue to track someone figuring this out. :)

_Originally posted by @chrahunt in https://github.com/pypa/pip/pull/7002_


Discussion:
If I try passing both, I get an error from distutils. It's pretty clear, so maybe this is why it's not checked in pip, but maybe it still makes sense to check it and fail faster.

```
$ pip install --user --target test-tgt/  astcheck
Collecting astcheck
  Using cached https://files.pythonhosted.org/packages/8d/a7/23752054ffc65eca77b2137f88c3b4b8d196112b5e13e9536f194ab67756/astcheck-0.2.5-py2.py3-none-any.whl
Installing collected packages: astcheck
Exception:
Traceback (most recent call last):
  File "/usr/lib/python3.7/site-packages/pip/_internal/cli/base_command.py", line 179, in main
    status = self.run(options, args)
  File "/usr/lib/python3.7/site-packages/pip/_internal/commands/install.py", line 421, in run
    strip_file_prefix=options.strip_file_prefix,
  File "/usr/lib/python3.7/site-packages/pip/_internal/req/__init__.py", line 57, in install_given_reqs
    **kwargs
  File "/usr/lib/python3.7/site-packages/pip/_internal/req/req_install.py", line 917, in install
    strip_file_prefix=strip_file_prefix,
  File "/usr/lib/python3.7/site-packages/pip/_internal/req/req_install.py", line 447, in move_wheel_files
    strip_file_prefix=strip_file_prefix,
  File "/usr/lib/python3.7/site-packages/pip/_internal/wheel.py", line 326, in move_wheel_files
    prefix=prefix,
  File "/usr/lib/python3.7/site-packages/pip/_internal/locations.py", line 180, in distutils_scheme
    i.finalize_options()
  File "/usr/lib64/python3.7/distutils/command/install.py", line 252, in finalize_options
    raise DistutilsOptionError("can't combine user with prefix, "
distutils.errors.DistutilsOptionError: can't combine user with prefix, exec_prefix/home, or install_(plat)base
``` 
This seems pretty straightforward, so changing status.
@pradyunsg I am on it. :+1: 

Resolving patch:
diff --git a/src/pip/_internal/commands/install.py b/src/pip/_internal/commands/install.py
--- a/src/pip/_internal/commands/install.py
+++ b/src/pip/_internal/commands/install.py
@@ -237,6 +237,9 @@ def __init__(self, *args, **kw):
     @with_cleanup
     def run(self, options, args):
         # type: (Values, List[Any]) -> int
+        if options.use_user_site and options.target_dir is not None:
+            raise CommandError("Can not combine '--user' and '--target'")
+
         cmdoptions.check_install_build_global(options)
         upgrade_strategy = "to-satisfy-only"
         if options.upgrade:


Test patch:
diff --git a/tests/functional/test_install.py b/tests/functional/test_install.py
--- a/tests/functional/test_install.py
+++ b/tests/functional/test_install.py
@@ -847,6 +847,27 @@ def test_install_package_with_target(script):
     assert singlemodule_py in result.files_updated, str(result)
 
 
+def test_install_package_to_usersite_with_target_must_fail(script):
+    """
+    Test that installing package to usersite with target
+    must raise error
+    """
+    target_dir = script.scratch_path / 'target'
+    result = script.pip_install_local(
+        '--user', '-t', target_dir, "simple==1.0", expect_error=True
+    )
+    assert "Can not combine '--user' and '--target'" in result.stderr, (
+        str(result)
+    )
+
+    result = script.pip_install_local(
+        '--user', '--target', target_dir, "simple==1.0", expect_error=True
+    )
+    assert "Can not combine '--user' and '--target'" in result.stderr, (
+        str(result)
+    )
+
+
 def test_install_nonlocal_compatible_wheel(script, data):
     target_dir = script.scratch_path / 'target'