tags:

views:

3679

answers:

7

This may seem like a dumb question, but why would you compile a python script? You can run them directly from the .py file and it works fine, so is there a performance advantage or something? I also notice that some files in my application get compiled into .pyc while others do not, why is this?

+4  A: 

There's certainly a performance difference when running a compiled script. If you run normal .py scripts, the machine compiles it every time it is run and this takes time. On modern machines this is hardly noticeable but as the script grows it may become more of an issue.

sirlancelot
+2  A: 

There is a performance increase in running compiled python, however when you run a .py python will compile and store it, and as long as the .py does not change it will always use the compiled version.

With any interpeted language when the file is used the process looks something like this:
1. File is processed by the interpeter.

2. File is compiled
3. Compiled code is executed.

obviously by using pre-compiled code you can eliminate step 2, this applies python, PHP and others.

Heres an interesting blog post explaining the differences http://julipedia.blogspot.com/2004/07/compiled-vs-interpreted-languages.html
And here's an entry that explains the Python compile process http://effbot.org/zone/python-compile.htm

Unkwntech
+1  A: 

Yep, performance is the main reason and, as far as I know, the only reason.

If some of your files aren't getting compiled, maybe Python isn't able to write to the .pyc file, perhaps because of the directory permissions or something. Or perhaps the uncompiled files just aren't ever getting loaded... (scripts/modules only get compiled when they first get loaded)

David Zaslavsky
+19  A: 

It's compiled to bytecode which can be used much, much, much faster.

The reason some files aren't compiled is that the main script, which you invoke with python main.py is recompiled every time you run the script. All imported scripts will be compiled and stored on the disk.

Important addition by Ben Blank:

It's worth noting that while running a compiled script has a faster startup time (as it doesn't need to be compiled), it doesn't run any faster.

Georg
It's worth noting that while running a compiled script has a faster *startup* time (as it doesn't need to be compiled), it doesn't *run* any faster.
Ben Blank
A common misconception. Thanks for sharing.
sirlancelot
Good point Ben!
Sander Versluys
+2  A: 

As already mentioned, you can get a performance increase from having your python code compiled into bytecode. This is usually handled by python itself, for imported scripts only.

Another reason you might want to compile your python code, could be to protect your intellectual property from being copied and/or modified.

You can read more about this in the Python documentation.

Simon Jensen
In regards to protecting your code - compiling won't help a whole lot. Compiling obfuscates - but someone with the desire will get your code regardless.
Josh Smeaton
@josh that is always possible, if one can access the memory or watch the instructions to the cpu, with enough time and will they can re construct your app.
Unkwntech
Agreed, however as Unkwntech said, that will always be possible, if the person is determined enough. But I'm convinced it will suffice in most situations, where you typically just want to restrict people from "fixing" your code...
Simon Jensen
+11  A: 

The .pyc file is Python that has already been compiled to byte-code. Python automatically runs a .pyc file if it finds one with the same name as a .py file you invoke.

"An Introduction to Python" says this about compiled Python files:

A program doesn't run any faster when it is read from a ‘.pyc’ or ‘.pyo’ file than when it is read from a ‘.py’ file; the only thing that's faster about ‘.pyc’ or ‘.pyo’ files is the speed with which they are loaded.

The advantage of running a .pyc file is that Python doesn't have to incur the overhead of compiling it before running it. Since Python would compile to byte-code before running a .py file anyway, there shouldn't be any performance improvement aside from that.

How much improvement can you get from using compiled .pyc files? That depends on what the script does. For a very brief script that simply prints "Hello World," compiling could constitute a large percentage of the total startup-and-run time. But the cost of compiling a script relative to the total run time diminishes for longer-running scripts.

The script you name on the command-line is never saved to a .pyc file. Only modules loaded by that "main" script are saved in that way.

Bill Karwin
A: 

Another advantage is that if you have a password in your program that someone needs to type, they could simply look in the .py file and find the password, but you can't see the source of a .pyc file.

sonaxaton
It would be incredibly easy to see the password in the .pyc file, by using a hex editor or Python's disassembler.
interjay
NEVER store passwords in plaintext. Doesn't matter if it's in a DB, in a Python file, or wherever. Always store an hash.
nico