From 26f2a178e378442f9aaa7785a583f9f3c9e98a5b Mon Sep 17 00:00:00 2001 From: Frost Ming Date: Tue, 15 Nov 2022 16:03:01 +0800 Subject: [PATCH] fix: fix the module loading of pth files for install cache Fix #863 --- news/863.bugfix.md | 1 + src/pdm/installers/installers.py | 11 ++++++----- 2 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 news/863.bugfix.md diff --git a/news/863.bugfix.md b/news/863.bugfix.md new file mode 100644 index 0000000000..a5d255bddf --- /dev/null +++ b/news/863.bugfix.md @@ -0,0 +1 @@ +Fix the module missing error of pywin32 in a virtualenv with `install.cache` set to `true` and caching method is `pth`. diff --git a/src/pdm/installers/installers.py b/src/pdm/installers/installers.py index d5d4376846..f8063a7edf 100644 --- a/src/pdm/installers/installers.py +++ b/src/pdm/installers/installers.py @@ -199,7 +199,7 @@ def install_wheel_with_cache( package_cache = CachedPackage(cache_path) interpreter = str(environment.interpreter.executable) script_kind = _get_kind(environment) - supports_symlink = ( + use_symlink = ( environment.project.config["install.cache_method"] == "symlink" and fs_supports_symlink() ) @@ -228,17 +228,18 @@ def skip_files(source: WheelFile, element: WheelContentElement) -> bool: or path.split("/")[0].endswith(".dist-info") # We need to skip the *-nspkg.pth files generated by setuptools' # namespace_packages merchanism. See issue#623 for details - or not supports_symlink + or not use_symlink and path.endswith(".pth") and not path.endswith("-nspkg.pth") ) additional_contents: list[WheelContentElement] = [] lib_path = package_cache.scheme()["purelib"] - if not supports_symlink: + if not use_symlink: # HACK: Prefix with aaa_ to make it processed as early as possible filename = "aaa_" + wheel_stem.split("-")[0] + ".pth" - stream = io.BytesIO(f"{lib_path}\n".encode()) + # use site.addsitedir() rather than a plain path to properly process .pth files + stream = io.BytesIO(f"import site;site.addsitedir({lib_path!r})\n".encode()) additional_contents.append( ((filename, "", str(len(stream.getvalue()))), stream, False) ) @@ -247,7 +248,7 @@ def skip_files(source: WheelFile, element: WheelContentElement) -> bool: scheme_dict=environment.get_paths(), interpreter=interpreter, script_kind=script_kind, - symlink_to=lib_path if supports_symlink else None, + symlink_to=lib_path if use_symlink else None, ) dist_info_dir = _install_wheel(