"Permission Denied" trying to run Python on Windows 10

271,484

Solution 1

As far as I can tell, this was caused by a conflict with the version of Python 3.7 that was recently added into the Windows Store. It looks like this added two "stubs" called python.exe and python3.exe into the %USERPROFILE%\AppData\Local\Microsoft\WindowsApps folder, and in my case, this was inserted before my existing Python executable's entry in the PATH.

Moving this entry below the correct Python folder (partially) corrected the issue.

The second part of correcting it is to type manage app execution aliases into the Windows search prompt and disable the store versions of Python altogether.

manage app execution aliases

It's possible that you'll only need to do the second part, but on my system I made both changes and everything is back to normal now.

Solution 2

Research

All of the files in %USERPROFILE%\AppData\Local\Microsoft\WindowsApps are placeholders that point to files that are actually located somewhere in C:\Program Files\WindowsApps, which happens to be denied permissions completely.

It appears I was on the right track with my statement made in my duplicate of this problem:

"Seems like they didn't really think about the distribution method screwing with permissions!"

Source: Cannot install pylint in Git Bash on Windows (Windows Store)

Permissions are screwed up royally because of the WindowsApps distribution method:

enter image description here enter image description here enter image description here Interestingly it says that the "Users" group can read and execute files, as well as my specific user, but the Administrators group can only List folder contents for some hilariously unfathomable reason. And when trying to access the folder in File Explorer, it refuses to even show the folder contents, so there's something fishy about that as well.

Interestingly, even though executing python in CMD works just fine, the "WindowsApps" folder does not show up when listing the files in the directory it resides in, and attempting to navigate into the folder generates a "Permission denied" error:

enter image description here

Attempting to change the permissions requires changing the owner first, so I changed the owner to the Administrators group. After that, I attempted to change the permissions for the Administrators group to include Full control, but it was unable to change this, because "access was denied" (duh, Micro$ucks, that's what we're trying to change!).

enter image description here

This permission error happened for so many files that I used Alt+C to quickly click "Continue" on repeat messages, but this still took too long, so I canceled the process, resulting in this warning message popping up:

enter image description here

And now I am unable to set the TrustedInstaller user back as the owner of the WindowsApps folder, because it doesn't show up in the list of Users/Groups/Built-in security principles/Other objects. *

enter image description here

*Actually, according to this tutorial, you can swap the owner back to TrustedInstaller by typing NT Service\TrustedInstaller into the object name text box.

Solution

There is no solution. Basically, we are completely screwed. Classy move, Microsoft.

Solution 3

This appears to be a limitation in git-bash. The recommendation to use winpty python.exe worked for me. See Python not working in the command line of git bash for additional information.

Solution 4

This issue is far too common to still be persistent. And most answers and instructions fail to address it. Here's what to do on Windows 10:

  1. Type environment variables in the start search bar, and open Edit the System Environment Variables.

  2. Click Environment Variables...

  3. In the System Variables section, locate the variable with the key Path and double click it.

  4. Look for paths pointing to python files. Likely there are none. If there are, select and delete them.

  5. Create a new variable set to the path to your python executable. Normally this is C:\Users\[YOUR USERNAME HERE]\AppData\Local\Programs\Python\Python38. Ensure this by checking via your File Explorer.

    Note: If you can't see AppData, it's because you've not enabled viewing of hidden items: click the View tab and tick the Hidden Items checkbox.

  6. Create another variable pointing to the Scripts directory. Typically it is C:\Users\[YOUR USERNAME HERE]\AppData\Local\Programs\Python\Scripts.

  7. Restart your terminal and try typing py, python, python3, or python.exe.

Solution 5

Simple answer: replace python with PY everything will work as expected

Share:
271,484

Related videos on Youtube

Izadi Egizabal
Author by

Izadi Egizabal

I've been developing software since I was a kid, professionally for the last twenty years. Most recently, I've founded my own company, Sauve Software Solutions, and I've been working as a developer for hire since then.

Updated on October 09, 2021

Comments

  • Izadi Egizabal
    Izadi Egizabal over 2 years

    Seems as though an update on Windows 10 overnight broke Python. Just trying to run python --version returned a "Permission Denied" error. None of the three updates; KB4507453, KB4506991, or KB4509096 look like they'd be the culprit but the timing of the issue is suspicious. Rather than messing with rolling back, I'm hoping there's a simpler fix that I'm missing.

    The permissions on python are "-rwxr-xr-x" and I haven't changed anything besides letting the Windows update reboot machine after installing last night's patches.

    According to the System Information, I'm running 10.0.18362

    Should also note that this is happening whether I (try) to execute Python from git-bash using "run as administrator" or not, and if I try using PowerShell, it just opens the Windows store as if the app isn't installed so I'm thinking it can't see the contents of my /c/Users/david/AppData/Local/Microsoft/WindowsApps/ folder for some reason.

    I've also tried to reinstall Python 3.7.4, but that didn't help either. Is there something else I should be looking at?

    • Ant
      Ant almost 5 years
      Probably worth checking the Microsoft website. I read somewhere, sorry can't remember where, that other people were having problems with this update - blank screen I think. Anyway, waiting until they fix it in a day or two might turn out to be the answer.
    • Eryk Sun
      Eryk Sun almost 5 years
      BTW, "permissions on python are '-rwxr-xr-x'" is probably meaningless in Windows. That's something fake reported by a Unix-like environment such as MSYS2 or git-bash.
    • Izadi Egizabal
      Izadi Egizabal almost 5 years
      Nope. This is the Python package from python.org. Same on that's been working for years without any issue and just started acting up with the latest Windows patch.
    • Shanks
      Shanks almost 5 years
      Did you find a solution? I have the same issue.
    • Izadi Egizabal
      Izadi Egizabal almost 5 years
      Unfortunately, no. I've been living with it as I do most of my work in Docker. If I do, I'll be sure to post my findings!
    • Shanks
      Shanks almost 5 years
      Thanks! I will drop one here if I figure it out.
    • MarkHu
      MarkHu over 3 years
      See also superuser.com/a/1576801/93082 solution #1 mklink \Python39\python3.exe \Python39\python.exe
  • Ryan
    Ryan almost 5 years
    I have in my local user's path variable two references to this same folder, one with a literal path, the other with a relative path: C:\Users\blah\AppData\Local\Microsoft\WindowsApps is the 4th entry, and %USERPROFILE%\AppData\Local\Microsoft\WindowsApps is the last entry in the list. -- On the other hand, my system path variable contains no reference to the WindowsApps folder at all. Additionally, neither one of these contains a reference to any other Python paths.
  • Ryan
    Ryan almost 5 years
    The python executable works in CMD even without Administrator privileges. The problem is that it doesn't work in Git Bash, which is an essential tool for programmers and is the default, and it works with every other distribution method for Python, which means it should [be made to] work here as well.
  • jcopenha
    jcopenha almost 5 years
    (Microsoft employee and CPython core developer here) You definitely only need to do the second part. There have been a couple of bugs related to upgrading apps resetting aliases which will be fixed in the next stable update, so it should be a one-time fix by then. While you're getting Insiders updates you may need to do it a couple more times.
  • jcopenha
    jcopenha almost 5 years
    (Microsoft employee and CPython core developer here) There's a bug right now I'm working on getting fixed in Windows where you can launch executables in this location but only if you have the global alias enabled (in Manage App Execution aliases). I'm trying to get it changed so that you only need to have installed the app for the current user.
  • jcopenha
    jcopenha almost 5 years
    Also, the "Access Denied" issue is a Git Bash bug (or whoever maintains their Bash port... I'm not sure myself whose it is). And launching the Store is a new feature to help people install Python - if you've added it to PATH using the regular installer it should take precedence over the new redirector, but if not you've discovered above how to disable it.
  • Eryk Sun
    Eryk Sun almost 5 years
    We can read the permissions via this dialog because it's owned by a dllhost.exe instance (hosting the security shell extension, rshx32.dll) that's running with administrator access and thus has "list folder contents" access (i.e. execute; sync; and read data, attributes, and permissions). The entry for the users group is not for a normal access check. It's conditional on the existence of a WIN://SYSAPPID security attribute, i.e. any app. The subfolder for each app grants read access to users, but execute access is conditional on running via the app link that sets a custom access token.
  • jcopenha
    jcopenha almost 5 years
    (To clarify my own comment - "if you've added it [Python, not the Store launcher] to PATH using the regular installer...")
  • Ryan
    Ryan almost 5 years
    @ErykSun That is the first informed answer I've ever seen about windows execution and permissions. Thanks.
  • Ryan
    Ryan almost 5 years
    I'm actually still dealing with the fallout from trying to change permissions on the folder. Certain WinApps stopped launching altogether, until I followed these directions: (superuser.com/questions/1288014/…) but now they give error messages on launching. I used an older Win10 USB image so it probably set the permissions incorrectly.
  • kennyB
    kennyB over 4 years
    I just had to to the second part to fix this problem for me.
  • Niels
    Niels over 4 years
    For me I had to also add python to my path (C:\Users\YourUsernameHere\AppData\Local\Programs\Python\Pyt‌​hon37) for git bash to find python
  • ThaJay
    ThaJay over 4 years
    I never did anything with the winsows store regarding Python. Python is also not listet in 'App execution aliases'.
  • John W. Clark
    John W. Clark over 4 years
    Adding it to my my path and moving it to the top was my solution, just like Neils did. The fact that I can't just rename the stub for the windows store stub is pretty annoying.
  • Mark Simpson
    Mark Simpson over 4 years
    This just caught me out, thanks for the good explanation. We use git bash to run shell scripts to invoke an emscripten build, and as part of the standard emscripten scripts it tries to divine your python version via $(which python) ... which immediately blows up.
  • Ryan
    Ryan over 4 years
    @MarkSimpson glad this thread helped. Not sure what I may have written. Be sure to thank the Microsoft employees that posted real answers in the comments here.
  • TheCodeLearner
    TheCodeLearner almost 4 years
    Thank you so much this help me solve my problem running flask from cmd (python)
  • worc
    worc over 3 years
    doing just the second part crashes git bash on startup with a "There are no available terminal" error. this kind of insanity makes dual-booting and dealing with windows 10's chronic inability to share a system nicely look inviting
  • worc
    worc over 3 years
    this bug still seems to be lurking over a year later... as far as i can tell getting GitBash, python and windows 10 to work together is still a dead end
  • Vida
    Vida over 3 years
    Just in case it helps.. I got this to work very simply by doing alias python='winpty python.exe' on the command line in git bash... On Windows 10 and after doing parts 1 and 2 above...
  • raresm
    raresm over 3 years
    For me, the problem was that python was moved to C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.8_3.8.17‌​76.0_x64__qbz5n2kfra‌​8p0\python3.8 instead of the default path. I removed all paths both from sublime and env variables, reinstalled, added new paths and now it works. The only problem is that it's a clean install and I have to reinstall all dependencies
  • Andrew Bullock
    Andrew Bullock over 3 years
    What a complete waste of humanity's time. Whichever parties conspired to make this be the case need to seriously consider their career choices. Thanks for the fixes
  • Matthew
    Matthew over 3 years
    I definitely have this issue running python from bash but not from powershell - however I don't think the bug you linked is the issue. Much more likely to be this: github.com/msys2/Msys2-packages/issues/1943
  • Dave Ankin
    Dave Ankin over 3 years
    the thing is, microsoft has no right to invent new operating system features, since those would not be standard, thats something only linux/posix can do. so msys2, kudos to them for putting up with all the msft they have, and im sure theyll figure this one out too, but its not their fault its msft
  • Biskrem Muhammad
    Biskrem Muhammad over 3 years
    this actually worked, this is like what the hell situation. lol :D
  • Josiah
    Josiah over 3 years
    huh, worked for me when running -m venv. Looks like I may have python3 installed in two locations causing this. I suspect one from the Windows store and one installed by vscode or something like that. ``` $ which python /c/Users/me/AppData/Local/Programs/Python/Python39/python $ which python3 /c/Users/me/AppData/Local/Microsoft/WindowsApps/python3 ```
  • Fabii
    Fabii over 3 years
    You're a life saver, and what the hell Microsoft ?
  • Martin
    Martin about 3 years
    What about users who installed Python via Anaconda? I did all every step, a system variable pointing to my python installation exists in system variables and the problem still persists.
  • dawn
    dawn about 3 years
    so that´s a new thing implemented in py 3.9.4?
  • Mirza Sisic
    Mirza Sisic about 3 years
    Adding in to the path manually and disabling those aliased did it for me Python 3.9.4
  • secavfr
    secavfr about 3 years
    Could anyone add more context on what is PY ?
  • Jono
    Jono about 3 years
    I did this, after I installed Python - it was not installed initially, hence the error :)
  • Shakeel
    Shakeel about 3 years
    Then you should have seen installation error.
  • trex
    trex almost 3 years
    This helped me to get python running, though python was installed in C:\Program Files\Python36\. Make sure this is added to the top (beginning) of your PATH, otherwise Windows may try to run the Windows Store versions of python instead of the version you installed. Also, I never got it working in git bash, but it will work in cmd or powershell.
  • Kevin
    Kevin over 2 years
    There are other answers suggesting winpty, but their reasoning is unrelated, wrong, or out-dated. I felt an updated summary for 2021 could be useful.
  • Frank Guo
    Frank Guo over 2 years
    worked for me, not sure whey
  • Frank Guo
    Frank Guo over 2 years
    THIS IS THE CORRECT WAY TO DO!! THANKS!!