Repository: pypa/virtualenv

Issue:
site.getsitepackages() broken on debian
**Issue**

Debian and it's derivatives patch site.getsitepackages() to use `dist-packages` directories instead of `site-packages` directories. See https://salsa.debian.org/cpython-team/python2/-/blob/master/debian/patches/site-locations.diff

This causes site.getsitepackages() to not point to the correct directory inside virtualenvs.

I discovered this bug while looking into why virtualenv>=20 broke some mypy tests: https://github.com/python/mypy/issues/10407

I'm currently preparing a PR to fix this.

**Environment**

- OS: debian derivatives. Tested on Ubuntu 18.04.3 LTS
- ``pip list`` of the host python where ``virtualenv`` is installed:

```console
apipkg (1.5)
appdirs (1.4.4)
attrs (20.3.0)
coverage (5.5)
distlib (0.3.1)
execnet (1.8.0)
filelock (3.0.12)
flake8 (3.9.1)
flake8-bugbear (21.4.3)
flake8-pyi (20.10.0)
importlib-metadata (0.20)
importlib-resources (5.1.2)
iniconfig (1.1.1)
lxml (4.6.3)
mccabe (0.6.1)
mypy-extensions (0.4.3)
packaging (20.9)
pip (9.0.1)
pkg-resources (0.0.0)
pluggy (0.13.1)
psutil (5.8.0)
py (1.10.0)
pycodestyle (2.7.0)
pyflakes (2.3.1)
pyparsing (2.4.7)
pytest (6.1.2)
pytest-cov (2.11.1)
pytest-forked (1.3.0)
pytest-xdist (1.34.0)
setuptools (56.0.0)
six (1.15.0)
toml (0.10.2)
typed-ast (1.4.3)
types-mypy-extensions (0.4.3)
types-toml (0.1.1)
types-typed-ast (1.4.1)
typing-extensions (3.10.0.0)
virtualenv (20.4.4)
zipp (3.4.1)
```

**Output of the virtual environment creation**

Make sure to run the creation with `-vvv --with-traceback`:

