We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
pip doesn't detect a venv created virtual environment as a virtual environment
The venv integration in Python 3.4 fails if PIP_REQUIREVIRTUALENV is set (http://bugs.python.org/issue19734)

I'm currently working around this by forcibly clearing the setting in the test, but the PIP_REQUIREVIRTUALENV check should pass when sys.prefix and sys.base_prefix are different.


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `pip/locations.py`
Content:
```
1 """Locations where we look for configs, install stuff, etc"""
2 
3 import sys
4 import site
5 import os
6 import tempfile
7 from distutils.command.install import install, SCHEME_KEYS
8 import getpass
9 from pip.backwardcompat import get_python_lib, get_path_uid, user_site
10 import pip.exceptions
11 
12 
13 DELETE_MARKER_MESSAGE = '''\
14 This file is placed here by pip to indicate the source was put
15 here by pip.
16 
17 Once this package is successfully installed this source code will be
18 deleted (unless you remove this file).
19 '''
20 PIP_DELETE_MARKER_FILENAME = 'pip-delete-this-directory.txt'
21 
22 def write_delete_marker_file(directory):
23     """
24     Write the pip delete marker file into this directory.
25     """
26     filepath = os.path.join(directory, PIP_DELETE_MARKER_FILENAME)
27     marker_fp = open(filepath, 'w')
28     marker_fp.write(DELETE_MARKER_MESSAGE)
29     marker_fp.close()
30 
31 
32 def running_under_virtualenv():
33     """
34     Return True if we're running inside a virtualenv, False otherwise.
35 
36     """
37     return hasattr(sys, 'real_prefix')
38 
39 
40 def virtualenv_no_global():
41     """
42     Return True if in a venv and no system site packages.
43     """
44     #this mirrors the logic in virtualenv.py for locating the no-global-site-packages.txt file
45     site_mod_dir = os.path.dirname(os.path.abspath(site.__file__))
46     no_global_file = os.path.join(site_mod_dir, 'no-global-site-packages.txt')
47     if running_under_virtualenv() and os.path.isfile(no_global_file):
48         return True
49 
50 def __get_username():
51     """ Returns the effective username of the current process. """
52     if sys.platform == 'win32':
53         return getpass.getuser()
54     import pwd
55     return pwd.getpwuid(os.geteuid()).pw_name
56 
57 def _get_build_prefix():
58     """ Returns a safe build_prefix """
59     path = os.path.join(tempfile.gettempdir(), 'pip_build_%s' %
60         __get_username())
61     if sys.platform == 'win32':
62         """ on windows(tested on 7) temp dirs are isolated """
63         return path
64     try:
65         os.mkdir(path)
66         write_delete_marker_file(path)
67     except OSError:
68         file_uid = None
69         try:
70             # raises OSError for symlinks
71             # https://github.com/pypa/pip/pull/935#discussion_r5307003
72             file_uid = get_path_uid(path)
73         except OSError:
74             file_uid = None
75 
76         if file_uid != os.geteuid():
77             msg = "The temporary folder for building (%s) is either not owned by you, or is a symlink." \
78                 % path
79             print (msg)
80             print("pip will not work until the temporary folder is " + \
81                  "either deleted or is a real directory owned by your user account.")
82             raise pip.exceptions.InstallationError(msg)
83     return path
84 
85 if running_under_virtualenv():
86     build_prefix = os.path.join(sys.prefix, 'build')
87     src_prefix = os.path.join(sys.prefix, 'src')
88 else:
89     # Note: intentionally NOT using mkdtemp
90     # See https://github.com/pypa/pip/issues/906 for plan to move to mkdtemp
91     build_prefix = _get_build_prefix()
92 
93     ## FIXME: keep src in cwd for now (it is not a temporary folder)
94     try:
95         src_prefix = os.path.join(os.getcwd(), 'src')
96     except OSError:
97         # In case the current working directory has been renamed or deleted
98         sys.exit("The folder you are executing pip from can no longer be found.")
99 
100 # under Mac OS X + virtualenv sys.prefix is not properly resolved
101 # it is something like /path/to/python/bin/..
102 # Note: using realpath due to tmp dirs on OSX being symlinks
103 build_prefix = os.path.abspath(os.path.realpath(build_prefix))
104 src_prefix = os.path.abspath(src_prefix)
105 
106 # FIXME doesn't account for venv linked to global site-packages
107 
108 site_packages = get_python_lib()
109 user_dir = os.path.expanduser('~')
110 if sys.platform == 'win32':
111     bin_py = os.path.join(sys.prefix, 'Scripts')
112     bin_user = os.path.join(user_site, 'Scripts') if user_site else None
113     # buildout uses 'bin' on Windows too?
114     if not os.path.exists(bin_py):
115         bin_py = os.path.join(sys.prefix, 'bin')
116         bin_user = os.path.join(user_site, 'bin') if user_site else None
117     default_storage_dir = os.path.join(user_dir, 'pip')
118     default_config_file = os.path.join(default_storage_dir, 'pip.ini')
119     default_log_file = os.path.join(default_storage_dir, 'pip.log')
120 else:
121     bin_py = os.path.join(sys.prefix, 'bin')
122     bin_user = os.path.join(user_site, 'bin') if user_site else None
123     default_storage_dir = os.path.join(user_dir, '.pip')
124     default_config_file = os.path.join(default_storage_dir, 'pip.conf')
125     default_log_file = os.path.join(default_storage_dir, 'pip.log')
126 
127     # Forcing to use /usr/local/bin for standard Mac OS X framework installs
128     # Also log to ~/Library/Logs/ for use with the Console.app log viewer
129     if sys.platform[:6] == 'darwin' and sys.prefix[:16] == '/System/Library/':
130         bin_py = '/usr/local/bin'
131         default_log_file = os.path.join(user_dir, 'Library/Logs/pip.log')
132 
133 
134 def distutils_scheme(dist_name, user=False, home=None, root=None):
135     """
136     Return a distutils install scheme
137     """
138     from distutils.dist import Distribution
139 
140     scheme = {}
141     d = Distribution({'name': dist_name})
142     d.parse_config_files()
143     i = d.get_command_obj('install', create=True)
144     # NOTE: setting user or home has the side-effect of creating the home dir or
145     # user base for installations during finalize_options()
146     # ideally, we'd prefer a scheme class that has no side-effects.
147     i.user = user or i.user
148     i.home = home or i.home
149     i.root = root or i.root
150     i.finalize_options()
151     for key in SCHEME_KEYS:
152         scheme[key] = getattr(i, 'install_'+key)
153 
154     if running_under_virtualenv():
155         scheme['headers'] = os.path.join(sys.prefix,
156                                     'include',
157                                     'site',
158                                     'python' + sys.version[:3],
159                                     dist_name)
160 
161         if root is not None:
162             scheme["headers"] = os.path.join(
163                 root,
164                 os.path.abspath(scheme["headers"])[1:],
165             )
166 
167     return scheme
168
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/pip/locations.py b/pip/locations.py
--- a/pip/locations.py
+++ b/pip/locations.py
@@ -34,7 +34,12 @@
     Return True if we're running inside a virtualenv, False otherwise.
 
     """
-    return hasattr(sys, 'real_prefix')
+    if hasattr(sys, 'real_prefix'):
+        return True
+    elif sys.prefix != getattr(sys, "base_prefix", sys.prefix):
+        return True
+
+    return False
 
 
 def virtualenv_no_global():

{"golden_diff": "diff --git a/pip/locations.py b/pip/locations.py\n--- a/pip/locations.py\n+++ b/pip/locations.py\n@@ -34,7 +34,12 @@\n     Return True if we're running inside a virtualenv, False otherwise.\n \n     \"\"\"\n-    return hasattr(sys, 'real_prefix')\n+    if hasattr(sys, 'real_prefix'):\n+        return True\n+    elif sys.prefix != getattr(sys, \"base_prefix\", sys.prefix):\n+        return True\n+\n+    return False\n \n \n def virtualenv_no_global():\n", "issue": "pip doesn't detect a venv created virtual environment as a virtual environment\nThe venv integration in Python 3.4 fails if PIP_REQUIREVIRTUALENV is set (http://bugs.python.org/issue19734)\n\nI'm currently working around this by forcibly clearing the setting in the test, but the PIP_REQUIREVIRTUALENV check should pass when sys.prefix and sys.base_prefix are different.\n\n", "before_files": [{"content": "\"\"\"Locations where we look for configs, install stuff, etc\"\"\"\n\nimport sys\nimport site\nimport os\nimport tempfile\nfrom distutils.command.install import install, SCHEME_KEYS\nimport getpass\nfrom pip.backwardcompat import get_python_lib, get_path_uid, user_site\nimport pip.exceptions\n\n\nDELETE_MARKER_MESSAGE = '''\\\nThis file is placed here by pip to indicate the source was put\nhere by pip.\n\nOnce this package is successfully installed this source code will be\ndeleted (unless you remove this file).\n'''\nPIP_DELETE_MARKER_FILENAME = 'pip-delete-this-directory.txt'\n\ndef write_delete_marker_file(directory):\n    \"\"\"\n    Write the pip delete marker file into this directory.\n    \"\"\"\n    filepath = os.path.join(directory, PIP_DELETE_MARKER_FILENAME)\n    marker_fp = open(filepath, 'w')\n    marker_fp.write(DELETE_MARKER_MESSAGE)\n    marker_fp.close()\n\n\ndef running_under_virtualenv():\n    \"\"\"\n    Return True if we're running inside a virtualenv, False otherwise.\n\n    \"\"\"\n    return hasattr(sys, 'real_prefix')\n\n\ndef virtualenv_no_global():\n    \"\"\"\n    Return True if in a venv and no system site packages.\n    \"\"\"\n    #this mirrors the logic in virtualenv.py for locating the no-global-site-packages.txt file\n    site_mod_dir = os.path.dirname(os.path.abspath(site.__file__))\n    no_global_file = os.path.join(site_mod_dir, 'no-global-site-packages.txt')\n    if running_under_virtualenv() and os.path.isfile(no_global_file):\n        return True\n\ndef __get_username():\n    \"\"\" Returns the effective username of the current process. \"\"\"\n    if sys.platform == 'win32':\n        return getpass.getuser()\n    import pwd\n    return pwd.getpwuid(os.geteuid()).pw_name\n\ndef _get_build_prefix():\n    \"\"\" Returns a safe build_prefix \"\"\"\n    path = os.path.join(tempfile.gettempdir(), 'pip_build_%s' %\n        __get_username())\n    if sys.platform == 'win32':\n        \"\"\" on windows(tested on 7) temp dirs are isolated \"\"\"\n        return path\n    try:\n        os.mkdir(path)\n        write_delete_marker_file(path)\n    except OSError:\n        file_uid = None\n        try:\n            # raises OSError for symlinks\n            # https://github.com/pypa/pip/pull/935#discussion_r5307003\n            file_uid = get_path_uid(path)\n        except OSError:\n            file_uid = None\n\n        if file_uid != os.geteuid():\n            msg = \"The temporary folder for building (%s) is either not owned by you, or is a symlink.\" \\\n                % path\n            print (msg)\n            print(\"pip will not work until the temporary folder is \" + \\\n                 \"either deleted or is a real directory owned by your user account.\")\n            raise pip.exceptions.InstallationError(msg)\n    return path\n\nif running_under_virtualenv():\n    build_prefix = os.path.join(sys.prefix, 'build')\n    src_prefix = os.path.join(sys.prefix, 'src')\nelse:\n    # Note: intentionally NOT using mkdtemp\n    # See https://github.com/pypa/pip/issues/906 for plan to move to mkdtemp\n    build_prefix = _get_build_prefix()\n\n    ## FIXME: keep src in cwd for now (it is not a temporary folder)\n    try:\n        src_prefix = os.path.join(os.getcwd(), 'src')\n    except OSError:\n        # In case the current working directory has been renamed or deleted\n        sys.exit(\"The folder you are executing pip from can no longer be found.\")\n\n# under Mac OS X + virtualenv sys.prefix is not properly resolved\n# it is something like /path/to/python/bin/..\n# Note: using realpath due to tmp dirs on OSX being symlinks\nbuild_prefix = os.path.abspath(os.path.realpath(build_prefix))\nsrc_prefix = os.path.abspath(src_prefix)\n\n# FIXME doesn't account for venv linked to global site-packages\n\nsite_packages = get_python_lib()\nuser_dir = os.path.expanduser('~')\nif sys.platform == 'win32':\n    bin_py = os.path.join(sys.prefix, 'Scripts')\n    bin_user = os.path.join(user_site, 'Scripts') if user_site else None\n    # buildout uses 'bin' on Windows too?\n    if not os.path.exists(bin_py):\n        bin_py = os.path.join(sys.prefix, 'bin')\n        bin_user = os.path.join(user_site, 'bin') if user_site else None\n    default_storage_dir = os.path.join(user_dir, 'pip')\n    default_config_file = os.path.join(default_storage_dir, 'pip.ini')\n    default_log_file = os.path.join(default_storage_dir, 'pip.log')\nelse:\n    bin_py = os.path.join(sys.prefix, 'bin')\n    bin_user = os.path.join(user_site, 'bin') if user_site else None\n    default_storage_dir = os.path.join(user_dir, '.pip')\n    default_config_file = os.path.join(default_storage_dir, 'pip.conf')\n    default_log_file = os.path.join(default_storage_dir, 'pip.log')\n\n    # Forcing to use /usr/local/bin for standard Mac OS X framework installs\n    # Also log to ~/Library/Logs/ for use with the Console.app log viewer\n    if sys.platform[:6] == 'darwin' and sys.prefix[:16] == '/System/Library/':\n        bin_py = '/usr/local/bin'\n        default_log_file = os.path.join(user_dir, 'Library/Logs/pip.log')\n\n\ndef distutils_scheme(dist_name, user=False, home=None, root=None):\n    \"\"\"\n    Return a distutils install scheme\n    \"\"\"\n    from distutils.dist import Distribution\n\n    scheme = {}\n    d = Distribution({'name': dist_name})\n    d.parse_config_files()\n    i = d.get_command_obj('install', create=True)\n    # NOTE: setting user or home has the side-effect of creating the home dir or\n    # user base for installations during finalize_options()\n    # ideally, we'd prefer a scheme class that has no side-effects.\n    i.user = user or i.user\n    i.home = home or i.home\n    i.root = root or i.root\n    i.finalize_options()\n    for key in SCHEME_KEYS:\n        scheme[key] = getattr(i, 'install_'+key)\n\n    if running_under_virtualenv():\n        scheme['headers'] = os.path.join(sys.prefix,\n                                    'include',\n                                    'site',\n                                    'python' + sys.version[:3],\n                                    dist_name)\n\n        if root is not None:\n            scheme[\"headers\"] = os.path.join(\n                root,\n                os.path.abspath(scheme[\"headers\"])[1:],\n            )\n\n    return scheme\n", "path": "pip/locations.py"}], "after_files": [{"content": "\"\"\"Locations where we look for configs, install stuff, etc\"\"\"\n\nimport sys\nimport site\nimport os\nimport tempfile\nfrom distutils.command.install import install, SCHEME_KEYS\nimport getpass\nfrom pip.backwardcompat import get_python_lib, get_path_uid, user_site\nimport pip.exceptions\n\n\nDELETE_MARKER_MESSAGE = '''\\\nThis file is placed here by pip to indicate the source was put\nhere by pip.\n\nOnce this package is successfully installed this source code will be\ndeleted (unless you remove this file).\n'''\nPIP_DELETE_MARKER_FILENAME = 'pip-delete-this-directory.txt'\n\ndef write_delete_marker_file(directory):\n    \"\"\"\n    Write the pip delete marker file into this directory.\n    \"\"\"\n    filepath = os.path.join(directory, PIP_DELETE_MARKER_FILENAME)\n    marker_fp = open(filepath, 'w')\n    marker_fp.write(DELETE_MARKER_MESSAGE)\n    marker_fp.close()\n\n\ndef running_under_virtualenv():\n    \"\"\"\n    Return True if we're running inside a virtualenv, False otherwise.\n\n    \"\"\"\n    if hasattr(sys, 'real_prefix'):\n        return True\n    elif sys.prefix != getattr(sys, \"base_prefix\", sys.prefix):\n        return True\n\n    return False\n\n\ndef virtualenv_no_global():\n    \"\"\"\n    Return True if in a venv and no system site packages.\n    \"\"\"\n    #this mirrors the logic in virtualenv.py for locating the no-global-site-packages.txt file\n    site_mod_dir = os.path.dirname(os.path.abspath(site.__file__))\n    no_global_file = os.path.join(site_mod_dir, 'no-global-site-packages.txt')\n    if running_under_virtualenv() and os.path.isfile(no_global_file):\n        return True\n\ndef __get_username():\n    \"\"\" Returns the effective username of the current process. \"\"\"\n    if sys.platform == 'win32':\n        return getpass.getuser()\n    import pwd\n    return pwd.getpwuid(os.geteuid()).pw_name\n\ndef _get_build_prefix():\n    \"\"\" Returns a safe build_prefix \"\"\"\n    path = os.path.join(tempfile.gettempdir(), 'pip_build_%s' %\n        __get_username())\n    if sys.platform == 'win32':\n        \"\"\" on windows(tested on 7) temp dirs are isolated \"\"\"\n        return path\n    try:\n        os.mkdir(path)\n        write_delete_marker_file(path)\n    except OSError:\n        file_uid = None\n        try:\n            # raises OSError for symlinks\n            # https://github.com/pypa/pip/pull/935#discussion_r5307003\n            file_uid = get_path_uid(path)\n        except OSError:\n            file_uid = None\n\n        if file_uid != os.geteuid():\n            msg = \"The temporary folder for building (%s) is either not owned by you, or is a symlink.\" \\\n                % path\n            print (msg)\n            print(\"pip will not work until the temporary folder is \" + \\\n                 \"either deleted or is a real directory owned by your user account.\")\n            raise pip.exceptions.InstallationError(msg)\n    return path\n\nif running_under_virtualenv():\n    build_prefix = os.path.join(sys.prefix, 'build')\n    src_prefix = os.path.join(sys.prefix, 'src')\nelse:\n    # Note: intentionally NOT using mkdtemp\n    # See https://github.com/pypa/pip/issues/906 for plan to move to mkdtemp\n    build_prefix = _get_build_prefix()\n\n    ## FIXME: keep src in cwd for now (it is not a temporary folder)\n    try:\n        src_prefix = os.path.join(os.getcwd(), 'src')\n    except OSError:\n        # In case the current working directory has been renamed or deleted\n        sys.exit(\"The folder you are executing pip from can no longer be found.\")\n\n# under Mac OS X + virtualenv sys.prefix is not properly resolved\n# it is something like /path/to/python/bin/..\n# Note: using realpath due to tmp dirs on OSX being symlinks\nbuild_prefix = os.path.abspath(os.path.realpath(build_prefix))\nsrc_prefix = os.path.abspath(src_prefix)\n\n# FIXME doesn't account for venv linked to global site-packages\n\nsite_packages = get_python_lib()\nuser_dir = os.path.expanduser('~')\nif sys.platform == 'win32':\n    bin_py = os.path.join(sys.prefix, 'Scripts')\n    bin_user = os.path.join(user_site, 'Scripts') if user_site else None\n    # buildout uses 'bin' on Windows too?\n    if not os.path.exists(bin_py):\n        bin_py = os.path.join(sys.prefix, 'bin')\n        bin_user = os.path.join(user_site, 'bin') if user_site else None\n    default_storage_dir = os.path.join(user_dir, 'pip')\n    default_config_file = os.path.join(default_storage_dir, 'pip.ini')\n    default_log_file = os.path.join(default_storage_dir, 'pip.log')\nelse:\n    bin_py = os.path.join(sys.prefix, 'bin')\n    bin_user = os.path.join(user_site, 'bin') if user_site else None\n    default_storage_dir = os.path.join(user_dir, '.pip')\n    default_config_file = os.path.join(default_storage_dir, 'pip.conf')\n    default_log_file = os.path.join(default_storage_dir, 'pip.log')\n\n    # Forcing to use /usr/local/bin for standard Mac OS X framework installs\n    # Also log to ~/Library/Logs/ for use with the Console.app log viewer\n    if sys.platform[:6] == 'darwin' and sys.prefix[:16] == '/System/Library/':\n        bin_py = '/usr/local/bin'\n        default_log_file = os.path.join(user_dir, 'Library/Logs/pip.log')\n\n\ndef distutils_scheme(dist_name, user=False, home=None, root=None):\n    \"\"\"\n    Return a distutils install scheme\n    \"\"\"\n    from distutils.dist import Distribution\n\n    scheme = {}\n    d = Distribution({'name': dist_name})\n    d.parse_config_files()\n    i = d.get_command_obj('install', create=True)\n    # NOTE: setting user or home has the side-effect of creating the home dir or\n    # user base for installations during finalize_options()\n    # ideally, we'd prefer a scheme class that has no side-effects.\n    i.user = user or i.user\n    i.home = home or i.home\n    i.root = root or i.root\n    i.finalize_options()\n    for key in SCHEME_KEYS:\n        scheme[key] = getattr(i, 'install_'+key)\n\n    if running_under_virtualenv():\n        scheme['headers'] = os.path.join(sys.prefix,\n                                    'include',\n                                    'site',\n                                    'python' + sys.version[:3],\n                                    dist_name)\n\n        if root is not None:\n            scheme[\"headers\"] = os.path.join(\n                root,\n                os.path.abspath(scheme[\"headers\"])[1:],\n            )\n\n    return scheme\n", "path": "pip/locations.py"}]}