如何从另一个 Python 脚本访问 Python 脚本的动态变量?
How to access a dynamic variable of a Python script from another Python script?
所以有两个脚本:script1.py
和 script2.py
。 script1.py
有一个变量 x
,用于存储在 运行 时间内从文件中读取的字符串。在 script2.py
中,我导入 script1.py
,然后使用以下内容导入 运行 script1.py
:script1.main()
。然后我 运行 script1.py
中的一个特定函数负责读取有问题的文件。
您可能想知道为什么我不首先 运行 那个特定函数而不是 运行 整个脚本。这是因为 script1.py
每次 运行.
时都会做一些 script2.py
需要的其他事情
当 script1.py
从 script2.py
变为 运行 时,我希望能够读取存储在 x
中的内容。我目前正在做的基本上是 运行ning 的一部分 script1.py
两次。我想知道是否可以在不执行以下操作的情况下访问变量 x
:
#script1
def read_file():
f = open('file_path.txt')
string = f.read()
return string
def main():
x = read_file()
if __name__ == '__main__':
main()
和script2.py
:
#script2
import script1
from script1 import *
def main():
script1.main()
x = read_file()
print(x)
if __name__ == '__main__':
main()
所以基本上我想知道这种事情通常应该如何完成。是否可以在不必 运行 read_file()
的情况下获得 x
的值? script1.main()
执行时,x
的值存储在哪里,如何访问?不确定我是否应该使用 import script1
还是应该使用 from script1 import *
来实现这一点。
由于 x
是 script1.main
函数中的局部变量,因此无法从外部访问它(无论是从 script2 还是从 script1)。事实上,该对象将在 script1.main
完成后被丢弃。
如果你想重复使用它,你需要将它存储在其他地方。
一个快速的解决方法是使变量 x
成为全局变量。然后 script2 可以将其用作 script1.x
.
# script1.py
def read_file():
return "foo bar"
def main():
global x
x = read_file()
if __name__ == '__main__':
main()
# script2
import script1
def main():
script1.main()
#x = read_file()
print(script1.x)
if __name__ == '__main__':
main()
如果您不想使用 global
语法,另一种方法是在 script1.py
中定义一个可变变量,并让它存储您要重用的变量。一种可能性是使用 class
。在这里,我使用 class
作为可变存储。如果你不喜欢它,其他可变对象如 dict
也可以。
# script1.py
class data:
x = None
def read_file():
return "foo bar"
def main():
data.x = read_file()
if __name__ == '__main__':
main()
# script2
import script1
def main():
script1.main()
#x = read_file()
print(script1.data.x)
if __name__ == '__main__':
main()
所以有两个脚本:script1.py
和 script2.py
。 script1.py
有一个变量 x
,用于存储在 运行 时间内从文件中读取的字符串。在 script2.py
中,我导入 script1.py
,然后使用以下内容导入 运行 script1.py
:script1.main()
。然后我 运行 script1.py
中的一个特定函数负责读取有问题的文件。
您可能想知道为什么我不首先 运行 那个特定函数而不是 运行 整个脚本。这是因为 script1.py
每次 运行.
script2.py
需要的其他事情
当 script1.py
从 script2.py
变为 运行 时,我希望能够读取存储在 x
中的内容。我目前正在做的基本上是 运行ning 的一部分 script1.py
两次。我想知道是否可以在不执行以下操作的情况下访问变量 x
:
#script1
def read_file():
f = open('file_path.txt')
string = f.read()
return string
def main():
x = read_file()
if __name__ == '__main__':
main()
和script2.py
:
#script2
import script1
from script1 import *
def main():
script1.main()
x = read_file()
print(x)
if __name__ == '__main__':
main()
所以基本上我想知道这种事情通常应该如何完成。是否可以在不必 运行 read_file()
的情况下获得 x
的值? script1.main()
执行时,x
的值存储在哪里,如何访问?不确定我是否应该使用 import script1
还是应该使用 from script1 import *
来实现这一点。
由于 x
是 script1.main
函数中的局部变量,因此无法从外部访问它(无论是从 script2 还是从 script1)。事实上,该对象将在 script1.main
完成后被丢弃。
如果你想重复使用它,你需要将它存储在其他地方。
一个快速的解决方法是使变量 x
成为全局变量。然后 script2 可以将其用作 script1.x
.
# script1.py
def read_file():
return "foo bar"
def main():
global x
x = read_file()
if __name__ == '__main__':
main()
# script2
import script1
def main():
script1.main()
#x = read_file()
print(script1.x)
if __name__ == '__main__':
main()
如果您不想使用 global
语法,另一种方法是在 script1.py
中定义一个可变变量,并让它存储您要重用的变量。一种可能性是使用 class
。在这里,我使用 class
作为可变存储。如果你不喜欢它,其他可变对象如 dict
也可以。
# script1.py
class data:
x = None
def read_file():
return "foo bar"
def main():
data.x = read_file()
if __name__ == '__main__':
main()
# script2
import script1
def main():
script1.main()
#x = read_file()
print(script1.data.x)
if __name__ == '__main__':
main()