```console
61 setup logging to NOTSET [DEBUG report:43]
78 find interpreter for spec PythonSpec(implementation=CPython, major=2) [INFO builtin:62]
78 discover exe for PythonInfo(spec=CPython3.6.9.final.0-64, exe=/home/vagrant/mypy/.venv/bin/python, platform=linux, version='3.6.9 (default, Jan 26 2021, 15:33:00) \n[GCC 8.4.0]', encoding_fs_io=utf-8-UTF-8) in /usr [DEBUG py_info:372]
79 filesystem is case-sensitive [DEBUG info:28]
80 Attempting to acquire lock 139857009658960 on /home/vagrant/.local/share/virtualenv/py_info/1/30295c5bec572e859485b1ffa5e89b8b3e2022ef6e3e739c1ac40f143a557caf.lock [DEBUG filelock:270]
80 Lock 139857009658960 acquired on /home/vagrant/.local/share/virtualenv/py_info/1/30295c5bec572e859485b1ffa5e89b8b3e2022ef6e3e739c1ac40f143a557caf.lock [INFO filelock:274]
81 got python info of /usr/bin/python3.6 from /home/vagrant/.local/share/virtualenv/py_info/1/30295c5bec572e859485b1ffa5e89b8b3e2022ef6e3e739c1ac40f143a557caf.json [DEBUG via_disk_folder:135]
81 Attempting to release lock 139857009658960 on /home/vagrant/.local/share/virtualenv/py_info/1/30295c5bec572e859485b1ffa5e89b8b3e2022ef6e3e739c1ac40f143a557caf.lock [DEBUG filelock:315]
82 Lock 139857009658960 released on /home/vagrant/.local/share/virtualenv/py_info/1/30295c5bec572e859485b1ffa5e89b8b3e2022ef6e3e739c1ac40f143a557caf.lock [INFO filelock:318]
82 proposed PythonInfo(spec=CPython3.6.9.final.0-64, system=/usr/bin/python3.6, exe=/home/vagrant/mypy/.venv/bin/python, platform=linux, version='3.6.9 (default, Jan 26 2021, 15:33:00) \n[GCC 8.4.0]', encoding_fs_io=utf-8-UTF-8) [INFO builtin:69]
82 discover PATH[0]=/home/vagrant/mypy/.venv/bin [DEBUG builtin:114]
83 discover exe from cache /usr - exact False: PythonInfo({'architecture': 64, 'base_exec_prefix': '/usr', 'base_prefix': '/usr', 'distutils_install': {'data': '', 'headers': 'include/python3.6m/UNKNOWN', 'platlib': 'lib/python3.6/site-packages', 'purelib': 'lib/python3.6/site-packages', 'scripts': 'bin'}, 'exec_prefix': '/usr', 'executable': '/home/vagrant/mypy/.venv/bin/python', 'file_system_encoding': 'utf-8', 'has_venv': True, 'implementation': 'CPython', 'max_size': 9223372036854775807, 'original_executable': '/usr/bin/python3.6', 'os': 'posix', 'path': ['/home/vagrant/mypy/.venv/lib/python3.6/site-packages/virtualenv/discovery', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages'], 'platform': 'linux', 'prefix': '/usr', 'real_prefix': None, 'stdout_encoding': 'UTF-8', 'sysconfig': {'makefile_filename': '/usr/lib/python3.6/config-3.6m-x86_64-linux-gnu/Makefile'}, 'sysconfig_paths': {'data': '{base}', 'include': '{installed_base}/include/python{py_version_short}{abiflags}', 'platlib': '{platbase}/lib/python{py_version_short}/site-packages', 'platstdlib': '{platbase}/lib/python{py_version_short}', 'purelib': '{base}/lib/python{py_version_short}/site-packages', 'scripts': '{base}/bin', 'stdlib': '{installed_base}/lib/python{py_version_short}'}, 'sysconfig_vars': {'PYTHONFRAMEWORK': '', 'abiflags': 'm', 'base': '/usr', 'installed_base': '/usr', 'platbase': '/usr', 'py_version_short': '3.6'}, 'system_executable': '/usr/bin/python3.6', 'system_stdlib': '/usr/lib/python3.6', 'system_stdlib_platform': '/usr/lib/python3.6', 'version': '3.6.9 (default, Jan 26 2021, 15:33:00) \n[GCC 8.4.0]', 'version_info': VersionInfo(major=3, minor=6, micro=9, releaselevel='final', serial=0)}) [DEBUG py_info:370]
83 discover PATH[1]=/usr/local/sbin [DEBUG builtin:114]
84 discover PATH[2]=/usr/local/bin [DEBUG builtin:114]
84 discover PATH[3]=/usr/sbin [DEBUG builtin:114]
84 discover PATH[4]=/usr/bin [DEBUG builtin:114]
85 Attempting to acquire lock 139857009612280 on /home/vagrant/.local/share/virtualenv/py_info/1/f92b2924b84ff19c1c3dc485f7644d4486f64738191026bf8e6de303969141b5.lock [DEBUG filelock:270]
85 Lock 139857009612280 acquired on /home/vagrant/.local/share/virtualenv/py_info/1/f92b2924b84ff19c1c3dc485f7644d4486f64738191026bf8e6de303969141b5.lock [INFO filelock:274]
85 got python info of /usr/bin/python2 from /home/vagrant/.local/share/virtualenv/py_info/1/f92b2924b84ff19c1c3dc485f7644d4486f64738191026bf8e6de303969141b5.json [DEBUG via_disk_folder:135]
86 Attempting to release lock 139857009612280 on /home/vagrant/.local/share/virtualenv/py_info/1/f92b2924b84ff19c1c3dc485f7644d4486f64738191026bf8e6de303969141b5.lock [DEBUG filelock:315]
86 Lock 139857009612280 released on /home/vagrant/.local/share/virtualenv/py_info/1/f92b2924b84ff19c1c3dc485f7644d4486f64738191026bf8e6de303969141b5.lock [INFO filelock:318]
86 proposed PathPythonInfo(spec=CPython2.7.15.final.0-64, exe=/usr/bin/python2, platform=linux2, version='2.7.15+ (default, Nov 27 2018, 23:36:35) \n[GCC 7.3.0]', encoding_fs_io=UTF-8-None) [INFO builtin:69]
87 accepted PathPythonInfo(spec=CPython2.7.15.final.0-64, exe=/usr/bin/python2, platform=linux2, version='2.7.15+ (default, Nov 27 2018, 23:36:35) \n[GCC 7.3.0]', encoding_fs_io=UTF-8-None) [DEBUG builtin:71]
123 create virtual environment via CPython2Posix(dest=/home/vagrant/mypy/testenv2, clear=False, no_vcs_ignore=False, global=False) [INFO session:52]
123 no include folders as can't find include marker /usr/local/include/python2.7/Python.h [DEBUG cpython2:53]
124 create folder /home/vagrant/mypy/testenv2/bin [DEBUG _sync:25]
124 create folder /home/vagrant/mypy/testenv2/lib/python2.7/site-packages [DEBUG _sync:25]
125 write /home/vagrant/mypy/testenv2/pyvenv.cfg [DEBUG pyenv_cfg:34]
125     home = /usr [DEBUG pyenv_cfg:38]
125     implementation = CPython [DEBUG pyenv_cfg:38]
126     version_info = 2.7.15.final.0 [DEBUG pyenv_cfg:38]
126     virtualenv = 20.4.4 [DEBUG pyenv_cfg:38]
126     include-system-site-packages = false [DEBUG pyenv_cfg:38]
127     base-prefix = /usr [DEBUG pyenv_cfg:38]
127     base-exec-prefix = /usr [DEBUG pyenv_cfg:38]
127     base-executable = /usr/bin/python2 [DEBUG pyenv_cfg:38]
128 copy /usr/bin/python2 to /home/vagrant/mypy/testenv2/bin/python [DEBUG _sync:52]
131 symlink /usr/lib/python2.7/os.py to /home/vagrant/mypy/testenv2/lib/python2.7/os.py [DEBUG _sync:44]
132 symlink /usr/lib/python2.7/os.pyc to /home/vagrant/mypy/testenv2/lib/python2.7/os.pyc [DEBUG _sync:44]
132 symlink directory /usr/lib/python2.7/lib-dynload to /home/vagrant/mypy/testenv2/lib/python2.7/lib-dynload [DEBUG _sync:44]
132 create virtualenv import hook file /home/vagrant/mypy/testenv2/lib/python2.7/site-packages/_virtualenv.pth [DEBUG api:95]
133 create /home/vagrant/mypy/testenv2/lib/python2.7/site-packages/_virtualenv.py [DEBUG api:98]
134 ============================== target debug ============================== [DEBUG session:54]
134 debug via /home/vagrant/mypy/testenv2/bin/python /home/vagrant/mypy/.venv/lib/python3.6/site-packages/virtualenv/create/debug.py [DEBUG creator:224]
134 {
  "sys": {
    "executable": "/home/vagrant/mypy/testenv2/bin/python",
    "_base_executable": null,
    "prefix": "/home/vagrant/mypy/testenv2",
    "base_prefix": "/usr",
    "real_prefix": "/usr",
    "exec_prefix": "/home/vagrant/mypy/testenv2",
    "base_exec_prefix": "/usr",
    "path": [
      "/usr/lib/python2.7",
      "/usr/lib/python2.7/plat-x86_64-linux-gnu",
      "/usr/lib/python2.7/lib-tk",
      "/usr/lib/python2.7/lib-old",
      "/usr/lib/python2.7/lib-dynload",
      "/home/vagrant/mypy/testenv2/lib/python2.7/site-packages"
    ],
    "meta_path": [
      "<class '_virtualenv._VirtualenvImporter'>"
    ],
    "fs_encoding": "UTF-8",
    "io_encoding": null
  },
  "version": "2.7.15+ (default, Nov 27 2018, 23:36:35) \n[GCC 7.3.0]",
  "makefile_filename": "/usr/lib/python2.7/config-x86_64-linux-gnu/Makefile",
  "os": "<module 'os' from '/usr/lib/python2.7/os.pyc'>",
  "site": "<module 'site' from '/usr/lib/python2.7/site.pyc'>",
  "datetime": "<module 'datetime' (built-in)>",
  "math": "<module 'math' (built-in)>",
  "json": "<module 'json' from '/usr/lib/python2.7/json/__init__.pyc'>"
} [DEBUG session:55]
150 add seed packages via FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/vagrant/.local/share/virtualenv) [INFO session:59]
152 got embed update of distribution pip from /home/vagrant/.local/share/virtualenv/wheel/2.7/embed/1/pip.json [DEBUG via_disk_folder:135]
154 got embed update of distribution pip from /home/vagrant/.local/share/virtualenv/wheel/2.7/embed/1/pip.json [DEBUG via_disk_folder:135]
155 got embed update of distribution setuptools from /home/vagrant/.local/share/virtualenv/wheel/2.7/embed/1/setuptools.json [DEBUG via_disk_folder:135]
156 got embed update of distribution setuptools from /home/vagrant/.local/share/virtualenv/wheel/2.7/embed/1/setuptools.json [DEBUG via_disk_folder:135]
157 got embed update of distribution wheel from /home/vagrant/.local/share/virtualenv/wheel/2.7/embed/1/wheel.json [DEBUG via_disk_folder:135]
158 got embed update of distribution wheel from /home/vagrant/.local/share/virtualenv/wheel/2.7/embed/1/wheel.json [DEBUG via_disk_folder:135]
158 install pip from wheel /home/vagrant/mypy/.venv/lib/python3.6/site-packages/virtualenv/seed/wheels/embed/pip-20.3.4-py2.py3-none-any.whl via CopyPipInstall [DEBUG via_app_data:49]
159 Attempting to acquire lock 139857002467224 on /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/pip-20.3.4-py2.py3-none-any.lock [DEBUG filelock:270]
159 Lock 139857002467224 acquired on /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/pip-20.3.4-py2.py3-none-any.lock [INFO filelock:274]
159 Attempting to release lock 139857002467224 on /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/pip-20.3.4-py2.py3-none-any.lock [DEBUG filelock:315]
160 Lock 139857002467224 released on /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/pip-20.3.4-py2.py3-none-any.lock [INFO filelock:318]
160 copy directory /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/pip-20.3.4-py2.py3-none-any/pip to /home/vagrant/mypy/testenv2/lib/python2.7/site-packages/pip [DEBUG _sync:52]
172 install setuptools from wheel /home/vagrant/mypy/.venv/lib/python3.6/site-packages/virtualenv/seed/wheels/embed/setuptools-44.1.1-py2.py3-none-any.whl via CopyPipInstall [DEBUG via_app_data:49]
173 Attempting to acquire lock 139857002509032 on /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/setuptools-44.1.1-py2.py3-none-any.lock [DEBUG filelock:270]
173 Lock 139857002509032 acquired on /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/setuptools-44.1.1-py2.py3-none-any.lock [INFO filelock:274]
173 Attempting to release lock 139857002509032 on /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/setuptools-44.1.1-py2.py3-none-any.lock [DEBUG filelock:315]
174 Lock 139857002509032 released on /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/setuptools-44.1.1-py2.py3-none-any.lock [INFO filelock:318]
174 copy /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/setuptools-44.1.1-py2.py3-none-any/easy_install.py to /home/vagrant/mypy/testenv2/lib/python2.7/site-packages/easy_install.py [DEBUG _sync:52]
174 copy directory /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/setuptools-44.1.1-py2.py3-none-any/setuptools-44.1.1.dist-info to /home/vagrant/mypy/testenv2/lib/python2.7/site-packages/setuptools-44.1.1.dist-info [DEBUG _sync:52]
176 copy directory /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/setuptools-44.1.1-py2.py3-none-any/setuptools to /home/vagrant/mypy/testenv2/lib/python2.7/site-packages/setuptools [DEBUG _sync:52]
183 copy directory /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/setuptools-44.1.1-py2.py3-none-any/pkg_resources to /home/vagrant/mypy/testenv2/lib/python2.7/site-packages/pkg_resources [DEBUG _sync:52]
176 install wheel from wheel /home/vagrant/mypy/.venv/lib/python3.6/site-packages/virtualenv/seed/wheels/embed/wheel-0.36.2-py2.py3-none-any.whl via CopyPipInstall [DEBUG via_app_data:49]
184 Attempting to acquire lock 139857002509312 on /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/wheel-0.36.2-py2.py3-none-any.lock [DEBUG filelock:270]
184 Lock 139857002509312 acquired on /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/wheel-0.36.2-py2.py3-none-any.lock [INFO filelock:274]
184 Attempting to release lock 139857002509312 on /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/wheel-0.36.2-py2.py3-none-any.lock [DEBUG filelock:315]
185 Lock 139857002509312 released on /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/wheel-0.36.2-py2.py3-none-any.lock [INFO filelock:318]
185 copy /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/wheel-0.36.2-py2.py3-none-any/wheel-0.36.2.virtualenv to /home/vagrant/mypy/testenv2/lib/python2.7/site-packages/wheel-0.36.2.virtualenv [DEBUG _sync:52]
185 copy directory /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/wheel-0.36.2-py2.py3-none-any/wheel-0.36.2.dist-info to /home/vagrant/mypy/testenv2/lib/python2.7/site-packages/wheel-0.36.2.dist-info [DEBUG _sync:52]
186 copy directory /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/wheel-0.36.2-py2.py3-none-any/wheel to /home/vagrant/mypy/testenv2/lib/python2.7/site-packages/wheel [DEBUG _sync:52]
189 generated console scripts wheel2 wheel wheel-2.7 wheel2.7 [DEBUG base:48]
192 copy /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/setuptools-44.1.1-py2.py3-none-any/setuptools-44.1.1.virtualenv to /home/vagrant/mypy/testenv2/lib/python2.7/site-packages/setuptools-44.1.1.virtualenv [DEBUG _sync:52]
207 generated console scripts easy_install-2.7 easy_install2.7 easy_install easy_install2 [DEBUG base:48]
216 copy /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/pip-20.3.4-py2.py3-none-any/pip-20.3.4.virtualenv to /home/vagrant/mypy/testenv2/lib/python2.7/site-packages/pip-20.3.4.virtualenv [DEBUG _sync:52]
217 copy directory /home/vagrant/.local/share/virtualenv/wheel/2.7/image/1/CopyPipInstall/pip-20.3.4-py2.py3-none-any/pip-20.3.4.dist-info to /home/vagrant/mypy/testenv2/lib/python2.7/site-packages/pip-20.3.4.dist-info [DEBUG _sync:52]
218 generated console scripts pip pip2 pip-2.7 pip2.7 [DEBUG base:48]
219 add activators for Bash, CShell, Fish, PowerShell, Python [INFO session:66]
220 write /home/vagrant/mypy/testenv2/pyvenv.cfg [DEBUG pyenv_cfg:34]
220     home = /usr [DEBUG pyenv_cfg:38]
221     implementation = CPython [DEBUG pyenv_cfg:38]
221     version_info = 2.7.15.final.0 [DEBUG pyenv_cfg:38]
221     virtualenv = 20.4.4 [DEBUG pyenv_cfg:38]
222     include-system-site-packages = false [DEBUG pyenv_cfg:38]
222     base-prefix = /usr [DEBUG pyenv_cfg:38]
223     base-exec-prefix = /usr [DEBUG pyenv_cfg:38]
223     base-executable = /usr/bin/python2 [DEBUG pyenv_cfg:38]
223 created virtual environment CPython2.7.15.final.0-64 in 164ms
  creator CPython2Posix(dest=/home/vagrant/mypy/testenv2, clear=False, no_vcs_ignore=False, global=False)
  seeder FromAppData(download=False, pip=bundle, setuptools=bundle, wheel=bundle, via=copy, app_data_dir=/home/vagrant/.local/share/virtualenv)
    added seed packages: pip==20.3.4, setuptools==44.1.1, wheel==0.36.2
  activators BashActivator,CShellActivator,FishActivator,PowerShellActivator,PythonActivator [WARNING __main__:19]
```



