Domanda

I am pretty new to ipython and still learning and liking its functionality. Trying to write a module with common tasks so users of a notebook can load my custom module and save typing common code.

Here is a sample mymodule.ipy:

    import mymodule as mymod
    mymod.func():
       wd=%pwd
       return wd

When I test it in the notebook:

    import mymodule as my
    ---------------
    ImportError        Traceback (most recent call last)
    <ipython-input-1-1036e4702cf4> in <module>()
    ----> 1 import mymodule as my

    ImportError: No module named mymodule

But when I rename the module file as mymodule.py, the module load fails with error message denoting it cannot recognize the pwd magic:

    File "mymodule.py", line 2
        wd=%pwd
           ^
    SyntaxError: invalid syntax

My question is how do I write a module with ipython magics and other ipython functionality in it which I can load from notebooks? I had the impression that I can put ipython code into a file with .ipy extension and can load it from ipython. Am I missing something?

È stato utile?

Soluzione

"%run mymodule.ipy" as pointed in my comment serves my need for now. Posting it as answer to close this question. Feel free to post more if you have a "module" approach.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top