2

This question is just out of curiosity: Is there any way to create a python file (.py) which is locked but executable?

Meaning if I try to access (open) it, it will ask for a key so that the source code can't be seen or edited. But if I execute it through a terminal, it can be run. For e.g.:

>>> python locked_file.py
Type an integer: 

PS: Just for clarification. My question is not about converting a .py file to an executable (.exe) or .pyc or encrypted or obfuscated file.

  • What is the enviroment you want to run on? (which OS) – Guy Aug 12 '22 at 17:42
  • 3
    No, of course not. Python source code is just text, if Python can read it then so can any other program. – jasonharper Aug 12 '22 at 17:47
  • @Guy any OS which has means to execute a .py file. – TheMaskedTitan Aug 12 '22 at 17:51
  • 2
    As far as Linux goes, this discussion might be useful: https://unix.stackexchange.com/questions/16623/file-permission-execute-only/77538 – slothrop Aug 12 '22 at 17:55
  • 2
    It seems like this will be possible for OpenBSD - https://unix.stackexchange.com/a/546810 – Guy Aug 12 '22 at 17:58
  • Another duplicate https://stackoverflow.com/questions/71652758/how-do-i-make-my-python-project-files-unreadable. – DavidW Aug 12 '22 at 18:07
  • @DavidW, as I mentioned at the last of my question. I am not asking about converting it into some unreadable or an obfuscated file. It is about locking the file like how you lock a pdf. – TheMaskedTitan Aug 12 '22 at 18:14
  • @TheMaskedTitan that's not quite the same - PDFs can have an owner password but *"the owner password does not [encrypt the file], instead relying on client software to respect these restrictions. An owner password can easily be removed by software, including some free online services."* (https://en.wikipedia.org/wiki/PDF#Encryption_and_signatures) – slothrop Aug 12 '22 at 18:18
  • @slothrop, I know. I am not asking this for commercializing or for distributing the code. I just wanted to have it locked (just a first and most basic layer of security) and share it among my friends to have fun. Nothing more. – TheMaskedTitan Aug 12 '22 at 18:22
  • 2
    @TheMaskedTitan - the thing is that a .py file is just text and can be read in any text editor: it's not like a PDF which needs a more sophisticated app to (meaningfully) edit, so that Adobe can at least make it a pain for you to get the ability to edit it. On Unix-like systems, there are things you could do (with some caveats) that get close to what you're looking for through careful use of file permissions and sudo. – slothrop Aug 12 '22 at 18:29
  • 1
    One more thought: if you want to share it with your friends for fun (which is good!) you could consider making a web-based app. So they interact with it through the browser, the Python code itself is running on a server and they won't see any of the source code. – slothrop Aug 12 '22 at 18:31
  • @slothrop, thanks. It is really a good idea. Sure, I will check it out. – TheMaskedTitan Aug 12 '22 at 18:33

0 Answers0