1

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?

1
  • I could use the mymodule.ipy by doing a "%run mymodule.ipy" and now the functions in the module is available for using in the notebooks. This servers my need. Commented Dec 7, 2012 at 0:12

1 Answer 1

3

"%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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, that's the right way to do it. You can't import it, because .ipy scripts are just scripts, not modules. But %pwd is just shorthand for os.getcwd(), so you could use that in a module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.