-
-
Notifications
You must be signed in to change notification settings - Fork 33k
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
Changes from 7 commits
08917bd
bab2fa8
81885ad
7905ab0
c78a1a6
b9cb5d3
73e4c94
fed1797
e5a864e
e6a1965
5a74186
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1584,20 +1584,30 @@ Note that if ``name`` is a submodule (contains a dot), | |
Importing a source file directly | ||
'''''''''''''''''''''''''''''''' | ||
|
||
To import a Python source file directly, use the following recipe:: | ||
This recipe should be used with caution: it is an approximation of an import statement where the file path is specified directly, rather than `sys.path` being searched. | ||
Alternatives should first be considered, such as modifying `sys.path` when a proper module is required, or using `runpy.run_path()` when the global namespace resulting from running a Python file is appropriate. | ||
ChrisBarker-NOAA marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
import importlib.util | ||
import sys | ||
To import a Python source file directly from a path, use the following recipe:: | ||
|
||
import importlib.util | ||
import sys | ||
|
||
# For illustrative purposes. | ||
import tokenize | ||
file_path = tokenize.__file__ | ||
module_name = tokenize.__name__ | ||
|
||
spec = importlib.util.spec_from_file_location(module_name, file_path) | ||
module = importlib.util.module_from_spec(spec) | ||
sys.modules[module_name] = module | ||
spec.loader.exec_module(module) | ||
def import_from_path(module_name, file_path): | ||
spec = importlib.util.spec_from_file_location(module_name, file_path) | ||
module = importlib.util.module_from_spec(spec) | ||
sys.modules[module_name] = module | ||
spec.loader.exec_module(module) | ||
return module | ||
|
||
|
||
# For illustrative purposes only (use of `json` is arbitrary). | ||
import json | ||
file_path = json.__file__ | ||
module_name = json.__name__ | ||
|
||
# Similar outcome as `import json`. | ||
json = import_from_path(module_name, file_path) | ||
Comment on lines
+1609
to
+1614
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I included this example because there had been a similar one in the previous version. However, it is (always?) a bad idea to use this recipe to import a module on sys.path, particularly an stdlib one -- so maybe better to simply not provide a runnable example? I expect the code itself is clear enough on how it can be used. |
||
|
||
|
||
Implementing lazy imports | ||
|
@@ -1623,7 +1633,6 @@ The example below shows how to implement lazy imports:: | |
False | ||
|
||
|
||
|
||
Setting up an importer | ||
'''''''''''''''''''''' | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.