0

我有两 (2) 个脚本,如下所示:

测试.py

from google.apputils import app
import gflags

FLAGS = gflags.FLAGS
gflags.DEFINE_string('flag1', 'Value 1', 'flag#1')

def main(argv):
    FLAGS.flag1 = "Value2"
    print 'Flag 1 has value: ' + FLAGS.flag1

if __name__ == '__main__':
    app.run()

test1.py

from google.apputils import app
import test

def main(argv):
    print 'Flag 1 has value: ' + test.FLAGS.flag1


if __name__ == '__main__':
    app.run()

如何将更改后的标志值从 test.py 传递到 test1.py,以便在 test1.py 中输出“标志 1 具有值:Value2”?

4

1 回答 1

0

我找到了如下解决方案:

测试.py

from google.apputils import app
import subprocess


def main(argv):
    entry = raw_input("Enter Value: ")
    subprocess.Popen('python test1.py --flag1="' + entry + '"', shell=True)


if __name__ == '__main__':
    app.run()

test1.py

from google.apputils import app
import gflags

gflags.DEFINE_string('flag1', None, 'Flag#1')

FLAGS = gflags.FLAGS


def main(argv):
    print 'Flag 1 has value: ' + FLAGS.flag1


if __name__ == '__main__':
    app.run()
于 2017-09-13T18:48:04.333 回答