Skip to content
Merged
5 changes: 3 additions & 2 deletions Lib/test/support/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1716,9 +1716,10 @@ def _platform_specific(self):
))

self._env = {k.upper(): os.getenv(k) for k in os.environ}
self._env["PYTHONHOME"] = os.path.dirname(self.real)
home = os.path.dirname(self.real)
if sysconfig.is_python_build():
self._env["PYTHONPATH"] = STDLIB_DIR
home = os.path.join(home, sysconfig.get_config_var('VPATH'))
self._env["PYTHONHOME"] = home
else:
def _platform_specific(self):
pass
Expand Down
13 changes: 13 additions & 0 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -1479,6 +1479,10 @@ def test_init_setpythonhome(self):
stdlib = os.path.join(home, sys.platlibdir, f'python{version}{ABI_THREAD}')
expected_paths = self.module_search_paths(prefix=home, exec_prefix=home)

# Create the expected paths to avoid the bad stdlib dir warning
for entry in expected_paths:
os.makedirs(entry, exist_ok=True)

config = {
'home': home,
'module_search_paths': expected_paths,
Expand Down Expand Up @@ -1520,6 +1524,10 @@ def test_init_is_python_build_with_home(self):
stdlib = os.path.join(home, sys.platlibdir, f'python{version}{ABI_THREAD}')
expected_paths = self.module_search_paths(prefix=home, exec_prefix=home)

# Create the expected paths to avoid the bad stdlib dir warning
for entry in expected_paths:
os.makedirs(entry, exist_ok=True)

config = {
'home': home,
'module_search_paths': expected_paths,
Expand Down Expand Up @@ -1575,6 +1583,8 @@ def test_init_pybuilddir(self):
# The stdlib dir is dirname(executable) + VPATH + 'Lib'
stdlibdir = os.path.normpath(os.path.join(tmpdir, vpath, 'Lib'))
os.mkdir(libdir)
# Create the directory to avoid the bad stdlib dir warning
os.makedirs(stdlibdir)

filename = os.path.join(tmpdir, 'pybuilddir.txt')
with open(filename, "w", encoding="utf8") as fp:
Expand Down Expand Up @@ -1613,6 +1623,9 @@ def test_init_pybuilddir_win32(self):
# The stdlib dir is dirname(executable) + VPATH + 'Lib'
stdlibdir = os.path.normpath(os.path.join(tmpdir, vpath, 'Lib'))

# Create the directory to avoid the bad stdlib dir warning
os.makedirs(stdlibdir)

filename = os.path.join(tmpdir, 'pybuilddir.txt')
with open(filename, "w", encoding="utf8") as fp:
fp.write(tmpdir)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
A warning is now shown during :ref:`sys-path-init` if it can't find a valid
standard library.
23 changes: 20 additions & 3 deletions Modules/getpath.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ def search_up(prefix, *landmarks, test=isfile):

real_executable_dir = None
platstdlib_dir = None
stdlib_zip = None

# ******************************************************************************
# CALCULATE program_name
Expand Down Expand Up @@ -697,12 +698,13 @@ def search_up(prefix, *landmarks, test=isfile):
library_dir = dirname(library)
else:
library_dir = executable_dir
pythonpath.append(joinpath(library_dir, ZIP_LANDMARK))
stdlib_zip = joinpath(library_dir, ZIP_LANDMARK)
elif build_prefix:
# QUIRK: POSIX uses the default prefix when in the build directory
pythonpath.append(joinpath(PREFIX, ZIP_LANDMARK))
stdlib_zip = joinpath(PREFIX, ZIP_LANDMARK)
else:
pythonpath.append(joinpath(base_prefix, ZIP_LANDMARK))
stdlib_zip = joinpath(base_prefix, ZIP_LANDMARK)
pythonpath.append(stdlib_zip)

if os_name == 'nt' and use_environment and winreg:
# QUIRK: Windows also lists paths in the registry. Paths are stored
Expand Down Expand Up @@ -767,6 +769,21 @@ def search_up(prefix, *landmarks, test=isfile):
config['module_search_paths_set'] = 1


# ******************************************************************************
# SANITY CHECKS
# ******************************************************************************

# Warn if the standard library is missing
if not stdlib_zip or not isfile(stdlib_zip):
home_hint = f"The Python 'home' directory was set to {home!r}, is this correct?"
if not stdlib_dir or not isdir(stdlib_dir):
hint = home_hint if home else f'sys.prefix is set to {prefix}, is this correct?'
warn('WARN: Could not find the standard library directory! ' + hint)
elif (not platstdlib_dir and not build_prefix) or not isdir(platstdlib_dir):
Copy link
Copy Markdown
Member Author

@FFY00 FFY00 Feb 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the tests, but unless we skip the platstdlib_dir check when running from source, the warning will show up on make, during the pybuilddir.txt generation.

I would prefer to drop the build_prefix check, and instead add -X pathconfig_warnings=0 to the -m sysconfig --generate-posix-vars call in the makefile.

Suggested change
elif (not platstdlib_dir and not build_prefix) or not isdir(platstdlib_dir):
elif not platstdlib_dir or not isdir(platstdlib_dir):

hint = home_hint if home else f'sys.exec_prefix is set to {exec_prefix}, is this correct?'
warn('WARN: Could not find the platform standard library directory! ' + hint)


# ******************************************************************************
# POSIX prefix/exec_prefix QUIRKS
# ******************************************************************************
Expand Down
Loading