Discussion:
The patch only applies to the system python, not the created virtual environments.
The patch applies to virtual environments. Virtualenv>=20 on python2 has it's own site.py, which loads the system site.py and fixes up sys.path. It does however not fix `site.getsitepackages()`.

```console
python -m virtualenv -ppython2 testenv2
. testenv2/bin/activate
python -c "import site; print(site.getsitepackages())"
['/home/vagrant/mypy/testenv2/local/lib/python2.7/dist-packages', '/home/vagrant/mypy/testenv2/lib/python2.7/dist-packages']
```
virtualenv 20.4.4 on Ubuntu 18.04.3 LTS

I already wrote a patch to fix this and am currently preparing a PR.
What does happen in venv?
This issue only affects python2.
On python3 both venv and virtualenv output `['/home/vagrant/mypy/testenv3/lib/python3.6/site-packages', '/home/vagrant/mypy/testenv3/local/lib/python3.6/dist-packages', '/home/vagrant/mypy/testenv3/lib/python3/dist-packages', '/home/vagrant/mypy/testenv3/lib/python3.6/dist-packages']`, which also contain a few `dist-packages` paths, but also contain the correct `site-packages` path.
Ah, python 2 my old friend. 

![not miss you](https://media.tenor.co/images/a3ecaf5e457d1f402ea38ccc25522f15/tenor.gif)
I just checked the debian patches for python3 and there they work around virtual environments by checking for the VIRTUAL_ENV environment variable: https://salsa.debian.org/cpython-team/python3/-/blob/master/debian/patches/distutils-install-layout.diff#L197

Debian doesn't do that for python2
The fix I came up with just replaces occurences of 'dist-packages' with 'site-packages' in getsitepackages. It's probably not the cleanest fix, but it works.
https://github.com/freundTech/virtualenv/commit/00445101230aa88077428723cf57ceb345966464

A cleaner fix could be reimplementing getsitepackages in virtualenv and patching it into site.
Ugh, seems an ugly hack for Debian lacking proper patching, but can't come up with better.

Resolving patch:
diff --git a/src/virtualenv/create/via_global_ref/builtin/python2/site.py b/src/virtualenv/create/via_global_ref/builtin/python2/site.py
--- a/src/virtualenv/create/via_global_ref/builtin/python2/site.py
+++ b/src/virtualenv/create/via_global_ref/builtin/python2/site.py
@@ -9,6 +9,7 @@
 
 def main():
     """Patch what needed, and invoke the original site.py"""
+    here = __file__  # the distutils.install patterns will be injected relative to this site.py, save it here
     config = read_pyvenv()
     sys.real_prefix = sys.base_prefix = config["base-prefix"]
     sys.base_exec_prefix = config["base-exec-prefix"]
@@ -16,12 +17,13 @@ def main():
     global_site_package_enabled = config.get("include-system-site-packages", False) == "true"
     rewrite_standard_library_sys_path()
     disable_user_site_package()
-    load_host_site()
+    load_host_site(here)
     if global_site_package_enabled:
         add_global_site_package()
+    rewrite_getsitepackages(here)
 
 
-def load_host_site():
+def load_host_site(here):
     """trigger reload of site.py - now it will use the standard library instance that will take care of init"""
     # we have a duality here, we generate the platform and pure library path based on what distutils.install specifies
     # because this is what pip will be using; the host site.py though may contain it's own pattern for where the
@@ -36,23 +38,26 @@ def load_host_site():
     # to facilitate when the two match, or not we first reload the site.py, now triggering the import of host site.py,
     # as this will ensure that initialization code within host site.py runs
 
-    here = __file__  # the distutils.install patterns will be injected relative to this site.py, save it here
-
     # ___RELOAD_CODE___
 
     # and then if the distutils site packages are not on the sys.path we add them via add_site_dir; note we must add
     # them by invoking add_site_dir to trigger the processing of pth files
+
+    add_site_dir = sys.modules["site"].addsitedir
+    for path in get_site_packages_dirs(here):
+        add_site_dir(path)
+
+
+def get_site_packages_dirs(here):
+    import json
     import os
 
     site_packages = r"""
     ___EXPECTED_SITE_PACKAGES___
     """
-    import json
 
-    add_site_dir = sys.modules["site"].addsitedir
     for path in json.loads(site_packages):
-        full_path = os.path.abspath(os.path.join(here, path.encode("utf-8")))
-        add_site_dir(full_path)
+        yield os.path.abspath(os.path.join(here, path.encode("utf-8")))
 
 
 sep = "\\" if sys.platform == "win32" else "/"  # no os module here yet - poor mans version
@@ -161,4 +166,25 @@ def add_global_site_package():
         site.PREFIXES = orig_prefixes + site.PREFIXES
 
 
+# Debian and it's derivatives patch this function. We undo the damage
+def rewrite_getsitepackages(here):
+    site = sys.modules["site"]
+
+    site_package_dirs = get_site_packages_dirs(here)
+    orig_getsitepackages = site.getsitepackages
+
+    def getsitepackages():
+        sitepackages = orig_getsitepackages()
+        if sys.prefix not in site.PREFIXES or sys.exec_prefix not in site.PREFIXES:
+            # Someone messed with the prefixes, so we stop patching
+            return sitepackages
+        for path in site_package_dirs:
+            if path not in sitepackages:
+                sitepackages.insert(0, path)
+
+        return sitepackages
+
+    site.getsitepackages = getsitepackages
+
+
 main()


Test patch:
diff --git a/tests/unit/create/test_creator.py b/tests/unit/create/test_creator.py
--- a/tests/unit/create/test_creator.py
+++ b/tests/unit/create/test_creator.py
@@ -7,6 +7,7 @@
 import logging
 import os
 import shutil
+import site
 import stat
 import subprocess
 import sys
@@ -625,15 +626,11 @@ def test_pth_in_site_vs_PYTHONPATH(tmp_path):
 
 
 def test_getsitepackages_system_site(tmp_path):
-    import site
-
-    old_prefixes = site.PREFIXES
-    site.PREFIXES = [sys.base_prefix, sys.base_exec_prefix]
-    system_site_packages = site.getsitepackages()
-    site.PREFIXES = old_prefixes
-
     # Test without --system-site-packages
     session = cli_run([ensure_text(str(tmp_path))])
+
+    system_site_packages = get_expected_system_site_packages(session)
+
     out = subprocess.check_output(
         [str(session.creator.exe), "-c", r"import site; print(site.getsitepackages())"],
         universal_newlines=True,
@@ -645,6 +642,9 @@ def test_getsitepackages_system_site(tmp_path):
 
     # Test with --system-site-packages
     session = cli_run([ensure_text(str(tmp_path)), "--system-site-packages"])
+
+    system_site_packages = get_expected_system_site_packages(session)
+
     out = subprocess.check_output(
         [str(session.creator.exe), "-c", r"import site; print(site.getsitepackages())"],
         universal_newlines=True,
@@ -653,3 +653,32 @@ def test_getsitepackages_system_site(tmp_path):
 
     for system_site_package in system_site_packages:
         assert system_site_package in site_packages
+
+
+def get_expected_system_site_packages(session):
+    base_prefix = session.creator.pyenv_cfg["base-prefix"]
+    base_exec_prefix = session.creator.pyenv_cfg["base-exec-prefix"]
+    old_prefixes = site.PREFIXES
+    site.PREFIXES = [base_prefix, base_exec_prefix]
+    system_site_packages = site.getsitepackages()
+    site.PREFIXES = old_prefixes
+
+    return system_site_packages
+
+
+def test_get_site_packages(tmp_path):
+    case_sensitive = fs_is_case_sensitive()
+    session = cli_run([ensure_text(str(tmp_path))])
+    env_site_packages = [str(session.creator.purelib), str(session.creator.platlib)]
+    out = subprocess.check_output(
+        [str(session.creator.exe), "-c", r"import site; print(site.getsitepackages())"],
+        universal_newlines=True,
+    )
+    site_packages = ast.literal_eval(out)
+
+    if not case_sensitive:
+        env_site_packages = [x.lower() for x in env_site_packages]
+        site_packages = [x.lower() for x in site_packages]
+
+    for env_site_package in env_site_packages:
+        assert env_site_package in site_packages