Interactive Python (ipython) is simply amazing, especially as you are piecing things together on the fly... and does it in such a way that it is easy to go back.
However, what seems to be interesting is the use-case of having multiple ipython notebooks (ipynb files). It apparently seems like a notebook is NOT supposed to have a relationship with other notebooks, which makes sense, except that I would love to import other ipynb files.
The only workaround I see is converting my *.ipynb files into *.py files, which then can be imported into my notebook. Having one file hold everything in a project is a bit weird, especially if I want to really push for code-reuse (isn't that a core tenet of python?).
Am I missing something? Is this not a supported use case of ipython notebooks? Is there another solution I can be using for this import of an ipynb file into another notebook? I'd love to continue to use ipynb, but it's really messing up my workflow right now :(
The above mentioned comments are very useful but they are a bit difficult to implement. Below steps you can try, I also tried it and it worked:
The issue is that a notebooks is not a plain python file. The steps to import the
.ipynb
file are outlined in the following: Importing notebookI am pasting the code, so if you need it...you can just do a quick copy and paste. Notice that at the end I have the
import primes
statement. You'll have to change that of course. The name of my file isprimes.ipynb
. From this point on you can use the content inside that file as you would do regularly.Wish there was a simpler method, but this is straight from the docs.
Note: I am using jupyter not ipython.
Please make sure that you also add a
__init__.py
file in the package where all your other .ipynb files are located.This is in addition to the nbviewer link that
minrk
andsyi
provided above.I also had some similar problem and then I wrote the solution as well as a link to my public google drive folder which has a working example :)
My Stackoverflow post with step by step experimentation and Solution:
Jupyter Notebook: Import .ipynb file and access it's method in other .ipynb file giving error
Hope this will help others as well. Thanks all!
There is no problem at all using Jupyter with existing or new Python .py modules. With Jupyter running, simply fire up Spyder (or any editor of your choice) to build / modify your module class definitions in a .py file, and then just import the modules as needed into Jupyter.
One thing that makes this really seamless is using the autoreload magic extension. You can see documentation for autoreload here:
http://ipython.readthedocs.io/en/stable/config/extensions/autoreload.html
Here is the code to automatically reload the module any time it has been modified:
Note that I tried the code mentioned in a prior reply to simulate loading .ipynb files as modules, and got it to work, but it chokes when you make changes to the .ipynb file. It looks like you need to restart the Jupyter development environment in order to reload the .ipynb 'module', which was not acceptable to me since I am making lots of changes to my code.
If you want to import
A.ipynb
fromB.ipynb
writein
B.ipynb
.The
import_ipynb
module I've created is installed via pip:It's just one file and it strictly adheres to the official howto on the jupyter site.
PS It also supports things like
from A import foo
,from A import *
etcIt is really simple in newer Jupyter: