从另一个文件导入变量会产生错误

时间:2020-03-05 20:32:00

标签: python variables jupyter-notebook python-import

我正在使用Python的Jupyter Notebooks,并且一直试图从同一文件夹中的另一个文件中导入名为 thresh_mean 的变量。

import scipy.io as sio
import seaborn as sns
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from firstsound import thresh_mean #this line

但是,出现此错误:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-42-1ee7c2613f81> in <module>
      4 import pandas as pd
      5 import matplotlib.pyplot as plt
----> 6 import firstsound

ModuleNotFoundError: No module named 'firstsound'

有人知道为什么会这样吗,以及如何导入该变量?

1 个答案:

答案 0 :(得分:0)

由于您要传递变量,因此在定义变量后,您想在源笔记本的某处使用%store执行命令,然后通过添加{{1}将其读入另一个笔记本-r之后的}选项表示已读。参见here和#9 here

%store

(如果要使用变量并将其传递到自己的Jupyter环境之外,则可能要考虑使用pickle进行序列化和传递变量。)

如果它是一个函数,则可以使用import-ipynb。还有nbimporter

相关问题