'Start Python from Windows PowerShell

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.



Solution 1:[1]

Running any executable using Powershell (not just Python.exe) is possible using multiple approaches, but what has worked best for me is iex.

The basic steps I follow are:

  • Find the path to executable
  • Construct command string
  • Run iex on it. Use & to account for spaces in file paths.

To find the executable, I generally use get-command. This searches PATH:

    if (get-command curl.exe) {
        $exePath = "curl.exe"
    }

    $Cmd = '"'+ $exePath + '"' + ' args'
    iex "& $curlCmd"

Hope this helps.

Solution 2:[2]

Adding "C\Python27" in "System -> Advanced System settings -> Environment variable" to the system variables solved my issue. For me it was only in user variables.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vish
Solution 2 Spilos