How to force creation of a .pyc?

M

mrstephengross

I would like to distribute a python program, but only in .pyc form (so
that people cannot simply look at my code). Is there a way to do this?
I've read up a little on the logic by which python creates .pyc's, and
it sounds like python requires the main executed program to be in .py
format. Any ideas?

Thanks
--Steve ([email protected])
 
N

Nick Smallbone

mrstephengross said:
I would like to distribute a python program, but only in .pyc form (so
that people cannot simply look at my code). Is there a way to do this?
I've read up a little on the logic by which python creates .pyc's, and
it sounds like python requires the main executed program to be in .py
format. Any ideas?

If your main program is in main.py, you could perhaps launch it with
python -c "import main", or make another file which just does import
main. Then you could keep just the .pycs/.pyos.

But it's possible for a determined user to recover a lot of information
about the source code. For example, if I write

def foo(a):
for i in range(a):
print i

then
2 0 SETUP_LOOP 25 (to 28)
3 LOAD_GLOBAL 0 (range)
6 LOAD_FAST 0 (a)
9 CALL_FUNCTION 1
12 GET_ITER 16 STORE_FAST 1 (i)

3 19 LOAD_FAST 1 (i)
22 PRINT_ITEM
23 PRINT_NEWLINE
24 JUMP_ABSOLUTE 13 31 RETURN_VALUE

where 0..12 makes an iterator for range(a), 16..19 updates i and 22..23
prints it out. It would be possible to turn it into equivalent source
code. Decompyle (from
http://ftp.debian.org/debian/pool/main/d/decompyle/decompyle_2.3.2.orig.tar.gz)
can even do that.
 
J

Jeffrey Schwab

mrstephengross said:
I would like to distribute a python program, but only in .pyc form (so
that people cannot simply look at my code). Is there a way to do this?
I've read up a little on the logic by which python creates .pyc's, and
it sounds like python requires the main executed program to be in .py
format. Any ideas?

Make a dummy script to import your main module. The resulting pyc can
be fed directly to the Python interpreter.

<---------------------------------------------------------------------->
# main.py : My main source code. I cleverly will distribute only the
# byte code, such that no one knows my secrets. D'oh! You caught me
# monologging again...

if __name__ == "__main__":
print "Ha! Now you can't see me."
<---------------------------------------------------------------------->
# dummy.py : Imports the main module to create main.pyc.

import main
<---------------------------------------------------------------------->
[some client's prompt]% python main.py
Ha! Now you can't see me.
 
P

Peter Hansen

mrstephengross said:
I would like to distribute a python program, but only in .pyc form (so
that people cannot simply look at my code). Is there a way to do this?
I've read up a little on the logic by which python creates .pyc's, and
it sounds like python requires the main executed program to be in .py
format. Any ideas?

Use the compileall module (e.g. function compile_dir) and run the .pyc
file(s) directly. Python doesn't care if the main one is in .py format
and will happily run a .pyc if there's no matching .py file around.

-Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top