Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 27th, 2006, 12:48 PM   #1
titaniumdecoy
Expert Programmer
 
titaniumdecoy's Avatar
 
Join Date: Nov 2005
Posts: 839
Rep Power: 3 titaniumdecoy is on a distinguished road
Send a message via AIM to titaniumdecoy
Compiling Python code?

Is it possible to compile Python scripts so that the source code cannot be retrieved (without difficulty)? Also, is it true that accessing a Python script with an _ after the name, e.g. trip.py_, will show the source code? If so, is it possible to prevent this?
titaniumdecoy is offline   Reply With Quote
Old May 27th, 2006, 1:38 PM   #2
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
This is the way to compile a Python file to a byte code file. The byte code file is not easy to read, check it out with an editor.
# create a byte code compiled python file

import py_compile
py_compile.compile("MyFile.py")  # creates  MyFile.pyc
Python will run either the .py file or the .pyc file

The thing with the .py_ doesn't make much sense. If you take a source file like MyFile.py and rename it MyFile.py_ then you still have a source file. A byte code file like MyFile.pyc can be renamed to MyFile.py_, but it is still a byte code file!
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old May 27th, 2006, 6:32 PM   #3
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
Whenever you run your script from the command line using "python myfile.py" it automatically generates a myfile.pyc. That file is still pretty "open", you can inspect it for all variables, classes, constants, basically symbol information but you can't read the source directly. py2exe, py2app, and freeze will turn you python program into an executable, that is probably the hardest form of python program to read.
Game_Ender is offline   Reply With Quote
Old May 28th, 2006, 9:12 AM   #4
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Smile

I hate to differ, but Python does not normally generate a .pyc file. It creates the byte code file in memory, only with imported modules Python will generate a byte code file to speed up their reuse.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old May 29th, 2006, 10:44 AM   #5
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
:o, oops, yes you are correct.
Game_Ender is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:29 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC