[Python] Start Python from Windows PowerShell

Discussão em 'Python' iniciado por Stack, Maio 5, 2025.

  1. Stack

    Stack Membro Participativo

    I know this looks like a question answered thousands of time, but none of the traditional answers concerning the environment PATH are working.

    I want to run the following in Windows 7, in Powershell:

    python mycode.py


    However, Powershell returns an error, stating that python not recognized as an applet, function, application,...

    These are my path variables:

    Users variables:

    PATH
    C:\Python27\Lib\site-packages\PyQt4;
    C:\Python27;
    C:\Python27\DLLs;
    C:\Python27\Scripts;
    C:\Python27\Lib\site-packages\vtk;
    C:\Python27\gnuplot\binary;
    C:\Program Files (x86)\pythonxy\SciTE-3.3.2-3;
    C:\Program Files (x86)\pythonxy\console;
    C:\MinGW32-xy\bin;
    C:\Program Files (x86)\pythonxy\swig;
    C:\Program Files (x86)\pythonxy\gettext\bin

    PATHEXT
    .PY;.PYW

    PYTHON_INCLUDE
    C:\Python27\include

    PYTHON_LIB
    C:\Python27\libs\python27.lib


    and System variables:

    PATH
    C:\Python27\Lib\site-packages\PyQt4;
    C:\Python27;
    C:\Python27\DLLs;
    C:\Python27\Scripts;
    C:\Python27\Lib\site-packages\vtk;
    C:\Python27\gnuplot\binary;
    C:\Program Files (x86)\pythonxy\SciTE-3.3.2-3;
    C:\Program Files (x86)\pythonxy\console;
    C:\MinGW32-xy\bin;
    C:\Program Files (x86)\pythonxy\swig;
    C:\Program Files (x86)\pythonxy\gettext\bin;
    C:\WINDOWS\system32;
    C:\WINDOWS


    I tried the following:

    $env:path = $env:path + ";C:\Python27\"
    [Environment]::SetEnvironmentVariable("Path", "$env:path;C:\Python27", "User")


    without success. (I tried starting a new Powershell session, of course, and even tried to reboot my PC)

    Could it be that PowerShell doesn't properly read the path variable, or I don't have some permission? I am lost, especially knowing that this work fine on another Windows 7 install. I note that typing:

    python.exe


    ...opens a Python terminal as expected.

    Edit : Ok I tried the following test.py code :

    # -*- coding: utf-8 -*-
    print "Hello"
    input()

    python.exe test.py


    open a new terminal with "Hello" in it and wait for my input but I don't want that, I expect the normal behaviour, with "Hello" printed in PowerShell, error message in PowerShell and so on.

    Edit2 : I noticed that the "Path" variable given in PowerShell by: Get-ChildItem Env

    Is not equal to the one in the W7 options "System -> Advanced System settings -> Environment variable" . it was only :

    ;C:\Python27


    Like if my previous command line [Environment]::SetEnvironmentVariable("Path", "$env:path;C:\Python27") just wrote in it. Fixing this didn't solve my problem either.

    Continue reading...

Compartilhe esta Página