将变量从一个 python 脚本导入到另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19289171/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Importing a variable from one python script to another
提问by Z77
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
需要x
在script1.py
. 我尝试了一个非常简单的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. I get "BLAH" but not "HELLO".
调用 script2 (subprocess.call, os.system, subprocess.Popen ) 的所有 3 种情况都不起作用。我得到“BLAH”但不是“HELLO”。
回答by fjammes
I think you must refer at the variable by prefixing its module name (i.e. script1.x) in script2.py
我认为您必须通过在 script2.py 中为其模块名称(即 script1.x)添加前缀来引用该变量
回答by TerryA
The correct syntax is:
正确的语法是:
from script1 import x
So, literally, "from script1.py import the "x" object."
所以,从字面上看,“从 script1.py 导入“x”对象。”
回答by ppwt
Try this:
尝试这个:
from script1 import x
I just ran the following pieces of code and it worked
我刚刚运行了以下代码片段并且它起作用了
script1:
脚本1:
c = 10
c = 10
script2:
脚本2:
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. A couple of issues:
10 月 17 日编辑:就目前而言,代码要么不会产生所示的“Hello”,要么会进入无限循环。几个问题:
As it stands, BO is undefined. When you execute script1, the subprocess of script2 is opened. When script2 calls script1, it will print out blah but fail on x=BO as BO is undefined.
就目前而言,BO 是未定义的。当你执行script1时,script2的子进程被打开。当 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. So, script2 could take x as a function parameter and do whatever you need to do with it.
修复它的一种潜在方法是通过函数调用传递 x。因此,script2 可以将 x 作为函数参数并执行您需要对其执行的任何操作。
回答by user2753523
script1.py:
脚本1.py:
x = 2
from script2 import *
script2.py:
脚本2.py:
from script1 import x
print x
回答by Mihai Stan
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
调用在模块初始化代码中,因此在导入 script1 时会被 script2 调用(创建一个也导入 script1 的新 script2 进程...)
The recommended wayof 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
可以在运行时更改,您将需要一个实际的进程间通信方法。
回答by Bart Admiraal
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.
所以是的,它确实有效,不需要子流程。
回答by xfrmrs
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