PyInquirer及其子进程Popen

2024-06-28 19:02:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我的目标很简单,我有两个脚本A和B,它们都使用PyInquirer来处理用户的输入。在

脚本a.py:

from PyInquirer import prompt

    def do():
        questions = [
            {
                'type': 'input',
                'name': 'inputa1',
                'message': 'inputa1',
            }
        ]
        answers = prompt(questions)

        # call b.py

        if output_from_b == 1:
            # do something AAA
        else:
            # do something BBB

    if __name__ == "__main__":
        do()

脚本b.py几乎相同:

^{pr2}$

如您所见,我要做的是从a.py调用b.py,从b.py获取用户输入,然后在a.py中继续输出结果。在

我试过了:

^{3}$

要调用b.py但出现错误:

Traceback (most recent call last):
  File "b.py", line 27, in <module>
    do()
  File "b.py", line 22, in do
    answers = prompt(questions)
  File "python3.7/site-packages/PyInquirer/prompt.py", line 75, in prompt
    eventloop=eventloop)
  File "python3.7/site-packages/prompt_toolkit/shortcuts.py", line 576, in run_application
    output=create_output(true_color=true_color))
  File "python3.7/site-packages/prompt_toolkit/shortcuts.py", line 126, in create_output
    ansi_colors_only=ansi_colors_only, term=term)
  File "python3.7/site-packages/prompt_toolkit/terminal/vt100_output.py", line 424, in from_pty
    assert stdout.isatty()
AssertionError

我不知道哪里出了问题,或者有没有其他方法来完成这件事。在

如有任何建议,我们将不胜感激,谢谢:)


Tags: 用户infrompy脚本outputpackagesline
1条回答
网友
1楼 · 发布于 2024-06-28 19:02:23

最好的方法是使用这样的方法类:

#import class
classe_call = getattr(importlib.import_module('package.myclassB', 'myclassB')
#create object
myObject = class_call() #you can use param 
#call method
getattr(myObject, do)()

相关问题 更多 >