0

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?

Community
  • 1
  • 1
Jonathan Livni
  • 101,334
  • 104
  • 266
  • 359

3 Answers3

2

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.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
1

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 ImportErrors that don't happen on your machine, but do on others.

Daisy Leigh Brenecki
  • 7,571
  • 6
  • 28
  • 43
0

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).

Katriel
  • 120,462
  • 19
  • 136
  • 170
cdarke
  • 42,728
  • 8
  • 80
  • 84
  • 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