I am fairly new to Jupyter Notebook. I have played around with it for a while. But this was my first time trying to import another notebook into my main class.
For reference, I am using Anaconda 4.3.1 and Python v2.7.
I am trying to replicate what I did in my python project to jupyter notebooks. It requires importing other .ipynb files (translated from the original .py files) into it to use relevant methods as desired.
For this, I followed the directions as given on Jupyter Nbviewer Steps Link which I found through my preliminary search on the following stack Question. It gave me some idea but didn't help me after one stage.
I will walk you through the steps I took and the sample program I tried.
Created a small .ipynb file abc.ipynb as follows
def prt_n(str):
print(str)
if __name__ == '__main__':
prt_n("in abc")
Created an .ipynb file to import Jupyter Notebook from the Jupyter link given above. Say, importer.ipynb.
Run importer.ipynb
import abc
str="Hello Me"
Test step abc.__name__
results in abc
as output.
abc.prt_n(str)
throws following error
*---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-9-2fb88a43c9e5> in <module>()
----> 1 abc.prt_n(str) AttributeError: 'module' object has no attribute 'prt_n'*
I was hoping that the answer will be Hello Me
.
Later, I want to actually create a myMain.ipynb file and in that I want to include 2-3 such notebooks and call their methods with required parameters.
One such example can be a file efg.ipynb
as follows:
import abc
a="Hello Notebook"
abc.prt_n(a)
I also want to make sure if there is any other way to do this?
Note: I have already checked sys.executable and sys.path. Both have the same python path value.
Any sort of help is welcome!
Simple way to use ipynb files in jupyter note book are as follows: 1) Install import-ipynb. pip install import-ipynb. 2) Import import_ipynb in jupyter notebook. Then import ipynb file as you import .py file. import import_ipynb from test import print_name print_name ("your name") Share. Improve this answer.
It is a common problem that people want to import code from Jupyter Notebooks. This is made difficult by the fact that Notebooks are not plain Python files, and thus cannot be imported by the regular Python machinery.
The code within import_ipynb.py defines a “notebook loader” that allows you to ‘import’ other ipynb files into your current ipynb file. This entails: load the notebook document into memory; create an empty Module; execute every cell in the Module namespace
The steps to import the .ipynb file are outlined in the following: Importing notebook I 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 is primes.ipynb.
Simple way to use ipynb files in jupyter note book are as follows:
1) Install import-ipynb
pip install import-ipynb
2) Import import_ipynb
in jupyter notebook. Then import ipynb file as you import .py file
import import_ipynb
from test import print_name
print_name("your name")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With