[Python] Python script with input() and print() do not print when run from PowerShell class...

Discussão em 'Python' iniciado por Stack, Abril 30, 2025.

  1. Stack

    Stack Membro Participativo

    This script in question runs as expected from powershell:

    # scripts.py
    x = input("type your input: ")
    print(f"your input is: {x}")


    But once you wrap it into a module:

    class CSV{
    [string] $pythonScript

    CSV([string] $pythonPath){
    $this.pythonScript = $pythonPath
    }

    [void] Run(){
    python $this.pythonScript
    }
    }


    The interactivity is not printed. To run this class, create a new ps1 file as follows and run it

    # run.ps1
    using module .\module.psm1
    $newCSV = [CSV]::new(".\script.py")
    $newCSV.Run()


    In the terminal:

    PS C:\SE_temp_dir> .\run.ps1
    test // typed from user input
    PS C:\SE_temp_dir>


    Notice that no "type your input" or "your input is test" is printed. I have tried many other alternatives:

    ### 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}")


    But none of them worked. Why does calling a Python script with input()/print() work interactively in plain PowerShell, but not when run inside a method of a PowerShell class? Is this a PowerShell scoping or host behavior issue?

    Continue reading...

Compartilhe esta Página