该脚本按预期运行powershell
:
# scripts.py
x = input("type your input: ")
print(f"your input is: {x}")
但是一旦你将它包装成一个模块:
class CSV{
[string] $pythonScript
CSV([string] $pythonPath){
$this.pythonScript = $pythonPath
}
[void] Run(){
python $this.pythonScript
}
}
交互性未打印。要运行此类,请按如下方式创建一个新的 ps1 文件并运行它
# run.ps1
using module .\module.psm1
$newCSV = [CSV]::new(".\script.py")
$newCSV.Run()
在终端中:
PS C:\SE_temp_dir> .\run.ps1
test // typed from user input
PS C:\SE_temp_dir>
请注意,没有打印“输入您的输入”或“您的输入是测试”。我尝试了许多其他方法:
### module.psm1:
python $this.pythonScript | Write-Output
python $this.pythonScript | Write-Host
python -v $this.pythonScript | Write-Host
### script.py:
print(f"your input is: {x}", flush=True)
import sys; sys.stdout.write(f"your input is: {x}")
但它们都不起作用。为什么input()/print()
在普通的 PowerShell 中以交互方式调用 Python 脚本可以正常工作,但在 PowerShell 类的方法中运行时却不行?这是 PowerShell 作用域问题还是主机行为问题?