473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to force creation of a .pyc?

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 (mr************@hotmail.com)

Feb 22 '06 #1
3 2430
mrstephengross wrote:
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
dis.dis(foo)
2 0 SETUP_LOOP 25 (to 28)
3 LOAD_GLOBAL 0 (range)
6 LOAD_FAST 0 (a)
9 CALL_FUNCTION 1
12 GET_ITER 13 FOR_ITER 11 (to 27) 16 STORE_FAST 1 (i)

3 19 LOAD_FAST 1 (i)
22 PRINT_ITEM
23 PRINT_NEWLINE
24 JUMP_ABSOLUTE 13 27 POP_BLOCK
28 LOAD_CONST 0 (None)

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/ma....2.orig.tar.gz)
can even do that.

Feb 22 '06 #2
mrstephengross wrote:
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.
Feb 22 '06 #3
mrstephengross wrote:
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

Feb 23 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: [ EuGeNe ] | last post by:
Hi all, I would like to write a script that downloads one file from a ftp server if the file creation date satisfy a condition. I can't figure out how to find from a ftp server what is the...
133
by: Philipp Lenssen | last post by:
Why is there no standardized and well-working way for a web-page to offer the font for download/embed it, in order to be displayed on the page? No matter what you think of the preferred font of a...
3
by: SK | last post by:
I have a file. i get the creation time using File.GetCreationTime. then i go and delete that file. and then create it again and print the File.GetCreationTime. It is giving me the old creation...
6
by: | last post by:
I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of a new pointer) It has no sense (it is dangerous) to allocate an...
2
by: David Fickbohm | last post by:
People, I am trying to determine the creation date of files in a folder. I am using the following code to find the folder and confirm that files exist in the folder. If someone could give me an...
3
by: Steven Blair | last post by:
Hi, I have a trace log file for a system I am writing. If the creation date is older than 14 days, I have to rename that file (File.Move). The next time a trace message is required a new file is...
8
by: Anthony Munter | last post by:
I have a web application with impersonate=”true” in Web.config and on my own logon page I allow the user to either - specify a userid/password for the app to impersonate when calling legacy...
3
by: Nick Dreyer | last post by:
I was quite surprised to notice that Sub New() gets called twice, once at declaration time and once at creation time. I can't figure out why it would be called at declaration if there is no class...
5
by: Saber | last post by:
I've not installed SQL Express and want to use SQL Server 2000 instead, I want to use SQL Server address instead of App_Data folder. web.config contains:...
51
by: Hans | last post by:
Hi all, Is there a way that the program that created and started a thread also stops it. (My usage is a time-out). E.g. thread = threading.Thread(target=Loop.testLoop) thread.start() ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.