在python脚本之间传递变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16048237/
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
Pass variable between python scripts
提问by DavidJB
I'm sure this is very simple but I've been unable to get it working correctly. I need to have my main python script call another python script and pass variables from the original script to the script that I've called
我确信这很简单,但我一直无法让它正常工作。我需要让我的主 python 脚本调用另一个 python 脚本并将变量从原始脚本传递到我调用的脚本
So for a simplistic example my first script is,
所以对于一个简单的例子,我的第一个脚本是,
first.py
x = 5
import second
and my second script is,
我的第二个脚本是,
second.py
print x
and I would expect it to print x but I get
我希望它打印 x 但我得到
NameError: name 'x' is not defined
I'm not sure if import is right way to achieve this, but if someone could shed light on it in a simple way that would be great!
我不确定导入是否是实现这一目标的正确方法,但如果有人能以一种简单的方式阐明它,那就太好了!
thanks,
谢谢,
EDIT
编辑
After reading the comments I thought I would expand on my question. Aswin Murugesh answer fixes the import problem I was having, however the solution does not have the desired outcome as I can not seem to pass items in a list this way.
阅读评论后,我想我会扩展我的问题。Aswin Murugesh 的回答解决了我遇到的导入问题,但是该解决方案没有达到预期的结果,因为我似乎无法以这种方式传递列表中的项目。
In first.py I have a list which I process as follows
在 first.py 中,我有一个列表,我按如下方式处理
for insert, (list) in enumerate(list, start =1):
'call second.py passing current list item'
I wanted to pass each item in the list to a second python file for further processing (web scraping), I didn't want to do this in first.py as this is meant to be the main 'scan' program which then calls other programs. I hope this now make more sense.
我想将列表中的每个项目传递给第二个 python 文件以进行进一步处理(网络抓取),我不想在 first.py 中执行此操作,因为这是主要的“扫描”程序,然后调用其他程序程式。我希望这现在更有意义。
Thanks for the comments thus far.
感谢您到目前为止的评论。
采纳答案by Abhranil Das
When you call a script, the calling script can access the namespace of the called script. (In your case, firstcan access the namespace of second.) However, what you are asking for is the other way around. Your variable is defined in the calling script, and you want the called script to access the caller's namespace.
调用脚本时,调用脚本可以访问被调用脚本的命名空间。(在您的情况下,first可以访问second的命名空间。)但是,您要求的是另一种方式。您的变量在调用脚本中定义,并且您希望被调用脚本访问调用者的命名空间。
An answer is already stated in this SO post, in the question itself:
在这个 SO 帖子中,问题本身已经给出了答案:
Access namespace of calling module
But I will just explain it here in your context.
但我只会在你的上下文中解释它。
To get what you want in your case, start off the called script with the following line:
要在您的情况下获得您想要的内容,请使用以下行开始调用脚本:
from __main__ import *
This allows it to access the namespace (all variables and functions) of the caller script.
这允许它访问调用者脚本的命名空间(所有变量和函数)。
So now your calling script is, as before:
所以现在你的调用脚本和以前一样:
x=5
import second
and the called script is:
被调用的脚本是:
from __main__ import *
print x
This should work fine.
这应该可以正常工作。
回答by Aswin Murugesh
use the following script:
使用以下脚本:
first.py:
第一个.py:
x=5
second.py
第二个.py
import first
print first.x
this will print the x value. Always imported script data should be referenced with the script name, like in first.x
这将打印 x 值。应使用脚本名称引用始终导入的脚本数据,例如first.x
回答by Z-Jiang
Try use exec Python3.5:
尝试使用 exec Python3.5:
first.py
第一个.py
x=5
exec(open('second.py').read())
second.py
第二个.py
print(x)
You can also pass x by using:
您还可以使用以下方法传递 x:
x=5
myVars = {'x':x}
exec(open('second.py').read(), myVars)
Not sure if this is a good way.
不确定这是否是一个好方法。
回答by Grant Palmer
To avoid namespace pollution, import the variables you want individually: from __main__ import x, and so on. Otherwise you'll end up with naming conflicts you weren't aware of.
为避免命名空间污染,请单独导入您想要的变量:from __main__ import x,等等。否则,您最终会遇到您不知道的命名冲突。

