You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows PowerShell中Python input函数无响应的原因及解决方法

PowerShell中Python单行代码无响应的原因与解决办法

问题根源

Windows PowerShell(5.1版本)的引号解析逻辑和CMD、Linux Shell存在差异:
当你用双引号包裹python -c的代码参数时,PowerShell会把双引号内的分号;识别为自身的命令分隔符,导致传递给Python的代码被拆分——实际只有a=input()被执行,print(a)根本没传给Python,所以输入回车后程序卡在那里,没有输出也不退出。

解决办法

直接用下面任意一种方式即可解决:

1. 用单引号包裹Python代码

PowerShell不会解析单引号内的内容,能完整传递代码给Python:

python -c 'a=input();print(a)'

2. 转义双引号内的分号

如果一定要用双引号,用PowerShell的转义符`(反引号,键盘左上角)转义分号:

python -c "a=input()`;print(a)"

3. 借助CMD执行

通过cmd /c让CMD来处理命令,完全复用CMD下的执行逻辑:

cmd /c "python -c ""a=input();print(a)"""

这里内部的双引号用两个双引号转义,确保PowerShell正确传递给CMD。

附:当前PowerShell版本信息

Name                           Value
----                           -----
PSVersion                      5.1.19041.3031
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.19041.3031
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 11:02:42