Search code examples
pythonpython-3.xpasswordspassword-protection

How do I lock a python (.py) file for editing?


I wrote an automation program which logs in to a website to perform a certain task. The .py file will be given to a number of users but I don't want them to be able to read the code and see the password used for logging in. How do I make sure that they can only execute the file but not read it?


Solution

  • To lock a Python script so it cannot be edited, you compile the PY file into a PYC file. This ensures that the users are unable to make any changes to the existing code within the scripts. A PYC file is binary in nature and cannot be read directly. Below is code that compiles a PY file into a PYC file:

    import py_compile
    
    script = "C:\\temp\\myscript.py"
    py_compile.compile(script)
    

    That code would make myscript.pyc. PYC files run even if the PY files are not present.

    Source : https://support.esri.com/en/technical-article/000010321