Repository: pypa/pipenv

Issue:
pipenv prefers activated virtualenvs when looking for projects
### Issue description

```pipenv shell``` can not activate custom virtualenv created by ```virtualenv```

### Actual result

```
➜  abcd  . /Users/jxltom/.local/share/virtualenvs/abcd-Gj3qkKPD/bin/activate
.: no such file or directory: /Users/liujunxiao/.local/share/virtualenvs/abcd-Gj3qkKPD/bin/activate
```

### Steps to replicate

- ```virtualenv somedir```
- ```cd somedir && source bin/activate```
- ```pipenv shell```

-------------------------------------------------------------------------------

Please run `$ pipenv --support`, and paste the results here. Don't put backticks (`` ` ``) around it! The output already contains Markdown formatting.

If you're on macOS, run the following:

    $ pipenv --support | pbcopy

If you're on Windows, run the following:

    > pipenv --support | clip

If you're on Linux, run the following:

    $ pipenv --support | xclip



Discussion:

Resolving patch:
diff --git a/pipenv/core.py b/pipenv/core.py
--- a/pipenv/core.py
+++ b/pipenv/core.py
@@ -2139,11 +2139,6 @@ def do_shell(three=None, python=False, fancy=False, shell_args=None, pypi_mirror
         three=three, python=python, validate=False, pypi_mirror=pypi_mirror,
     )
 
-    # Set an environment variable, so we know we're in the environment.
-    os.environ["PIPENV_ACTIVE"] = vistir.misc.fs_str("1")
-
-    os.environ.pop("PIP_SHIMS_BASE_MODULE", None)
-
     # Support shell compatibility mode.
     if PIPENV_SHELL_FANCY:
         fancy = True
@@ -2159,6 +2154,13 @@ def do_shell(three=None, python=False, fancy=False, shell_args=None, pypi_mirror
         shell_args,
     )
 
+    # Set an environment variable, so we know we're in the environment.
+    # Only set PIPENV_ACTIVE after finishing reading virtualenv_location
+    # otherwise its value will be changed
+    os.environ["PIPENV_ACTIVE"] = vistir.misc.fs_str("1")
+    
+    os.environ.pop("PIP_SHIMS_BASE_MODULE", None)
+
     if fancy:
         shell.fork(*fork_args)
         return
@@ -2293,14 +2295,19 @@ def do_run(command, args, three=None, python=False, pypi_mirror=None):
         three=three, python=python, validate=False, pypi_mirror=pypi_mirror,
     )
 
-    # Set an environment variable, so we know we're in the environment.
-    os.environ["PIPENV_ACTIVE"] = vistir.misc.fs_str("1")
-
-    os.environ.pop("PIP_SHIMS_BASE_MODULE", None)
     load_dot_env()
 
     # Activate virtualenv under the current interpreter's environment
     inline_activate_virtual_environment()
+
+    # Set an environment variable, so we know we're in the environment.
+    # Only set PIPENV_ACTIVE after finishing reading virtualenv_location
+    # such as in inline_activate_virtual_environment
+    # otherwise its value will be changed
+    os.environ["PIPENV_ACTIVE"] = vistir.misc.fs_str("1")
+
+    os.environ.pop("PIP_SHIMS_BASE_MODULE", None)
+    
     try:
         script = project.build_script(command, args)
         cmd_string = ' '.join([script.command] + script.args)


Test patch:
diff --git a/tests/integration/test_project.py b/tests/integration/test_project.py
--- a/tests/integration/test_project.py
+++ b/tests/integration/test_project.py
@@ -170,7 +170,15 @@ def test_include_editable_packages(PipenvInstance, pypi, testsroot, pathlib_tmpd
 @pytest.mark.virtualenv
 def test_run_in_virtualenv(PipenvInstance, pypi, virtualenv):
     with PipenvInstance(chdir=True, pypi=pypi) as p:
+        os.environ['PIPENV_IGNORE_VIRTUALENVS'] = '1'
+        c = p.pipenv('run which pip')
+        assert c.return_code == 0
+        assert 'virtualenv' not in c.out
+
         os.environ.pop("PIPENV_IGNORE_VIRTUALENVS", None)
+        c = p.pipenv('run which pip')
+        assert c.return_code == 0
+        assert 'virtualenv' in c.out
         project = Project()
         assert project.virtualenv_location == str(virtualenv)
         c = p.pipenv("run pip install click")