如何传递变量以在不同的脚本文件中运行
How to pass on variable to function in different script file
我有一个 python 文件,我在其中声明我的变量,第二个文件包含更多函数(在本例中只有一个函数)。我似乎无法弄清楚如何将变量从 one.py 传递到 two.py。
one.py:
import two
a=1
b=2
c=3
dict={'ONE':a,'TWO':b,'THREE':c}
two.py:
def f():
d=a+b+c
return d
two.f() 应该 return:
6
提前致谢
编辑:
同时它在命令行中运行良好。但是由于某种原因,当我 运行 Abaqus 中的脚本时不想工作。它说:
count=two.f(my_dictionary)
AttributeError:'Module' object has no attribute 'f'
首先创建一个模块使 'import two' 工作。
- 创建一个目录并在其中放置一个空白 init.py 文件,two.py 和 one.py.
您的 two.py 应该是:
def f(x):
return x['ONE']+x['TWO']+x['Three']
您的 one.py 应该是:
import two
a=1
b=2
c=3
my_dictionary={'ONE':a,'TWO':b,'THREE':c}
two.f(my_dictionary)
我有一个 python 文件,我在其中声明我的变量,第二个文件包含更多函数(在本例中只有一个函数)。我似乎无法弄清楚如何将变量从 one.py 传递到 two.py。
one.py:
import two
a=1
b=2
c=3
dict={'ONE':a,'TWO':b,'THREE':c}
two.py:
def f():
d=a+b+c
return d
two.f() 应该 return:
6
提前致谢
编辑:
同时它在命令行中运行良好。但是由于某种原因,当我 运行 Abaqus 中的脚本时不想工作。它说:
count=two.f(my_dictionary)
AttributeError:'Module' object has no attribute 'f'
首先创建一个模块使 'import two' 工作。 - 创建一个目录并在其中放置一个空白 init.py 文件,two.py 和 one.py.
您的 two.py 应该是:
def f(x):
return x['ONE']+x['TWO']+x['Three']
您的 one.py 应该是:
import two
a=1
b=2
c=3
my_dictionary={'ONE':a,'TWO':b,'THREE':c}
two.f(my_dictionary)