Macos – rm file “Operation not permitted” even when sudo


Im working with python 2.7 and I was having import errors with random, So I checked the path and noticed it was using random.pyc instead of

I tried to remove the file but it won't work.

sudo rm -rf /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.pyc

and rm -R gives me this error even when I say y

sudo rm -R /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.pyc
override rw-r--r--  root/wheel restricted,compressed for /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.pyc? y
rm: /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/random.pyc: Operation not permitted

What should I do?

Edit: My whole "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/" is full of .pyc and .pyo files and import doesn't work on any of them (unless I use idle)

Best Answer

First: This is normal. .pyc files are precompiled Python files. They contain the same data as the .py files adjacent to them. If you are having problems with the random module, you are probably doing something else wrong, and you should probably ask a question about your code on Stack Overflow.

More generally: You cannot, and should not, modify files under the /System directory. They are part of the operating system -- modifying them will cause your computer to work incorrectly.

macOS prevents system files from being modified (other than by system updates) using a mechanism called System Integrity Protection, or SIP. It is possible to disable SIP, but this should generally not be necessary, even for developers.