简体   繁体   English

重新启动脚本后命令提示符(Windows)和 python 脚本出现问题

[英]Issue with command prompt (windows) and python script after restarting script

I am trying to write a CLI program with python in windows.我正在尝试在 windows 中使用 python 编写 CLI 程序。 I recently came across an issue with argparse and my work-around was to clear the command prompt and re-run my script with the same arguments using this code:我最近遇到了 argparse 的问题,我的解决方法是清除命令提示符并使用以下代码使用相同的 arguments 重新运行我的脚本:

sys.stdout.flush
os.execl(sys.executable, 'python', __file__, *sys.argv[1:])

This works perfectly until I try to input something into my program.在我尝试在我的程序中输入一些东西之前,这非常有效。 I have this perpetual loop in my code which allows me to input a string and have it echoed back to me:我的代码中有这个永久循环,它允许我输入一个字符串并将其回显给我:

while True:
    action = input("> ")
    print(f'typed {action}')

but once I reach that point in the code, it does not work, this is my output:但是一旦我到达代码中的那个点,它就不起作用了,这是我的 output: 错误代码输出

This is the expected output up until after the red "Ready."这是预期的 output 直到红色“就绪”之后。 for some reason, the command prompt alternates between an input to my script, and an input to the command prompt, which are both prefaced with the wrong text, the ">" should be an input to my script, but that input is being sent to the command prompt, and the "...\raspi>" should be an input to the command prompt, but that input is being sent to my script.出于某种原因,命令提示符在我的脚本的输入和命令提示符的输入之间交替,它们都以错误的文本开头,“>”应该是我的脚本的输入,但是正在发送该输入到命令提示符,并且“...\raspi>”应该是命令提示符的输入,但是该输入被发送到我的脚本。 Anyone have any ideas as to why this is happening?任何人对为什么会发生这种情况有任何想法? Why it's alternating between my script and regular command prompt?为什么它在我的脚本和常规命令提示符之间交替出现?

The solution I came to was to realize that the cprint() function of the colorama.py library does not return text and instead just calls the print function (I should have been able to deduce that through the name of the function).我想到的解决方案是意识到colorama.py库的cprint() function 不返回文本,而只是调用 print function (我应该能够通过函数名称推断出)。 So all I had to do was to not set the description of my parser to a cprint() function and instead call cprint() when it's needed.所以我所要做的就是不要将我的解析器的描述设置为cprint() function ,而是在需要时调用cprint()

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

相关问题 Python:从python脚本的Windows命令提示符中执行windows命令 - Python: Executing the windows command in windows command prompt from python script 使用Windows命令提示符查找python脚本的Windows PID - Find Windows PID of a python script with Windows Command Prompt 如何在命令提示符Windows 10中运行python脚本 - How to run python script in Command Prompt Windows 10 在Windows命令提示中运行Python脚本时如何修复NameError - How to fix NameError when running Python Script in Windows Command Prompt 在 Anaconda 命令提示符中运行 Python 脚本的 Windows 快捷方式 - Windows Shortcut to Run Python Script in Anaconda Command Prompt python脚本打开Windows命令提示符并打印一些字符串 - python script to open windows command prompt and print some strings 如何在Windows命令提示符下执行sql Python脚本? - How to execute sql Python script in Windows command Prompt? 在命令提示符下运行脚本,而后不退出Python - Run script in command prompt without exiting Python after Windows上的Python中的自动重启脚本 - Self-Restarting Script in Python on Windows 是否可以从Windows命令提示符运行python脚本并同时为该脚本传递参数? - Is it possible to run a python script from the windows command prompt and pass an argument for that script at the same time?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM