You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
to_process.run_sync fails to set up __main__ in worker process when run from an entry point #1027
I have searched the existing issues and didn't find my bug already reported there
I have checked that my bug is still present in the latest release
AnyIO version
4.11
Python version
3.11
What happened?
I have a package with an entry point defined in pyproject.toml and I want to run a function in a worker process. The function imports dill, which raises
I've tried to debug this a bit, and it turns out that importlib.util.spec_from_file_location (used in the worker process to set up __main__) always returns None if the path does not end in .py or .pyc.
sys.modules["__main__"].__spec__ is also None in that case.
How can we reproduce the bug?
Write a small dummy package with an entry point defined in pyproject.toml that runs a function in a worker process.
As noted above this is due to the entry-point scripts not ending in .py and therefore will not be found by the normal loaders.
This is only one case this can happen with executable scripts run as direct arguments to the python interpreter.
Activity
Fix ModuleNotFoundError for __main__ when run from entry points
tapetersen commentedon May 5, 2026
As noted above this is due to the entry-point scripts not ending in
.pyand therefore will not be found by the normal loaders.This is only one case this can happen with executable scripts run as direct arguments to the python interpreter.
@agronholm Relevant code:
anyio/src/anyio/to_process.py
Lines 235 to 244 in f821c37
runpy.run_pathinstead which doesn't have this problem.https://github.com/python/cpython/blob/4fa5c0428213f9b80a2b3e25e49b52e8596d8528/Lib/multiprocessing/spawn.py#L295-L300
Any reason that couldn't be used here? (I've tested and preliminary it seems to pass everything)
Edit: PR up at #1134
Ensure __main__ module is importable in ``to_process`` when not endin…