Jupyter Notebook: Import .ipynb file and access it's method in other .ipynb file giving error

我是研究僧i 提交于 2019-12-20 00:52:04

问题


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.

  1. Created a small .ipynb file abc.ipynb as follows

    def prt_n(str):
        print(str)
    if __name__ == '__main__':
        prt_n("in abc")
    
  2. Created an .ipynb file to import Jupyter Notebook from the Jupyter link given above. Say, importer.ipynb.

  3. Run importer.ipynb

  4. import abc

  5. str="Hello Me"

  6. Test step abc.__name__ results in abc as output.

  7. 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!


回答1:


**

Link to sample files on Drive

**

Ok. So, after some struggle and looking around on Internet, and finally found a solution that worked for my sample case.

Firstly, this is the stackoverflow question that was the most helpful to me. Mohideen and Tyhela's answer was the actual solution and not the one with most number of votes.

So, what I did was, I made a folder by the name module and placed all my .ipynb files there. Plus, I created an __init__.py file in that module by using touch __init__.py command such that the import can register it as a valid module. Those guys have given a detailed explanation for it which seems legit.

Then from my working directory I ran the following commands:

str = "Hello Me"
import test.abc as tabc
tabc.prt_n(str)

that gave me Hello Me in the output.

And for,

`import test.efg as tefg`

I got

importing Jupyter notebook from test/efg.ipynb Hello Note

As my desired output.

I hope this will be of help to others who land up on similar problem.

If you have a better approach then I will appreciate if you can share that with me.

Thank you :)




回答2:


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")


来源:https://stackoverflow.com/questions/44336087/jupyter-notebook-import-ipynb-file-and-access-its-method-in-other-ipynb-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!