首页 > 解决方案 > Jupyter notebook 使用 numpy 导入其他 .py 文件不起作用

问题描述

我正在使用 Jupyter notebook 导入另一个包含我的函数的 .py 文件。外部函数需要 numpy 模块。但是当我运行时,它说“名称'np'未定义”。

我试图从我的 .py 文件或 Jupyter 中导入 numpy,但它不起作用。我不知道是什么导致了这里的问题。即使我评论了 .py 文件中的 np 导入行并删除了我使用 np 的行,错误仍然显示。

在我的 Jupyter 笔记本中:

import test
import numpy as np
a = np.array([8,3])
m = test.test(a)

在 test.py 中:

import numpy as np
def test(x):
    m = np.mean(x)
    return m

标签: pythonpython-3.xnumpyjupyter-notebook

解决方案


推荐阅读