Load an IPython extension by its module name. Returns the string "already loaded" if the extension is already loaded, "no load function" if the module doesn't have a load_ipython_extension function, or None if it succeeded.
(self, module_str)
| 63 | ensure_dir_exists(self.ipython_extension_dir) |
| 64 | |
| 65 | def load_extension(self, module_str): |
| 66 | """Load an IPython extension by its module name. |
| 67 | |
| 68 | Returns the string "already loaded" if the extension is already loaded, |
| 69 | "no load function" if the module doesn't have a load_ipython_extension |
| 70 | function, or None if it succeeded. |
| 71 | """ |
| 72 | if module_str in self.loaded: |
| 73 | return "already loaded" |
| 74 | |
| 75 | from IPython.utils.syspathcontext import prepended_to_syspath |
| 76 | |
| 77 | with self.shell.builtin_trap: |
| 78 | if module_str not in sys.modules: |
| 79 | with prepended_to_syspath(self.ipython_extension_dir): |
| 80 | mod = import_module(module_str) |
| 81 | if mod.__file__.startswith(self.ipython_extension_dir): |
| 82 | print(("Loading extensions from {dir} is deprecated. " |
| 83 | "We recommend managing extensions like any " |
| 84 | "other Python packages, in site-packages.").format( |
| 85 | dir=compress_user(self.ipython_extension_dir))) |
| 86 | mod = sys.modules[module_str] |
| 87 | if self._call_load_ipython_extension(mod): |
| 88 | self.loaded.add(module_str) |
| 89 | else: |
| 90 | return "no load function" |
| 91 | |
| 92 | def unload_extension(self, module_str): |
| 93 | """Unload an IPython extension by its module name. |