Skip to content

gh-120037: Disable user site packages when a ._pth file is used #137428

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 19, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Doc/c-api/init_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2258,6 +2258,7 @@ If a ``._pth`` file is present:
* Set :c:member:`~PyConfig.isolated` to ``1``.
* Set :c:member:`~PyConfig.use_environment` to ``0``.
* Set :c:member:`~PyConfig.site_import` to ``0``.
* Set :c:member:`~PyConfig.user_site_directory` to ``0`` (since 3.15).
* Set :c:member:`~PyConfig.safe_path` to ``1``.

If :c:member:`~PyConfig.home` is not set and a ``pyvenv.cfg`` file is present in
Expand All @@ -2278,6 +2279,12 @@ The ``__PYVENV_LAUNCHER__`` environment variable is used to set
therefore affected by :option:`-S`.


.. versionchanged:: 3.15

:c:member:`~PyConfig.user_site_directory` is now set to ``0`` when a
``._pth`` file is present.


Py_GetArgcArgv()
================

Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,15 @@ def test_underpth_dll_file(self):
)], env=env)
self.assertTrue(rc, "sys.path is incorrect")

@support.requires_subprocess()
def test_underpth_no_user_site(self):
pth_lines = [test.support.STDLIB_DIR, 'import site']
exe_file = self._create_underpth_exe(pth_lines)
p = subprocess.run([exe_file, '-X', 'utf8', '-c',
'import sys; '
'sys.exit(not sys.flags.no_user_site)'])
self.assertEqual(p.returncode, 0, "sys.flags.no_user_site was 0")


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Disable user site packages directory when a ``._pth`` file is used, even if
it contains ``import site``.
1 change: 1 addition & 0 deletions Modules/getpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -790,6 +790,7 @@ def search_up(prefix, *landmarks, test=isfile):
config['isolated'] = 1
config['use_environment'] = 0
config['site_import'] = 0
config['user_site_directory'] = 0
config['safe_path'] = 1
pythonpath = []
for line in pth:
Expand Down
Loading