![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Expert Programmer
|
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?
|
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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.pycThe 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! |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
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.
|
|
|
|
|
|
#4 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
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! |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
:o, oops, yes you are correct.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|