问题描述
我对Jupyter Notebook很陌生.我玩了一段时间.但这是我第一次尝试将另一个笔记本导入我的主班.
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.
作为参考,我正在使用Anaconda 4.3.1和Python v2.7.
For reference, I am using Anaconda 4.3.1 and Python v2.7.
我正在尝试将我在python项目中所做的工作复制到jupyter笔记本中.它需要将其他.ipynb文件(从原始.py文件转换而成)导入到其中,以根据需要使用相关方法.
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.
为此,我遵循Jupyter Nbviewer 步骤链接,我在以下堆栈.它给了我一些主意,但经过一个阶段后并没有帮助我.
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.
-
按如下所示创建一个小的.ipynb文件abc.ipynb
Created a small .ipynb file abc.ipynb as follows
def prt_n(str):
print(str)
if __name__ == '__main__':
prt_n("in abc")
创建了一个.ipynb文件,以从上面给出的Jupyter链接导入Jupyter Notebook.说importer.ipynb.
Created an .ipynb file to import Jupyter Notebook from the Jupyter link given above. Say, importer.ipynb.
运行importer.ipynb
Run importer.ipynb
import abc
str="Hello Me"
测试步骤abc.__name__
导致abc
作为输出.
Test step abc.__name__
results in abc
as output.
abc.prt_n(str)
引发以下错误
* ---------------------------------------------- -----------------------------
*---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-9-2fb88a43c9e5> in <module>()
----> 1 abc.prt_n(str)AttributeError:模块"对象没有属性"prt_n" *
----> 1 abc.prt_n(str)AttributeError: 'module' object has no attribute 'prt_n'*
我希望答案是Hello Me
.
稍后,我想实际创建一个myMain.ipynb文件,并希望包含2-3个这样的笔记本,并使用必需的参数调用它们的方法.
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.
一个这样的示例可以是文件efg.ipynb
,如下所示:
One such example can be a file efg.ipynb
as follows:
import abca="Hello Notebook"abc.prt_n(a)
import abca="Hello Notebook"abc.prt_n(a)
我还想确保是否还有其他方法可以做到这一点?
I also want to make sure if there is any other way to do this?
注意:我已经检查了sys.executable和sys.path.两者具有相同的python路径值.
Note: I have already checked sys.executable and sys.path. Both have the same python path value.
欢迎任何帮助!
推荐答案
**
**
好的.因此,经过一番挣扎并在Internet上四处浏览,终于找到了适用于我的示例案例的解决方案.
Ok. So, after some struggle and looking around on Internet, and finally found a solution that worked for my sample case.
首先,这是 stackoverflow 对我最有帮助的问题. Mohideen和Tyhela的答案是实际的解决方案,而不是票数最多的解决方案.
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.
所以,我要做的是,通过名称模块创建了一个文件夹,并将所有.ipynb文件放置在那里.另外,我使用touch __init__.py
命令在该模块中创建了一个__init__.py
文件,以便导入可以将其注册为有效模块.那些家伙对此进行了详细的解释,这似乎是合法的.
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)
在输出中给了我Hello Me
.
还有,
`import test.efg as tefg`
我知道了
importing Jupyter notebook from test/efg.ipynbHello Note
importing Jupyter notebook from test/efg.ipynbHello Note
作为我想要的输出.
我希望这对陷入类似问题的其他人有所帮助.
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.
谢谢:)
这篇关于Jupyter Notebook:导入.ipynb文件并访问其他.ipynb文件中给出错误的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!