3

When I type python in PowerShell I get an error like:

Program 'python' failed to execute: The system cannot find the file specified
At line:1 char:11
+ python.exe <<<< .
At line:1 char:1
+  <<<< python.exe
    + CategoryInfo          : ResourceUnavailable: (:) [], ApplicationFailedException
    + FullyQualifiedErrorId : NativeCommandFailed

I have added C:\Python27 and its sub directories to PATH. I can run it by typing python.exe. Can run scripts the same way.

I installed Python 2.7.11 64 bit.

Albino Zenci
  • 33
  • 1
  • 1
  • 6

2 Answers2

11

The only way I can reproduce your error is if I replace my python.exe file with a new empty text file named python.exe.

First Steps

I notice you mentioned that you added C:\Python27 and all subdirectories to your PATH. I would first suggest to remove all the subdirectories (just leaving the parent directory) from your path. Make sure you restart Powershell after making this change to pull in the new values. Then test it to see whether this has resolved the issue.

Alternative solution

The other possibility is that somewhere on your path you have either a corrupted python.exe or some other cmdlet, function or script file that is shadowing your python installation. You could try running from within powershell:

Get-Command python | Select-Object -ExpandProperty Definition

to see what powershell is actually referencing.* If the only value returned is C:\Python27\python.exe then I would probably suggest a reinstall.

Notes:

[*] - Taken from https://stackoverflow.com/a/16949127/1468125

Community
  • 1
  • 1
Gavin
  • 1,070
  • 18
  • 24
  • I think I have found the problem. Tried your alternative solution first. It returned C:\Python27\python.exe and E:\cygwin64\bin\python. So I don't want to delete cygwin's path. What should I do? Would changing the order of them solve the problem? Because cygwin directory is written before the Python's. – Albino Zenci Apr 04 '16 at 21:06
  • Actually now I tried changing their order and it worked! Thank you so much this was bugging me for days now :D – Albino Zenci Apr 04 '16 at 21:11
  • 2
    In my case there was a 0KB file at `%AppData%\Local\Microsoft\WindowsApps\python.exe` that was overriding my standard python 3.9 exe. – Laurence Aug 13 '21 at 13:19
0

I ran into this problem recently. To fix it, I had to edit the PATH environment variable to ensure my installed location of Python 3 came before C:\cygwin64\bin, which has a softlink of python to an older version. Technically one could also launch the Cygwin install utility and remove the older python to fix it as well.

However all I did was to right click on the My Computer icon, select properties, then clicked on Advanced systems settings in order to open the Windows System Properties dialog. From there I selected the Environment Variables button. Then I saw, again in my case, that C:\cygwin64\bin was listed in the PATH environment variable twice, once in the user variables section and also once in the System variables section. I re-ordered Python38 to be first in my user variables PATH environment and deleted the C:\cygwin64\bin entry from the System variables PATH environment. Then clicked OK and opened a new Terminal in VS code, and all is good.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Michael Behrens
  • 911
  • 10
  • 8