简体   繁体   English

将变量从一个python脚本导入另一个

[英]Importing a variable from one python script to another

I have script1.py which calls script2.py (subprocess.call([sys.executable, "script2.py"]) . But script2.py needs variable x that is known in script1.py . I tried a very simple import x from script1 , but it seems not to work. 我有script1.py调用script2.py (subprocess.call([sys.executable, "script2.py"]) 。但是script2.py需要在script1.py已知的变量x 。我尝试了一个非常简单的import x from script1 ,但它似乎无法正常工作。

Is that the right approach to use? 这是正确的使用方法吗? For example: 例如:

#script1.py
import subprocess, sys
##subprocess.call([sys.executable, 'C:\\...\\Desktop\\script2.py'], shell=True)
##os.system("C:\\...\\Desktop\\script2.py")
subprocess.Popen("C:\\...\\Desktop\\script2.py", shell=True)
print "BLAH"
x = BO

#script2.py
from script1 import x
print "HELLO"
print x

All 3 cases of calling script2 (subprocess.call, os.system, subprocess.Popen ) do not work. 所有3个调用script2(subprocess.call,os.system,subprocess.Popen)的情况都不起作用。 I get "BLAH" but not "HELLO". 我得到“BLAH”而不是“HELLO”。

The correct syntax is: 正确的语法是:

from script1 import x

So, literally, "from script1.py import the "x" object." 所以,从字面上看,“从script1.py导入”x“对象。”

Try this: 试试这个:

from script1 import x

I just ran the following pieces of code and it worked 我刚刚运行了以下代码片段并且工作正常

script1: SCRIPT1:

c = 10

script2: SCRIPT2:

from script1 import c
print c

The second script printed the integer 10 as you should expect. 第二个脚本按预期打印整数10。

Oct 17 Edit: As it stands the code will either not produce the "Hello" as indicated or will go into an infinite loop. 10月17日编辑:现在代码将不会产生指示的“Hello”或将进入无限循环。 A couple of issues: 几个问题:

As it stands, BO is undefined. 就目前而言,BO未定义。 When you execute script1, the subprocess of script2 is opened. 执行script1时,将打开script2的子进程。 When script2 calls script1, it will print out blah but fail on x=BO as BO is undefined. 当script2调用script1时,它将打印出blah但在x = BO时失败,因为BO未定义。

So, if you fix that by specifying BO with say a string, it will go into an infinite loop (each script calling the other one and printing x, Hello and Blah). 所以,如果你通过用一个字符串指定BO来修复它,它将进入一个无限循环(每个脚本调用另一个脚本并打印x,Hello和Blah)。

One potential way to fix it is to pass x through a function call. 修复它的一种可能方法是通过函数调用传递x。 So, script2 could take x as a function parameter and do whatever you need to do with it. 因此,script2可以将x作为函数参数,并做任何你需要做的事情。

Your code is looping because the subprocess.Popen call is in the module initialization code, so it will be called by script2 when importing script1 (creating a new script2 process that also imports script1 ...) 您的代码是循环,因为subprocess.Popen通话是在模块初始化代码,则其将被导入SCRIPT2 SCRIPT1时被调用(创建一个新的SCRIPT2的过程,也进口SCRIPT1 ...)

The recommended way of having a python file usable as both a script and a module is to use the __name__ variable python文件用作脚本和模块的推荐方法是使用__name__变量

#script1.py

x = BO

if __name__ == "__main__":
    import subprocess, sys
    subprocess.Popen("C:\\...\\Desktop\\script2.py", shell=True)
    print "BLAH"

But also consider this only works for constants . 但也要考虑这只适用于常数 If x can change at runtime you will need an actual interprocess communication method. 如果x可以在运行时更改,则需要实际的进程间通信方法。

script1.py: script1.py:

x = 2
from script2 import *

script2.py: script2.py:

from script1 import x
print x

Script 0: 脚本0:

#!/usr/bin/env python
from script1 import x

print x

Script 1: 脚本1:

#!/usr/bin/env python
x = "Hello world"

Output: 输出:

Hello world

So yeah it does work, no need for sub processes. 所以它确实有效,不需要子流程。

我认为你必须通过在script2.py中为其模块名称(即script1.x)添加前缀来引用该变量

The method being used is more complex than is necessary. 使用的方法比必要的更复杂。 After fixing BO (I assumed it is meant as a string literal, so I quoted it), and assuming the scripts are co-located, you can call script2 and get the result you are after. 修复BO之后(我假设它是一个字符串文字,所以我引用它),并假设脚本位于同一位置,你可以调用script2并获得你所追求的结果。

See the following: 请参阅以下内容:

#script1.py
print "BLAH"
x = "BO"

#script2.py
from script1 import x
print "HELLO"
print x

$ python script2.py 
BLAH
HELLO
BO

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

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