diff --git a/Lib/test/test_embed.py b/Lib/test/test_embed.py
index 94161b651ff..8012d80bf3e 100644
--- a/Lib/test/test_embed.py
+++ b/Lib/test/test_embed.py
@@ -1362,6 +1362,8 @@ def test_init_pyvenv_cfg(self):
if not MS_WINDOWS:
paths[-1] = lib_dynload
else:
+ # Include DLLs directory as well
+ paths.insert(1, '.\\DLLs')
for index, path in enumerate(paths):
if index == 0:
# Because we copy the DLLs into tmpdir as well, the zip file
diff --git a/Lib/test/test_getpath.py b/Lib/test/test_getpath.py
index 9dd167b9975..3fb1b280031 100644
--- a/Lib/test/test_getpath.py
+++ b/Lib/test/test_getpath.py
@@ -100,6 +100,7 @@ def test_venv_win32(self):
module_search_paths_set=1,
module_search_paths=[
r"C:\Python\python98.zip",
+ r"C:\Python\DLLs",
r"C:\Python\Lib",
r"C:\Python",
],
diff --git a/Misc/NEWS.d/next/Windows/2021-12-08-16-36-20.bpo-46105.t1mJ6Q.rst b/Misc/NEWS.d/next/Windows/2021-12-08-16-36-20.bpo-46105.t1mJ6Q.rst
new file mode 100644
index 00000000000..bfb6d31f7b6
--- /dev/null
+++ b/Misc/NEWS.d/next/Windows/2021-12-08-16-36-20.bpo-46105.t1mJ6Q.rst
@@ -0,0 +1 @@
+Fixed calculation of :data:`sys.path` in a venv on Windows.
diff --git a/Modules/getpath.c b/Modules/getpath.c
index 0b982f10e41..fedb41cdb37 100644
--- a/Modules/getpath.c
+++ b/Modules/getpath.c
@@ -390,7 +390,7 @@ getpath_readlines(PyObject *Py_UNUSED(self), PyObject *args)
while (cb && (p1[cb] == L'\n' || p1[cb] == L'\r')) {
--cb;
}
- PyObject *u = PyUnicode_FromWideChar(p1, cb + 1);
+ PyObject *u = PyUnicode_FromWideChar(p1, cb ? cb + 1 : 0);
if (!u || PyList_Append(r, u) < 0) {
Py_XDECREF(u);
Py_CLEAR(r);
diff --git a/Modules/getpath.py b/Modules/getpath.py
index 6a13e232ec3..84c9760b112 100644
--- a/Modules/getpath.py
+++ b/Modules/getpath.py
@@ -668,14 +668,17 @@ def search_up(prefix, *landmarks, test=isfile):
pythonpath.append(joinpath(prefix, p))
# Then add stdlib_dir and platstdlib_dir
- if stdlib_dir:
- pythonpath.append(stdlib_dir)
- if platstdlib_dir:
- if os_name == 'nt' and venv_prefix:
- # QUIRK: Windows appends executable_dir instead of platstdlib_dir
- # when in a venv
- pythonpath.append(executable_dir)
- else:
+ if os_name == 'nt' and venv_prefix:
+ # QUIRK: Windows generates paths differently in a venv
+ if platstdlib_dir:
+ pythonpath.append(platstdlib_dir)
+ if stdlib_dir:
+ pythonpath.append(stdlib_dir)
+ pythonpath.append(executable_dir)
+ else:
+ if stdlib_dir:
+ pythonpath.append(stdlib_dir)
+ if platstdlib_dir:
pythonpath.append(platstdlib_dir)
config['module_search_paths'] = pythonpath
diff --git a/PCbuild/python.vcxproj b/PCbuild/python.vcxproj
index f4451370287..77bccde69e3 100644
--- a/PCbuild/python.vcxproj
+++ b/PCbuild/python.vcxproj
@@ -117,6 +117,10 @@
+
+
+
+
ucrtbase
@@ -147,7 +151,4 @@ $(_PGOPath)
-
-
-