Skip to content

Commit 9a85a0a

Browse files
committed
Fix library search paths not working when used from REPL
1 parent 11b5caa commit 9a85a0a

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

raylibpy/__init__.py

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sys
22
import os
33
import colorsys
4+
from pathlib import Path
45
from math import modf
56
from enum import IntEnum, IntFlag
67
from typing import Tuple, List, Union, Sequence, AnyStr, Optional, Iterator, Type
@@ -42,34 +43,32 @@
4243
if "ENABLE_V2_0_0_FEATURE_CLIPRECT" in os.environ:
4344
ENABLE_V2_0_0_FEATURE_CLIPRECT = True
4445

45-
RAYLIB_BIN_PATH = None
46-
if "RAYLIB_BIN_PATH" in os.environ:
47-
env_path = os.environ['RAYLIB_BIN_PATH']
48-
if env_path == '__main__':
49-
RAYLIB_BIN_PATH = os.path.dirname(sys.modules['__main__'].__file__)
50-
elif env_path == '__file__':
51-
RAYLIB_BIN_PATH = os.path.abspath(os.path.dirname(__file__))
52-
elif os.path.exists(env_path) and os.path.isdir(env_path):
53-
RAYLIB_BIN_PATH = env_path
54-
else:
55-
first_path = os.path.abspath(os.path.dirname(__file__))
56-
second_path = os.path.dirname(sys.modules['__main__'].__file__)
57-
if os.path.exists(os.path.join(first_path, _lib_filename[_platform])):
58-
RAYLIB_BIN_PATH = first_path
59-
elif os.path.exists(os.path.join(second_path, _lib_filename[_platform])):
60-
RAYLIB_BIN_PATH = second_path
61-
else:
62-
s = ("'{}' is expected to be located\n"
63-
"in the directory specified by the environment variable\n"
64-
"RAYLIB_BIN_PATH, in the program's entry point (__main__)\n"
65-
"directory, or in the raylibpy package __init__ directory.\n"
66-
"The library file is not in any of these locations.")
67-
print(s.format(_lib_filename[_platform]))
68-
raise RuntimeError("Unable to find raylib library ('{}').".format(_lib_filename[_platform]))
46+
lib_name = _lib_filename[_platform]
47+
main_mod = sys.modules['__main__']
48+
running_from_repl = '__file__' not in dir(main_mod)
49+
50+
env_path = Path(os.environ['RAYLIB_BIN_PATH']) if 'RAYLIB_BIN_PATH' in os.environ else None
51+
file_path = Path(__file__).parent
52+
main_path = Path(main_mod.__file__).parent if not running_from_repl else Path('REPL')
6953

70-
if RAYLIB_BIN_PATH:
71-
_rl = CDLL(os.path.join(RAYLIB_BIN_PATH, _lib_filename[_platform]))
54+
if env_path and env_path.exists():
55+
RAYLIB_BIN_PATH = env_path
7256

57+
elif (file_path / lib_name).exists():
58+
RAYLIB_BIN_PATH = file_path
59+
60+
elif (main_path / lib_name).exists():
61+
RAYLIB_BIN_PATH = main_path
62+
else:
63+
raise Exception(
64+
f'Cannot find "{lib_name}" in these search paths:\n'
65+
f'__file__ folder: "{str(file_path)}"\n'
66+
f'__main__ folder: "{str(main_path)}"\n'
67+
f'os.environ["RAYLIB_BIN_PATH"] -> "{str(env_path) if env_path else "NOT SET"}"'
68+
)
69+
70+
print(f'INFO: Found "{lib_name}" in "{str(RAYLIB_BIN_PATH)}"')
71+
_rl = CDLL(str(RAYLIB_BIN_PATH / lib_name))
7372

7473
__all__ = [
7574
# CONSTANTS

0 commit comments

Comments
 (0)