Are there any benefits, performance or otherwise for avoiding .pyc
files, except for the convenience of not having a bunch of these files in the source folder?

- 1
- 1

- 101,334
- 104
- 266
- 359
-
No. If it was better not to have `.pyc`s then Python wouldn't include them =) – Katriel May 30 '12 at 08:51
3 Answers
I don't think there really is. .pyc
files are cached bytecode files, and you save startup time as Python does not have to recompile your python files every time you start the interpreter.
At most, switching off bytecode compilation lets you measure how much time the interpreter spends on this step. If you want to compare how much time is saved, remove all the .pyc
files in your project, and time Python by using the -B switch to the interpreter:
$ time python -B yourproject
then run again without the -B switch:
$ time python yourproject
It could be that the user under which you want to run your program does not have write access to the source code directories; for example a web server where you do not want remote users to have any chance of altering your source code through a vulnerability. In such cases I'd use the included compileall
module to bytecompile everything using a priviledged user rather than forgo writing .pyc
files.

- 1,048,767
- 296
- 4,058
- 3,343
-
@dkson - actually he didn't, even though I think it's a partial answer – Jonathan Livni May 30 '12 at 08:31
-
-
@dkson: No worries, I appreciate feedback and I improve my answers accordingly. :-) – Martijn Pieters May 30 '12 at 08:57
One reason I can think of: during development, if you remove or rename a .py
file, the .pyc
file will stay around in your local copy with the old name and the old bytecode. Since you don't normally commit .pyc
files to version control, this can lead to stray ImportError
s that don't happen on your machine, but do on others.

- 7,571
- 6
- 28
- 43
It is possible that .pyc files could encourage people to think they need not maintain/ship the original source. pyc files might not be portable between operating systems and versions of Python. When moving Python modules it is safer to leave the pyc files behind and just copy or ship the source and let the host python generate new pyc files.
By the way, from Python 3.2 the .pyc files no longer go into the source folder, but in __pycache__
(in the source folder).
-
2`.pyc` files are indeed not portable between Python versions and you should never ship code with these unless you package for a specific python version. – Martijn Pieters May 30 '12 at 08:35