Feature
Mypyc should set __file__
Pitch
Getting installation directory is important when referencing data files included in your package. Things like this are common:
DATA_FOLDER: Final = Path(__file__).parent / "data"
Currently, mypyc will compile this with no warnings about possible issues and crash with
Traceback (most recent call last): <unrelated details> File "src/checkers/game.py", line 108, in <module> DATA_FOLDER: Final = Path(__file__).parent / "data" KeyError: '__file__'
There is a workaround for this, doing something like this before hand
if globals().get("__file__") is None: import inspect __file__ = inspect.stack()[0].filename
but that's messy
This is very inconvenient, because __file__ is so commonly used, all my logging modules has this line:
python -m status.cp313-win_amd64.pyd Traceback (most recent call last): File "<frozen runpy>", line 189, in _run_module_as_main File "<frozen runpy>", line 112, in _get_module_details File "bin\status.py", line 16, in <module> log = logging.getLogger("root.bin." + os.path.basename(__file__).split('.')[0]) KeyError: '__file__'
Fwiw, I started to look into implementing this and it appears to have already been done by the PR for #21101
I believe this can be closed @p-sawicki
Blog post doesn't mention __file__ is fixed, only that macos imports should be faster in the 1.20.0 release notes, but just tested it and yes __file__ is set properly now.
Activity
fenchu commented on Aug 7, 2025
This is very inconvenient, because __file__ is so commonly used, all my logging modules has this line:
python -m status.cp313-win_amd64.pyd Traceback (most recent call last): File "<frozen runpy>", line 189, in _run_module_as_main File "<frozen runpy>", line 112, in _get_module_details File "bin\status.py", line 16, in <module> log = logging.getLogger("root.bin." + os.path.basename(__file__).split('.')[0]) KeyError: '__file__'rheard commented on Jun 1, 2026
Fwiw, I started to look into implementing this and it appears to have already been done by the PR for #21101
I believe this can be closed @p-sawicki
CoolCat467 commented on Jun 1, 2026
Blog post doesn't mention __file__ is fixed, only that macos imports should be faster in the 1.20.0 release notes, but just tested it and yes __file__ is set properly now.