Fix ModuleNotFoundError for __main__ when run from entry points#1072
Open
worksbyfriday wants to merge 2 commits intoagronholm:masterfrom
Open
Fix ModuleNotFoundError for __main__ when run from entry points#1072worksbyfriday wants to merge 2 commits intoagronholm:masterfrom
worksbyfriday wants to merge 2 commits intoagronholm:masterfrom
Conversation
When to_process.run_sync() is called from a package entry point (console_scripts), the worker's __main__.__file__ points to a script without a .py extension. spec_from_file_location returns None for such paths, leaving __main__ deleted from sys.modules and causing ModuleNotFoundError when libraries like dill try to import it. Create a placeholder __main__ module when spec_from_file_location fails to resolve the entry point path, matching multiprocessing's approach. Fixes agronholm#1027
for more information, see https://pre-commit.ci
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes
ModuleNotFoundError: No module named '__main__'whento_process.run_sync()is called from a package entry point (console_scripts).Fixes #1027
Root cause
When
__main__.__file__points to an entry point script without a.pyextension,spec_from_file_locationreturnsNone. The worker's init code deletessys.modules["__main__"](line 233) but never restores it, because the spec guard at line 239 skips module loading. Libraries likedillthat subsequently tryimport __main__getModuleNotFoundError.Fix
After the init block, if
"__main__"is no longer insys.modules, create a placeholdertypes.ModuleType("__main__"). This matchesmultiprocessing's approach for handling non-importable main modules.Test plan
test_entrypoint_main_module: sets__main__.__file__to a real file without.pyextension, verifies worker can still access__main__viasys.modulestest_to_process.pytests pass (10 skipped for uvloop)