1

I have compiled this visible_code.py file into .pyc compiled byte code file, then I am running the function fun from .pyc file.

# visible_code.py def fun(): print("Hello") 
# convet to bytecode import py_compile py_compile.compile("visible_code.py", "secret_code.pyc") 

Then I am deleting this file called visible_code.py and trying to run the function fun from secret_code.pyc in main.py file.

# main.py from secret_code import fun fun() 

Everything works perfect until I change the python version, I get error regarding magic numbers. I have used this image from a different file structure.

enter image description here

Is there is any way that I can create this secret_code.pyc file for all the python version or at least python3. How to get rid of this magic numbers error.

10
  • 4
    Not possible. You have to compile new pyc with each Python interpreter. Commented Nov 11, 2022 at 16:11
  • Why do you want to remove the .py file ?, The .pyc is trivially turned back to .py if you know the magic number (and sometimes without it), which you should know to run the code anyway, so using .pyc file instead of .py has no advantage in terms of code obfuscation. Commented Nov 11, 2022 at 16:14
  • 1
    How to obfuscate Python code effectively? Commented Nov 11, 2022 at 16:21
  • 1
    ...really, though, there's no such thing as effective obfuscation. If a user can run it on a general-purpose computer they control, they can inspect what that computer is doing. (There's thesis-level research work on encrypted computation, but I'll believe it as practical reality only when I see it). Commented Nov 11, 2022 at 16:22
  • 1
    Among the above-linked question's answers, though, see in particular stackoverflow.com/a/58979147/14122 and stackoverflow.com/a/68214794/14122 Commented Nov 11, 2022 at 16:24

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.