[英]How can I import python custom class in Anaconda (Jupyter notebook)
I don't manage to find how import custom class in Python with the Jupyter notebook in anaconda. 我无法找到如何在蟒蛇中使用Jupyter笔记本在Python中导入自定义类。
In my work folder I have a file 'user.ipynb' that contains a class name User . 在我的工作文件夹中,我有一个文件'user.ipynb' ,其中包含一个类名User 。 In an other file in the same folder, I try to import this class with : from user import User .
在同一文件夹中的另一个文件中,我尝试使用以下命令导入此类: from user import User 。
I get this error: ImportError: No module named user. 我收到此错误:ImportError:没有名为用户的模块。
I tried to create a file ' _ _init__.py' and _ _init__.ipynb in this folder but it doesn't work. 我试图在此文件夹中创建文件'_ _init__.py'和_ _ init __。ipynb,但是它不起作用。
Do you know how I could do this ? 你知道我该怎么做吗?
Thank you in advance 先感谢您
Python modules are either files named *.py
or directories with an __init__.py
in them. Python模块是名为
*.py
文件或其中带有__init__.py
目录。 user.ipynb
is neither, it's an IPython (Jupyter) notebook, so it is not a module and you can't import it. user.ipynb
都不是,它不是IPython(Jupyter)笔记本,因此它不是模块,您不能导入它。
The easiest way to get this to work is to convert the notebook to a .py
file (this can be done using the export function) and then you'll be able to import it. 使此工作最简单的方法是将笔记本转换为
.py
文件(可以使用导出功能来完成),然后就可以导入它。
An alternative is to provide code that lets you import notebooks directly; 另一种方法是提供使您可以直接导入笔记本的代码。 such code is available here .
这样的代码可以在这里找到 。 Looks like a lot of bother, though.
看起来很麻烦。 I'm not sure why they didn't just bake this in;
我不确定他们为什么不把它烤进去。 it seems like it would be useful.
似乎很有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.