简体   繁体   English

从已完成运行的 python 脚本中检索值

[英]retrieving values from a finished running python script

i have a question related with python, anyway i have a script that has finished running, i give script1.py for example :我有一个与python相关的问题,无论如何我有一个已经完成运行的脚本,例如我给出script1.py:

#script1.py
def test():
    x=1
    return x

val=test()

the question is how to retrieve script1.py after it has finished running?问题是如何在 script1.py 运行完成后检索它?

i am doing something like this using script2.py but script1.py starts running again :我正在使用 script2.py 做这样的事情,但 script1.py 再次开始运行:

#sript2.py
import script1
val2=script1.val

do you know how to just retrieve the value from script1.py without restarting script1.py ??您知道如何在不重新启动 script1.py 的情况下从 script1.py 中检索值吗? any answer is appreciated, thx before任何答案表示赞赏,谢谢之前

You can use below code.您可以使用以下代码。

from script1 import x

Example: script1:示例:脚本 1:

c = 10

script2:脚本2:

from script1 import c
print(c)

Let's look at what running script 1 does.让我们看看运行脚本 1 做了什么。

#script1.py 
def test():  # This defines a function, not an object
    x=1
    return x

val=test() # This assigns the object val={int}1

So what happens in script2 follows那么在 script2 中发生的事情如下

#script2.py
import script1 # Evaluates the whole context of script1, which will "rerun" it
val2=script1.val # This value can only be "carried over" because it was reevaluated

Since the script is ran in two separate instances, the original value from script1 will not be saved, as it will be wiped out with the rest of the objects in script1.由于脚本在两个单独的实例中运行,因此不会保存 script1 中的原始值,因为它将与 script1 中的其余对象一起被清除。

eg If I rewrite script1 to the following, script2 will always crash:例如,如果我将 script1 重写为以下内容,script2 将始终崩溃:

#script1.py changed to break script2
def test():
    x=1
    return x

# Syntax for saying, only evaluate this value if I execute this, not on import
if __name__ == '__main__':
    val=test()

If you need the value to "persist" between contexts here you will need to to put that somewhere else.如果您需要值在此处的上下文之间“持久化”,则需要将其放在其他地方。 A simple way, or one of the first ways I learned how to, would be to pickle your data out to a file and read it back in script2.一种简单的方法,或者说是我学会的第一种方法,就是将数据提取到文件中,然后在 script2 中读回。

https://www.geeksforgeeks.org/understanding-python-pickling-example/ https://www.geeksforgeeks.org/understanding-python-pickling-example/

#script1.py pickling
import pickle
pickled_output='s1.pkl'

def test():
    x=1
    return x

# Syntax for saying, only evaluate this value if I execute this, not on import
if __name__ == '__main__':
    val=test()
    with open(pickled_output,'wb') as pickled_file:
        pickle.dump(val, pickled_file)

#script2.py pickling
import pickle
import s1

with open(s1.pickled_output,'rb') as pickled_output:
    val2 = pickle.load(pickled_output)

This is nice because it is flexible to many types of objects and can make it clearer what you are doing in each stage.这很好,因为它适用于多种类型的对象,并且可以使您在每个阶段更清楚地了解您在做什么。 Plus it will raise an error if the file doesn't exist!另外,如果文件不存在,它会引发错误!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM