-
Notifications
You must be signed in to change notification settings - Fork 31
new clawutil.util module containing fullpath_import #186
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 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e572b2f
new clawutil.util module containing fullpath_import
rjleveque ba97501
move import statement out of function
rjleveque e749ddc
switch to using pathlib in fullpath_import, so fullpath input can be …
rjleveque 95f997c
allow fullpath to start with environment variable and resolve if poss…
rjleveque 589b181
improved fullpath_import using expandvars and expanduser
rjleveque c3be67e
change default verbose to False
rjleveque 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| r""" | ||
| clawutil.util Module `$CLAW/clawutil/src/python/clawutil/util.py` | ||
|
|
||
| Provides general utility functions. | ||
|
|
||
| :Functions: | ||
|
|
||
| - fullpath_import: import a module using its full path | ||
|
|
||
| """ | ||
|
|
||
| def fullpath_import(fullpath, verbose=True): | ||
| """ | ||
| Return a module imported from a full path name, e.g. if you have | ||
| a personal enhanced version of the geoclaw topotools module at | ||
| /full/path/to/topotools.py then instead of: | ||
|
|
||
| from clawpack.geoclaw import topotools | ||
|
|
||
| use: | ||
|
|
||
| from clawpack.clawutil.util import fullpath_import | ||
| topotools = fullpath_import('/full/path/to/topotools.py') | ||
|
|
||
| Relative imports also work, e.g. | ||
|
|
||
| topotools = fullpath_import('../topotools.py') | ||
|
|
||
| To reload the module if you make changes to it, use this function again | ||
| (rather than using importlib.reload). | ||
| """ | ||
|
|
||
| import os, sys, importlib | ||
| fname = os.path.split(fullpath)[1] | ||
| modname = os.path.splitext(fname)[0] | ||
mandli marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| spec = importlib.util.spec_from_file_location(modname, fullpath) | ||
| module = importlib.util.module_from_spec(spec) | ||
| spec.loader.exec_module(module) | ||
| sys.modules[modname] = module | ||
| if verbose: | ||
| print('loaded module from file: ',module.__file__) | ||
| return module | ||
|
|
||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.