将功能与笔记本目录中其他python文件中的变量一起使用

问题描述 投票:1回答:1

例如,在我的文件夹中,我有一个ipython笔记本“ program.ipynb”和一个其中包含某些功能的python文件“ functions.py”,例如“ func”

from numpy import sqrt
def func(x):
    return N + sqrt(x)

将在“ program.ipynb”中使用,看起来像这样

from functions import func
N = 5
func(2)
--> name 'N' is not defined

要修复该错误,我需要在我的functions.py文件中定义变量N,但是没有办法吗?我想在主程序(program.ipynb)中定义所有全局变量。

python function variables jupyter-notebook
1个回答
0
投票

您无法访问这样的变量,最好的方法是:

functions.py

from numpy import sqrt
def func(x, N):
    return N + sqrt(x)

program.ipynb

from functions import func
N = 5
func(2, N)
© www.soinside.com 2019 - 2024. All rights reserved.