diff --git a/Lib/site.py b/Lib/site.py index 64e8192a9ac81a..708a70a464a0ee 100644 --- a/Lib/site.py +++ b/Lib/site.py @@ -490,13 +490,16 @@ def addsitedir(sitedir, known_paths=None, *, defer_processing_start_files=False) reset = False sitedir, sitedircase = makepath(sitedir) - # If the normcase'd new sitedir isn't already known, append it to - # sys.path, keep a record of it, and process all .pth and .start files - # found in that directory. If the new sitedir is known, be sure not - # to process all of those more than once! gh-75723 + # If the normcase'd new sitedir isn't already known, record it to + # prevent re-processing, append it to sys.path (only if not already + # present), and process all .pth and .start files found in that + # directory. Use a direct sys.path membership check for the append + # guard so that callers (like main()) can pass a fresh known_paths + # set while avoiding duplicate sys.path entries (gh-149819). if sitedircase not in known_paths: - sys.path.append(sitedir) known_paths.add(sitedircase) + if sitedir not in sys.path: + sys.path.append(sitedir) try: names = os.listdir(sitedir) @@ -1000,7 +1003,8 @@ def main(): global ENABLE_USER_SITE orig_path = sys.path[:] - known_paths = removeduppaths() + removeduppaths() + known_paths = set() if orig_path != sys.path: # removeduppaths() might make sys.path absolute. # Fix __file__ of already imported modules too. diff --git a/Misc/NEWS.d/next/Library/2026-05-15-16-28-00.gh-issue-149819.fixpth.rst b/Misc/NEWS.d/next/Library/2026-05-15-16-28-00.gh-issue-149819.fixpth.rst new file mode 100644 index 00000000000000..66e6da0ecf0d87 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-05-15-16-28-00.gh-issue-149819.fixpth.rst @@ -0,0 +1,4 @@ +Fix regression in :func:`site.addsitedir` where ``.pth`` files were no +longer processed in Python subprocesses. This happened because +:func:`site.main` seeded ``known_paths`` with entries inherited from +the parent process, causing ``addsitedir`` to skip ``.pth`` processing.