1

Python does not work in PowerShell anymore.

I've never had any problems, until recently. CMD still recognizes the py command, but powershell doesn't recognize any of the basic python commands: py,py3,python,python3. My problem occured after I installed MinGW and added its path to the Path variable.

I have restarted my computer many times, while trying new things.

Things I've tried:

  • I have tried removing MinGW from the Path variable.
  • I have tried uninstalling MinGW.
  • I have tried reinstalling python.
  • I have tried manually adding the Python path.
  • I have tried letting Python automatically add my Python path.
  • I have tried adding the Python path to both the user environment variables and the system variables.
  • I have tried running PowerShell as administrator.

Finally, this is what my Path variable looks like now:

User Path Variable:

C:\Users\lcdew\AppData\Local\Microsoft\WindowsApps
C:\Users\lcdew\AppData\Local\GitHubDesktop\bin

System Path Variable:

C:\Program Files\Python38\Scripts
C:\Program Files\Python38
C:\Program Files\Scripts
C:\Program Files
C:\Program Files (x86)\Scripts
C:\Program Files (x86)
C:\Program Files (x86)\Razer Chroma SDK\bin
C:\Program Files\Razer Chroma SDK\bin
C:\Windows\system32
C:\Windows
C:\Windows\System32\Wbem
C:\Windows\System32\WindowsPowerShell\v1.0
C:\Windows\System32\OpenSSH
C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps
C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR
C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common
Compo
  • 36,585
  • 5
  • 27
  • 39
Boomer
  • 383
  • 2
  • 4
  • 15
  • 1
    `C:\Program Files` respectively `%ProgramFiles%` and `C:\Program Files (x86)` respectively `%ProgramFiles(x86)%` should be removed from __system__ environment variable `Path` as those two directories contain usually only subdirectories and not files with a file extension as defined in value of __system__ environment variable `PATHEXT`. – Mofi Aug 01 '20 at 14:39
  • 1
    The first five directory paths in __system__ environment variable `Path` should be `%SystemRoot%\System32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SystemRoot%\System32\WindowsPowerShell\v1.0;%SystemRoot%\System32\OpenSSH;%SystemRoot%\System32\config\systemprofile\AppData\Local\Microsoft\WindowsApps` So I strongly recommend to move the six program files directory paths down (Windows 10) respectively to left (former Windows versions) below/after the `WindowsApps` directory path. – Mofi Aug 01 '20 at 14:43
  • 2
    Restart Windows after making these modifications on __system__ environment variable `Path` to get the update applied to all processes started automatically by Windows after Windows restart. Please take also a look on [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/a/41461002/3074564) – Mofi Aug 01 '20 at 14:47
  • @Mofi I have tried your suggestion. This is my system Path variable: ```%SystemRoot%\System32;%SystemRoot%;%SystemRoot%\System32\Wbem;%SystemRoot%\System32\WindowsPowerShell\v1.0;%SystemRoot%\System32\OpenSSH;%SystemRoot%\System32\config\systemprofile\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Python38\Scripts\;C:\Program Files\Python38\;C:\Program Files\Scripts\;C:\Program Files (x86)\Scripts\;C:\Program Files (x86)\Razer Chroma SDK\bin;C:\Program Files\Razer Chroma SDK\bin;C:\Program Files\NVIDIA Corporation\NVIDIA NvDLISR;C:\Program Files (x86)\NVIDIA Corporation\PhysX\Common;``` – Boomer Aug 01 '20 at 15:24
  • But it doesn't seem to work. I restarted my computer, but PowerShell still does not recognize ```py```. – Boomer Aug 01 '20 at 15:25
  • Just happened to me overnight. Just yesterday I was testing .bat files without any issue. Today Powershell(and Powershell7) started requiring the ```.exe``` extension for no apparent reason. Entering "py" alone pops up a "Open with..." window. – 101is5 Aug 24 '22 at 11:32

2 Answers2

0

I managed to solve my problem. Apparently I accidentally deleted the PathExt variable, which contains the file extensions that the Windows command processor will look for. So after adding the following variable to the system path variables everything worked:

PathExt

.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC

However, I still don't understand why CMD worked and PowerShell didn't, maybe someone knows the answer to this.

Boomer
  • 383
  • 2
  • 4
  • 15
  • The Windows command processor `cmd.exe` has included in its code the file extensions list `.COM;.EXE;.BAT;.CMD;.VBS;.JS;.WS;.MSC` which is used on environment variable `PATHEXT` not defined at all with more extensions by default. `cmd.exe` is the only executable on Windows with a default path extension list embedded in code for the error condition that the environment variable `PATHEXT` is not defined at all. – Mofi Aug 02 '20 at 09:52
  • 1
    The library file `wshext.dll` contains the file extensions list `.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH` for the error condition that the environment variable `PATHEXT` is not defined at all. For that reason the Windows Script Host (`cscript.exe` (console version) or `wscript.exe` (Windows GUI version)) is the second program which can find executables and scripts specified with just file name without file extension on environment variable `PATHEXT` not defined in local execution environment. All other applications including Windows PowerShell depend on `PATHEXT`. – Mofi Aug 02 '20 at 09:56
0

multiple definitions for app alias may cause it in PowerShell, error is as below:

Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Manage App Execution Aliases.
kadamb
  • 1,532
  • 3
  • 29
  • 55
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 05 '22 at 06:37