简体   繁体   中英

Can't run python on powershell

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.

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

My laptop returned to this "C:\\cygwin64\\bin\\python". so what should I do next to fix:

Program 'python' failed to run: No application is associated with the specified file for this operationAt line:1 char:1 + python "c:\\Users\\kimtu\\Desktop\\Fnds of Machine Learning\\PA2\\pa2_templ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~. At line:1 char:1 + python "c:\\Users\\kimtu\\Desktop\\Fnds of Machine Learning\\PA2\\pa2_templ ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (:) [], ApplicationFailedException + FullyQualifiedErrorId : NativeCommandFailed

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM