将多个变量从一个模块传递到另一个模块
python 223
原文标题 :Pass multiple variables from one module to another
我在文件夹“Programm/modules”中有几个模块,它们被“Programm”中的另一个脚本调用。
例如,一个看起来像这样:
“计算.py”
def calculate():
x = 1 + 2
y = 3 + 4
return x
return y
这个我想加载到另一个模块“print.py”
import os
import sys
sys.path.append(".")
import modules.calculate
def print():
print(x)
print(y)
但是我得到了错误"ModuleNotFoundError: No module named 'calculate'"
怎么了?
编辑
Thx 伙计们,我现在可以使用以下命令加载模块:
from modules.calculate import calculate
我将返回更改为:
return x, y
但现在我得到:
"NameError: name "x" is not defined"
如何将“x”和“y”导入“print.py”?
回复
我来回复-
Damiaan 评论
该回答已被采纳!
如果您的程序文件夹如下所示:
├── programm.py ├── modules │ ├── calculate.py
from modules.calculate import calculate
编辑:
使用值 you
return
将其分配给变量。喜欢x, y = calculate()
。现在你可以像这样在你的print.py
中使用(这些)x 和 y:import os import sys sys.path.append(".") import modules.calculate def print(): x, y = calculate() print(x) print(y)
2年前