简体   繁体   English

如何在Anaconda(Jupyter笔记本)中导入python自定义类

[英]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.

相关问题 Python:已安装Anaconda,但无法在Jupyter笔记本中导入numpy或matplotlib - Python: Installed Anaconda, but can't import numpy or matplotlib in Jupyter notebook 为什么我不能在 anaconda jupyter 笔记本中导入 package? - why can't i import a package in anaconda jupyter notebook? 如何在 Google Colab 的 Jupyter 笔记本中导入自定义 Python package 和模块? - How can I import a custom Python package and module in a Jupyter notebook on Google Colab? 如何在 jupyter 笔记本中编写 python class 然后将其导入以再次使用? - How can I write a python class in a jupyter notebook and then import it to use again? 如何在 jupyter 笔记本中导入 python 类/模块? - How to import a python class/module in a jupyter notebook? 如何在Jupyter笔记本中导入CPLEX? - How can I import CPLEX in Jupyter notebook? 如何通过Anaconda在Jupyter Notebook中安装pydotplus? - How can I install pydotplus in Jupyter Notebook via Anaconda? 如何在 Mac 上为 Jupyter Notebook(Anaconda) 创建桌面快捷方式? - How can I create a desktop shortcut for Jupyter Notebook(Anaconda) on Mac? 我无法在Anaconda中启动Jupyter笔记本 - I can't launch a jupyter notebook in anaconda 如何在 Anaconda 中的 Jupyter notebook 中使用 Python 连接 Cassandra? - How to connect Cassandra in Jupyter notebook In Anaconda with Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM