Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从另一个文件导入输入变量而不重复运行此输入函数?_Python_Python 3.x_Input_Import - Fatal编程技术网

Python 如何从另一个文件导入输入变量而不重复运行此输入函数?

Python 如何从另一个文件导入输入变量而不重复运行此输入函数?,python,python-3.x,input,import,Python,Python 3.x,Input,Import,My master.py要求输入参与者名称,然后运行使用此名称的其他.py文件: master.py: participant = str(input("Participant's name: ")) if __name__ == "__main__": import os, subprocess cDir = os.path.dirname(os.path.realpath(__file__)) files = ['antisa

My master.py要求输入参与者名称,然后运行使用此名称的其他.py文件:

master.py

participant = str(input("Participant's name: "))

if __name__ == "__main__":
    import os, subprocess
    cDir = os.path.dirname(os.path.realpath(__file__))
    files = ['antisaccade','change_detection','digit_span']
    for f in files:
        wDir = cDir+ '/' + f
        subprocess.run(["python", f+'.py'], cwd=wDir)
sys.path.append(os.path.abspath('../'))
from master import participant
filename = u'data/%s_%s_%s' % participant
例如,数字\u span.py

participant = str(input("Participant's name: "))

if __name__ == "__main__":
    import os, subprocess
    cDir = os.path.dirname(os.path.realpath(__file__))
    files = ['antisaccade','change_detection','digit_span']
    for f in files:
        wDir = cDir+ '/' + f
        subprocess.run(["python", f+'.py'], cwd=wDir)
sys.path.append(os.path.abspath('../'))
from master import participant
filename = u'data/%s_%s_%s' % participant
因此,每次运行文件时,它们都会使用输入循环调用line:

participant = str(input("Participant's name: "))
但我只需要询问参与者的姓名一次,然后将其发送到其他文件

如果我将
input()
放入
If
中,我将得到

ImportError:无法导入名称“参与者”


有人能解释一下如何解决这个问题吗?

如果master.py询问名称并运行列表中的脚本.py,您可以做的是将名称作为参数传递

就这样,

master.py

participant = list([str(input("Participant's name: "))])
if __name__ == "__main__":
    import os, subprocess
    cDir = os.path.dirname(os.path.realpath(__file__))
    files = ['slave']
    for f in files:
        wDir = cDir+ '/' + f
        subprocess.Popen(["python", "{0}.py".format(wDir)]+participant)
import sys

with open("check.txt".format(sys.argv[1]),'w') as f:
    f.write("{0} participant".format(sys.argv[1]))
从.py

participant = list([str(input("Participant's name: "))])
if __name__ == "__main__":
    import os, subprocess
    cDir = os.path.dirname(os.path.realpath(__file__))
    files = ['slave']
    for f in files:
        wDir = cDir+ '/' + f
        subprocess.Popen(["python", "{0}.py".format(wDir)]+participant)
import sys

with open("check.txt".format(sys.argv[1]),'w') as f:
    f.write("{0} participant".format(sys.argv[1]))
master.py将与参数一起运行slave.py


为了测试结果,它将生成一个文件check.txt,其中包含参与者的名称,这意味着master.py将名称传递给(slave.py)脚本。

而不是每次调用输入函数,将所有名字保存到一个文件中,然后从该文件中读取。@KnowledgeGainer这是一个认知实验的脚本,所以我们永远不知道下一个参与者是谁。我想让这个过程对实验者来说尽可能简单,不需要像打开文件、写名字、关闭、然后运行脚本这样的额外操作。然后你可以将名字作为一个附件传递给你稍后在主函数中调用的那些脚本。非常感谢你的帮助,现在它可以工作了!此外,我还添加了process=subprocess.Popen([“python”,“{0}.py.”format(wDir)]+participant)process.wait()以使其等待子流程的结束