bl-sdk / PythonSDK

Borderlands 2 PythonSDK

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

__file__ is not defined when running through pyexec

apple1417 opened this issue · comments

Basically all in the title. Demo program:

import unrealsdk
unrealsdk.Log(__file__)
>>> pyexec test.py <<<
NameError: name '__file__' is not defined

At:
  D:\SteamLibrary\steamapps\common\Borderlands 2\Binaries\Win32\Mods\\test.py(3): <module>

>>> py from Mods import test <<<
'D:\\SteamLibrary\\steamapps\\common\\Borderlands 2\\Binaries\\Win32\\Mods\\test.py'

Workaround: cause an exception and extract the file path from it. Put this code near the top of the file:

if __name__ == "__main__":
    import sys
    try:
        raise NotImplementedError
    except NotImplementedError:
        __file__ = sys.exc_info()[-1].tb_frame.f_code.co_filename  # type: ignore

Fixed in pybind 2.8, if we upgrade.

Fixed, but leaving open for documentation until released.