Skip to content

Commit ddc900e

Browse files
authored
Merge pull request #3117 from pygame-community/ankith26-fix-editable
Fix editable install on windows
2 parents f554e77 + 98170b3 commit ddc900e

File tree

4 files changed

+47
-7
lines changed

4 files changed

+47
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ dist
4444
*.so
4545
__pycache__
4646
_headers/*
47+
buildconfig/win_dll_dirs.json
4748

4849
# cython generated files
4950
src_c/_sdl2/*.c

buildconfig/_meson_win_dll.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""
2+
A helper file invoked by the meson buildconfig to write DLL paths to a json file
3+
"""
4+
5+
import json
6+
import sys
7+
8+
from pathlib import Path
9+
10+
dll_parents = {str(Path(i).parent) for i in sys.argv[1:]}
11+
win_dll_dirs_file = Path(__file__).parent / "win_dll_dirs.json"
12+
13+
if __name__ == "__main__":
14+
win_dll_dirs_file.write_text(json.dumps(list(dll_parents)), encoding="utf-8")

meson.build

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ if plat == 'win' and host_machine.cpu_family().startswith('x86')
167167
)
168168
endif
169169

170+
run_command(
171+
[
172+
find_program('python3', 'python'),
173+
base_dir / 'buildconfig' / '_meson_win_dll.py',
174+
dlls,
175+
],
176+
check: true,
177+
)
178+
170179
# put dlls in root of install
171180
install_data(dlls, install_dir: pg_dir, install_tag: 'pg-tag')
172181
else

src_py/__init__.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,32 @@
3030
# Choose Windows display driver
3131
if os.name == "nt":
3232
pygame_dir = os.path.split(__file__)[0]
33+
dll_parents = {pygame_dir}
34+
try:
35+
# For editable support, add some more folders where DLLs are available.
36+
# This block only executes under an editable install. In a "normal"
37+
# install, the json file will not be installed at the supplied path.
38+
with open(
39+
os.path.join(
40+
os.path.dirname(pygame_dir), "buildconfig", "win_dll_dirs.json"
41+
),
42+
encoding="utf-8",
43+
) as f:
44+
import json
45+
46+
dll_parents.update(json.load(f))
47+
del json
48+
except (FileNotFoundError, ValueError):
49+
pass
3350

34-
# pypy does not find the dlls, so we add package folder to PATH.
35-
os.environ["PATH"] = os.environ["PATH"] + ";" + pygame_dir
36-
37-
# Windows store python does not find the dlls, so we run this
38-
if sys.version_info > (3, 8):
39-
os.add_dll_directory(pygame_dir) # only available in 3.8+
51+
for d in dll_parents:
52+
# adding to PATH is the legacy way, os.add_dll_directory is the new
53+
# and recommended method. For extra safety we do both
54+
os.environ["PATH"] = os.environ["PATH"] + ";" + d
55+
os.add_dll_directory(d)
4056

4157
# cleanup namespace
42-
del pygame_dir
58+
del pygame_dir, dll_parents
4359

4460
# when running under X11, always set the SDL window WM_CLASS to make the
4561
# window managers correctly match the pygame window.

0 commit comments

Comments
 (0)