-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-121607: Edited source file import recipe to make it more clear #121519
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
08917bd
edited source file import recipe to make it more clear
ChrisBarker-NOAA bab2fa8
added a note about deprecation of the load_module() method.
ChrisBarker-NOAA 81885ad
Update Doc/library/importlib.rst
ChrisBarker-NOAA 7905ab0
Update Doc/library/importlib.rst
ChrisBarker-NOAA c78a1a6
removed the note directive.
ChrisBarker-NOAA b9cb5d3
Update importlib.rst
ChrisBarker-NOAA 73e4c94
added intro paragraph to Importing a source file directly recipe with…
ChrisBarker-NOAA fed1797
Update Doc/library/importlib.rst
ChrisBarker-NOAA e5a864e
Update Doc/library/importlib.rst
ChrisBarker-NOAA e6a1965
Wrap lines to 80 columns
brettcannon 5a74186
Merge branch 'main' into importlib_docs
brettcannon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why the extra note? The method isn't used here and it's documented as deprecated, so why call it out here and add a visible callout in the rendered docs that some might find distracting?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did that because the advice to use
SourceFileLoader.load_module()is found all over the internet (e.g. https://krbnite.github.io/How-to-Import-a-Python-Module-from-an-Arbitrary-Path/)-- and it seems a lot simpler than this recipe, and it doesn't raise a deprecation error. so I thought it would be helpful to make it clear why the more complex recipe is preferred.
I removed the
notedirective to make it less distracting.That being said, while it might be helpful now, it'll be clutter in the future -- so your call if you want to simply remove it.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think having a lead-in paragraph makes sense, but I think it's pointing at the wrong alternative: the reason importing an arbitrary path is a bad idea is because it's a good way to confuse the import system, and that's why the stdlib's only shorthand API for doing that is deprecated without a straightforward replacement.
The example given uses a top level module/package, so it's potentially OK (although I'm not 100% certain without checking the code for
module_from_spec- if that bypassessys.modules, you might end up with two different versions ofJSONDecodeErrorkicking around, which can easily land you in exception handling hell), but if the file is part of an already importable package than you're far more likely to get some weird state duplication going on (it's a good way to get yourself caught in the "double import trap").The much safer alternative for executing a Python file and getting access to its top-level module state is to use
runpy.run_path(which returns the global namespace that results from executing the file rather than creating a module object).In my experience, folks wanting to import from arbitrary paths is usually a classic case of the "XY problem": "X" is "I want to run a Python file from a Python program and get access to the resulting global variables", which becomes the "Y" question of "How do I import a Python module given only its path name?". With
importbeing a statement, and theexecfilebuiltin ancient history, it's an understandable leap for people to make, but that doesn't mean it's correct when there's no actual need to create a module object at all (let alone register it with the import system).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... yet. 😉 #121604
So is probably outdated Python 2 advice, but that doesn't mean we need to call it out. 😉
I agree. I think it would be better to have a sentence or paragraph pointing out that the recipe is an approximation of an
importstatement where you specify the file path. It should also point out that modifyingsys.pathmay be better than circumventing the import system or usingrunpy.run_path()if you just need the global namespace and not a module object.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, this is already in the docs! All this PR does is clean up the snippet a little bit.