简体   繁体   English

如何从 Jupyter Notebook 上的 other.ipynb 导入变量?

[英]How to import variables from other .ipynb on Jupyter Notebook?

I've seen a lot of other questions here about this topic however none have helped.我在这里看到了很多关于这个主题的其他问题,但是没有一个有帮助。 I am coding on Jupyter Notebook using Python 3, to organize my code I am trying to use 2 different modules for my program.我正在使用 Python 3 在 Jupyter Notebook 上进行编码,以组织我的代码,我试图为我的程序使用 2 个不同的模块。 Thus I have 1 module called let's for simplicity call it abc.ipynb and now created a different module called for simplicity edf.ipynb (Both on Jupyter Notebook)因此,我有一个名为让我们为简单起见将其称为abc.ipynb的模块,现在为简单起见创建了一个名为edf.ipynb的不同模块(均在 Jupyter Notebook 上)

At first i wasn't able to import abc to edf but after importing the import_ipynb package and fixing the PYTHONPATH it worked.起初我无法将 abc 导入 edf,但在导入import_ipynb package 并修复 PYTHONPATH 后它起作用了。

However, now I just want to import variables from abc to edf, I am trying:但是,现在我只想将变量从 abc 导入到 edf,我正在尝试:

  1. from abc import x从 abc 进口 x
  2. import abc print(abc.x)导入 abc 打印(abc.x)
  3. I even tried calling a function我什至试着打电话给 function

All give 1 of below errors:都给出以下错误之一:

  1. cannot import name 'x' from 'abc'无法从“abc”导入名称“x”
  2. module 'abc' has no attribute 'function'模块“abc”没有属性“功能”

Any advice?有什么建议吗? I already created an init .py in my folder however I think it is of no use since I am working with.ipynb我已经在我的文件夹中创建了一个init .py 但是我认为它没有用,因为我正在使用 .ipynb

Try changing the name of the file other than "abc".尝试更改“abc”以外的文件名。 Python already has a module called abc Python 已经有一个名为abc的模块

I wanted to do the same thing and found a workaround by creating a function in a separate functions.py file that returns the variable I wanted to pass between files我想做同样的事情,并通过在单独的 functions.py 文件中创建 function 找到了一种解决方法,该文件返回我想在文件之间传递的变量

from functions.py import get_variable
desired_variable = get_variable()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从终端运行 an.ipynb Jupyter Notebook? - How to run an .ipynb Jupyter Notebook from terminal? Jupyter Notebook:导入.ipynb文件,并在其他.ipynb文件中访问它的方法,给出错误 - Jupyter Notebook: Import .ipynb file and access it's method in other .ipynb file giving error 如何在 jupyter notebook 中将一个 ipynb 文件中定义的变量访问到另一个文件中? - How to access variables defined in one ipynb file into another in jupyter notebook? 如何从运行 PySpark 内核的 EMR jupyter notebook 中的另一个 ipynb 文件导入? - How to import from another ipynb file in EMR jupyter notebook which runs a PySpark kernel? 如何从 Jupyter 笔记本上的 * .IPYNB 文件执行 * .PY 文件? - How to execute a * .PY file from a * .IPYNB file on the Jupyter notebook? 如何直接从终端创建和打开 jupyter notebook ipynb 文件 - How to create and open a jupyter notebook ipynb file directly from terminal 如何从.ipynb(Jupyter Notebook)制作可执行的Python文件? - How to make executable Python file from .ipynb (Jupyter Notebook)? vscode:如何自动缩进jupyter notebook ipynb文件? - vscode: how to autoindent jupyter notebook ipynb file? ImportError:无法从 jupyter notebook 中的“app”(app.ipynb)导入名称“app” - ImportError: cannot import name 'app' from 'app' (app.ipynb) in jupyter notebook 如何将 jupyter notebook 导入另一个 jupyter notebook? - How to import jupyter notebook to another jupyter notebook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM