简体   繁体   English

如何从另一个 python 脚本向 python 脚本发送多个“用户输入”?

[英]How can I send multiple “user input” to python script from another python script?

For example I have this file例如我有这个文件

#file2.py
while True:
    name = input("Your name: ")
    print("Hello ", name)

I want to run file2.py from other python file and just send inputs and get outputs.我想从其他 python 文件中运行 file2.py 并只发送输入并获取输出。 I used subprocess module and stdin and stdout but it close file2.py after one input and I got error for second input.我使用了子进程模块以及标准输入和标准输出,但它在一次输入后关闭了 file2.py,第二次输入出现错误。

You can use import .您可以使用import Name the file from which you want to open file2.py as file.py and enter your code there, with import file2 at where you want file2.py to be opened.将要从中打开 file2.py 的文件命名为 file.py 并在此处输入您的代码,在您希望打开 file2.py 的位置使用import file2 Save the file in the same directory as file2.py.将文件保存在与 file2.py 相同的目录中。 So, file.py should look like this:所以,file.py 应该是这样的:

{code}
import file2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将一个python脚本的输出作为另一个python脚本的用户输入 - How can i take the output of one python script as user input for another python script 如何在Python中将用户输入从一个脚本传递到另一个脚本? - How to pass user input from one script to another in Python? 如何在另一个 python 脚本中使用来自 1 个 python 脚本的输入 - How to use the input from 1 python script in another python script 如何从一个 Python 脚本发送信号以触发另一个脚本? - How can I send a signal from one Python script to trigger another? 如何将数据从python脚本发送到Matplotlib? - How can I send data from a python script to Matplotlib? 如何将Python脚本中的JSON对象发送到jQuery? - How can I send a JSON object from a Python script to jQuery? 我如何从 python 读取特定终端 output 作为另一个脚本的输入 - How can i read a particular terminal output from python as input in another script 如何将一个 python 脚本的输入也分配为另一个脚本的输入 - How do I assign an input from one python script to be an input in another script as well 如何在 Python 脚本中运行另一个 Python 脚本? - How can I run another Python script in a Python script? 如何将 django 网站用户输入数据发送到外部 python 脚本 - how to send django website user input data to external python script
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM