Errno 13 whenever .py file is run with sudo on Linux

153 views Asked by At

Whenever I run any .py script with sudo, even simple ones, I get the error "[Errno 13] Permission denied" on Linux. The reason I want to run a Python script with sudo in the first place is because the "keyboard" module in one of my files needs root permissions to function properly.

import keyboard

def record():
    events = keyboard.record(until='esc')
    return events

def process_recorded_letters(events):
    letter_events = ""

    for key in events:
        if key.event_type == 'down' and key.name != 'shift' and key.name != 'space':
            letter_events += key.name
    
    return letter_events

events = record()
print(process_recorded_letters(events))

I did the simple things like checking that I was actually the root user, checking if the files actually existed, and changing the file permissions with chown and chmod, but nothing worked, I kept on getting the Errno 13 error. I also tested running the script on a different Linux distro and I still got Errno 13, so it's definitely some problem on my end.

1

There are 1 answers

0
Sashank Bhagavatula On

I fixed it. I was using a OneDrive client and it was messing with Python's ability to access the file. Just copying and pasting the file somewhere else fixed the error. Thanks for the help everyone!