3

Say I have a python code base organized like so:

./mod/:
 ./__init__.py
 ./main/main.py
 ./main/__init__.py
 ./mytest/__init__.py

The file

mod/main/__init__.py

is empty. And

$ cat mod/main/main.py 
 import sys
 import mytest
 def main(argv):
 mytest.test()
 return
 if __name__ == '__main__':
 sys.exit(main(sys.argv[1:]))

And

$ cat mod/mytest/__init__.py 
def test():
 print('test worked!')

As expected, this works (from within the directory "mod"):

$ python3 -m main.main
test worked!

Now, I want to remove all the .py files, and still be able to run the command that I have above - or something very similar. "Very similar" is defined by not having to change my code structure at all - or, if that cannot be done, as little as possible.

How can I achieve this?

asked Oct 28, 2015 at 18:50
4
  • 2
    Why do you want to do that? Commented Oct 28, 2015 at 18:56
  • 2
    If you're hoping to prevent people from reading your code this way, forget it. .pyc files aren't obfuscated and almost can't be. Python even comes with a Python disassembler built in, so people who can run your code don't even need to download any additional tools to start taking it apart. Commented Oct 28, 2015 at 19:09
  • The only reason to do this is to addressing handling questions coming from from edited scripts being run. People edit, forget, run, ask. This way I can create the pyc files, and if there is a question on what I created - I can handle it - else people can figure out that they have edited the code and should address their question themselves. Commented Oct 28, 2015 at 19:15
  • Wouldn't you also be limiting your user base to people using the same architecture you compiled it on in the first place? Commented Oct 28, 2015 at 19:15

1 Answer 1

5

the

compileall

utility with the "-b" option did the thing for me.

answered Oct 28, 2015 at 19:12
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.