Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How %load_ext work in ipython

I install the package : https://github.com/jaysw/ipydb

And according to the tutorial, I should use to enable it,

$ ipython
In [1] : %load_ext ipydb

it looks like an IPython extention.

But I feel confused, that this package is not installed under my " ~/.ipython/extentions "

aaron@aarons-MacBook-Pro:~/Desktop/github/ipydb$ls ~/.ipython/extensions/
aaron@aarons-MacBook-Pro:~/Desktop/github/ipydb$ls ~/.ipython/nbextensions/
livereveal usability

So, I want to know

  • how %load_ext magic works or what happened when I type "%load_ext ipydb"

  • how ipython do the config if I just type " ipython ", which will use the default profile, but no config files under my " ~/.ipython/profile_default/ "

bellow is the default profile and one user defined profile:

aaron@aarons-MacBook-Pro:~/Desktop/github/ipydb$ls ~/.ipython/profile_default/
db             history.sqlite log            nbconfig       pid            security       startup        static
aaron@aarons-MacBook-Pro:~/Desktop/github/ipydb$ipython profile create my_profile
[ProfileCreate] Generating default config file: u'/Users/aaron/.ipython/profile_my_profile/ipython_config.py'
[ProfileCreate] Generating default config file: u'/Users/aaron/.ipython/profile_my_profile/ipython_kernel_config.py'
[ProfileCreate] Generating default config file: u'/Users/aaron/.ipython/profile_my_profile/ipython_console_config.py'
[ProfileCreate] Generating default config file: u'/Users/aaron/.ipython/profile_my_profile/ipython_qtconsole_config.py'
[ProfileCreate] Generating default config file: u'/Users/aaron/.ipython/profile_my_profile/ipython_notebook_config.py'
[ProfileCreate] Generating default config file: u'/Users/aaron/.ipython/profile_my_profile/ipython_nbconvert_config.py'
aaron@aarons-MacBook-Pro:~/Desktop/github/ipydb$ls ~/.ipython/profile_my_profile/
ipython_config.py           ipython_nbconvert_config.py log                         startup
ipython_console_config.py   ipython_notebook_config.py  pid                         static
ipython_kernel_config.py    ipython_qtconsole_config.py security
aaron@aarons-MacBook-Pro:~/Desktop/github/ipydb$

thanks,

like image 788
taotao.li Avatar asked Sep 23 '15 06:09

taotao.li


People also ask

What does %% do in Jupyter Notebook?

Both ! and % allow you to run shell commands from a Jupyter notebook. % is provided by the IPython kernel and allows you to run "magic commands", many of which include well-known shell commands. ! , provided by Jupyter, allows shell commands to be run within cells.

What is IPython extension?

An IPython extension is an importable Python module that has a couple of special functions to load and unload it. Here is a template: # myextension.py def load_ipython_extension(ipython): # The `ipython` argument is the currently active `InteractiveShell` # instance, which can be used in any way.

What is Autoreload in Jupyter Notebook?

The `%autoreload` magic command allows you to reimport packages and modules every time a cell that depends on that script is called. This allows you to update a custom script that you are working with in a seperate . py file and use those updated changes your jupyter notebook, live, having already imported said .

How do I reload IPython?

IPython extension to reload modules before executing user code. autoreload reloads modules automatically before entering the execution of code typed at the IPython prompt. The module was reloaded without reloading it explicitly, and the object imported with from foo import ... was also updated.


1 Answers

ok, finally, I just find out the logic of loading an extention in ipython:

source code is on: https://github.com/ipython/ipython/blob/master/IPython/core/extensions.py

and the core logic is :

def load_extension(self, module_str):
        """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.
        """
        if module_str in self.loaded:
            return "already loaded"

        from IPython.utils.syspathcontext import prepended_to_syspath

        with self.shell.builtin_trap:
            if module_str not in sys.modules:
                with prepended_to_syspath(self.ipython_extension_dir):
                    __import__(module_str)
            mod = sys.modules[module_str]
            if self._call_load_ipython_extension(mod):
                self.loaded.add(module_str)
            else:
                return "no load function"

that make sense now, thanks IPython team to build such an excellent tool, I really love it.

like image 197
taotao.li Avatar answered Oct 10 '22 05:10

taotao.li