diff --git a/aoc.py b/aoc.py index 835743d..d12d219 100644 --- a/aoc.py +++ b/aoc.py @@ -1,7 +1,8 @@ import os +import tools from typing import List, Any, Type -BASE_PATH = os.path.dirname(os.path.dirname(__file__)) +BASE_PATH = tools.get_script_dir() INPUTS_PATH = os.path.join(BASE_PATH, 'inputs') diff --git a/tools.py b/tools.py new file mode 100644 index 0000000..8ee2e30 --- /dev/null +++ b/tools.py @@ -0,0 +1,18 @@ +import inspect +import os.path +import sys + + +def get_script_dir(follow_symlinks=True): + if getattr(sys, 'frozen', False): + path = os.path.abspath(sys.executable) + else: + if '__main__' in sys.modules and hasattr(sys.modules['__main__'], '__file__'): + path = sys.modules['__main__'].__file__ + else: + path = inspect.getabsfile(get_script_dir) + + if follow_symlinks: + path = os.path.realpath(path) + + return os.path.dirname(path)