coveralls: ## Pull Request Test Coverage Report for [Build 120](https://coveralls.io/builds/18505798)

* **0** of **0**   changed or added relevant lines in **0** files are covered.
* **10** unchanged lines in **1** file lost coverage.
* Overall coverage decreased (**-1.2%**) to **73.93%**

---


|  Files with Coverage Reduction | New Missed Lines | % |
| :-----|--------------|--: |
| [.tox/py36/lib/python3.6/site-packages/shiv/bootstrap/__init__.py](https://coveralls.io/builds/18505798/source?filename=.tox%2Fpy36%2Flib%2Fpython3.6%2Fsite-packages%2Fshiv%2Fbootstrap%2F__init__.py#L103) | 10 | 51.56% |
<!-- | **Total:** | **10** |  | -->

|  Totals | [![Coverage Status](https://coveralls.io/builds/18505798/badge)](https://coveralls.io/builds/18505798) |
| :-- | --: |
| Change from base [Build 116](https://coveralls.io/builds/18505654): |  -1.2% |
| Covered Lines: | 190 |
| Relevant Lines: | 257 |

---
##### 💛  - [Coveralls](https://coveralls.io)

diff --git a/src/shiv/bootstrap/__init__.py b/src/shiv/bootstrap/__init__.py
index 82169bf..02488ba 100644
--- a/src/shiv/bootstrap/__init__.py
+++ b/src/shiv/bootstrap/__init__.py
@@ -83,6 +83,12 @@ def extract_site_packages(archive, target_path):
     shutil.move(str(target_path_tmp), str(target_path))
 
 
+def _first_sitedir_index():
+    for index, part in enumerate(sys.path):
+        if Path(part).stem == 'site-packages':
+            return index
+
+
 def bootstrap():
     """Actually bootstrap our shiv environment."""
 
@@ -99,18 +105,18 @@ def bootstrap():
     if not site_packages.exists() or env.force_extract:
         extract_site_packages(archive, site_packages.parent)
 
-    preserved = sys.path[1:]
+    # get sys.path's length
+    length = len(sys.path)
 
-    # truncate the sys.path so our package will be at the start,
-    # and take precedence over anything else (eg: dist-packages)
-    sys.path = sys.path[0:1]
+    # Find the first instance of an existing site-packages on sys.path
+    index = _first_sitedir_index() or length
 
     # append site-packages using the stdlib blessed way of extending path
     # so as to handle .pth files correctly
     site.addsitedir(site_packages)
 
-    # restore the previous sys.path entries after our package
-    sys.path.extend(preserved)
+    # reorder to place our site-packages before any others found
+    sys.path = sys.path[:index] + sys.path[length:] + sys.path[index:length]
 
     # do entry point import and call
     if env.entry_point is not None and env.interpreter is None:

The change to sys.path potentially overrides standard library modules.
The change in #48 makes sure that shiv packed content (packages) has the precedence over the vended Python distribution packages or things installed into system Python site-packages. Fair enough. We want to run what we packed into the shiv.

However, it does have a potentially buggy behavior. Example:
* an old deprecated 3rd party package such as *uuid* or *argparse* is pulled in transitively
* these packages are unmaintained and did not get any changes (uuid since Python 2.5, for example)
* the standard library modules have been maintained and added new APIs (uuid did)
* since shiv's site-packages are stored on the sys.path **before** the standard library path, an obsolete 3rd party backward-compatibility package (but not **forward** compatible) will override the corresponding standard library module
* if any other package uses the new APIs from the affected package (e.g., new uuid APIs), the application will break because the old 3rd party package does not have these functions/methods.

I believe this requires changes to what was done in #48 to insert *shiv* site-packages *before* any other site-packages paths, but *after* the standard library path.
diff --git a/test/test_bootstrap.py b/test/test_bootstrap.py
index 5ece54f..1f77034 100644
--- a/test/test_bootstrap.py
+++ b/test/test_bootstrap.py
@@ -12,7 +12,7 @@ import pytest
 
 from unittest import mock
 
-from shiv.bootstrap import import_string, current_zipfile, cache_path
+from shiv.bootstrap import import_string, current_zipfile, cache_path, _first_sitedir_index
 from shiv.bootstrap.environment import Environment
 
 
@@ -61,6 +61,13 @@ class TestBootstrap:
 
         assert cache_path(mock_zip, Path.cwd(), uuid) == Path.cwd() / f"test_{uuid}"
 
+    def test_first_sitedir_index(self):
+        with mock.patch.object(sys, 'path', ['site-packages', 'dir', 'dir', 'dir']):
+            assert _first_sitedir_index() == 0
+
+        with mock.patch.object(sys, 'path', []):
+            assert _first_sitedir_index() is None
+
 
 class TestEnvironment:
     def test_overrides(self):

pip install -e .[dev]
parse_log_pytest
pytest
pytest pytest-cov mypy flake8
apt-get update apt-get install -y gcc
3.6
pytest --no-header -rA --tb=line --color=no -p no:cacheprovider -W ignore::DeprecationWarning
attrs @ file:///opt/conda/conda-bld/attrs_1642510447205/work
certifi==2021.5.30
click==6.7
coverage==6.2
flake8==5.0.4
importlib-metadata==4.2.0
importlib-resources==5.4.0
iniconfig @ file:///home/linux1/recipes/ci/iniconfig_1610983019677/work
mccabe==0.7.0
more-itertools @ file:///tmp/build/80754af9/more-itertools_1637733554872/work
mypy==0.971
mypy-extensions==1.0.0
packaging @ file:///tmp/build/80754af9/packaging_1637314298585/work
pluggy @ file:///tmp/build/80754af9/pluggy_1615976315926/work
py @ file:///opt/conda/conda-bld/py_1644396412707/work
pycodestyle==2.9.1
pyflakes==2.5.0
pyparsing @ file:///tmp/build/80754af9/pyparsing_1635766073266/work
pytest==6.2.4
pytest-cov==4.0.0
-e git+https://github.com/linkedin/shiv.git@6d00b754852f4f3e79d494d7577a029ecb72c1a1#egg=shiv
toml @ file:///tmp/build/80754af9/toml_1616166611790/work
tomli==1.2.3
typed-ast==1.5.5
typing_extensions @ file:///opt/conda/conda-bld/typing_extensions_1647553014482/work
zipp @ file:///tmp/build/80754af9/zipp_1633618647012/work

name: shiv
channels:
  - defaults
  - https://repo.anaconda.com/pkgs/main
  - https://repo.anaconda.com/pkgs/r
  - conda-forge
dependencies:
  - _libgcc_mutex=0.1=main
  - _openmp_mutex=5.1=1_gnu
  - attrs=21.4.0=pyhd3eb1b0_0
  - ca-certificates=2025.2.25=h06a4308_0
  - certifi=2021.5.30=py36h06a4308_0
  - importlib_metadata=4.8.1=hd3eb1b0_0
  - iniconfig=1.1.1=pyhd3eb1b0_0
  - ld_impl_linux-64=2.40=h12ee557_0
  - libffi=3.3=he6710b0_2
  - libgcc-ng=11.2.0=h1234567_1
  - libgomp=11.2.0=h1234567_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - more-itertools=8.12.0=pyhd3eb1b0_0
  - ncurses=6.4=h6a678d5_0
  - openssl=1.1.1w=h7f8727e_0
  - packaging=21.3=pyhd3eb1b0_0
  - pip=21.2.2=py36h06a4308_0
  - pluggy=0.13.1=py36h06a4308_0
  - py=1.11.0=pyhd3eb1b0_0
  - pyparsing=3.0.4=pyhd3eb1b0_0
  - pytest=6.2.4=py36h06a4308_2
  - python=3.6.13=h12debd9_1
  - readline=8.2=h5eee18b_0
  - setuptools=58.0.4=py36h06a4308_0
  - sqlite=3.45.3=h5eee18b_0
  - tk=8.6.14=h39e8969_0
  - toml=0.10.2=pyhd3eb1b0_0
  - typing_extensions=4.1.1=pyh06a4308_0
  - wheel=0.37.1=pyhd3eb1b0_0
  - xz=5.6.4=h5eee18b_1
  - zipp=3.6.0=pyhd3eb1b0_0
  - zlib=1.2.13=h5eee18b_1
  - pip:
      - click==6.7
      - coverage==6.2
      - flake8==5.0.4
      - importlib-metadata==4.2.0
      - importlib-resources==5.4.0
      - mccabe==0.7.0
      - mypy==0.971
      - mypy-extensions==1.0.0
      - pycodestyle==2.9.1
      - pyflakes==2.5.0
      - pytest-cov==4.0.0
      - tomli==1.2.3
      - typed-ast==1.5.5
prefix: /opt/conda/envs/shiv

test/test_bootstrap.py::TestBootstrap::test_various_imports test/test_bootstrap.py::TestBootstrap::test_is_zipfile test/test_bootstrap.py::TestBootstrap::test_argv0_is_not_zipfile test/test_bootstrap.py::TestBootstrap::test_cache_path test/test_bootstrap.py::TestBootstrap::test_first_sitedir_index test/test_bootstrap.py::TestEnvironment::test_overrides test/test_bootstrap.py::TestEnvironment::test_serialize test/test_builder.py::TestBuilder::test_file_prefix[/usr/bin/python-#!/usr/bin/python\n] test/test_builder.py::TestBuilder::test_file_prefix[/usr/bin/env test/test_builder.py::TestBuilder::test_file_prefix[/some/other/path/python test/test_builder.py::TestBuilder::test_binprm_error test/test_builder.py::TestBuilder::test_create_archive test/test_builder.py::TestBuilder::test_archive_permissions test/test_cli.py::TestCLI::test_no_args test/test_cli.py::TestCLI::test_no_outfile test/test_cli.py::TestCLI::test_blacklisted_args[-t] test/test_cli.py::TestCLI::test_blacklisted_args[--target] test/test_cli.py::TestCLI::test_blacklisted_args[--editable] test/test_cli.py::TestCLI::test_blacklisted_args[-d] test/test_cli.py::TestCLI::test_blacklisted_args[--download] test/test_cli.py::TestCLI::test_blacklisted_args[--user] test/test_cli.py::TestCLI::test_blacklisted_args[--root] test/test_cli.py::TestCLI::test_blacklisted_args[--prefix] test/test_cli.py::TestCLI::test_hello_world[.] test/test_cli.py::TestCLI::test_hello_world[absolute-path] test/test_pip.py::test_clean_pip_env
src/shiv/bootstrap/__init__.py
src/shiv/bootstrap/__init__.py