7

There are already a couple of question on Python's .pyc files but I have not read about 1) when (in what circumstances) are these files created and 2) whether there is a good reason to keep them.

More specifically:

About (1): In 200 or more PY files (new and old ones) that I have run, I have seen only a couple of .pyc files created. However, in Python's own (installation) directory there are currently 1160 .pyc files, about half of the .py files! I don't know how many of them existed already on Python's installation, but about 450 of them have been certainly created after that. So, it's a mystery to me in what circumstances these files are created. Or are they always automatically created and then they are supposed to be deleted after the PY program terminates?

About (2): I have compared the execution timing .py against .pyc files and I cannot see any (significant) difference. I don't have large .py files or .py files with heave computations, etc. to test. But as far as "regular" files (small to medium size and with regular computations) are concerned, the .pyc files seem to be certainly useless. So, why should one keep them?


... Later: I just realized that a .pyc file is created only when its corresp. .py file is used as a module, i.e. it is imported. OK, there's so meaning in this! :)

Apostolos
  • 3,115
  • 25
  • 28
  • 1
    Possible duplicate of [When are .pyc files refreshed?](https://stackoverflow.com/questions/15839555/when-are-pyc-files-refreshed) – ndmeiri Mar 15 '18 at 06:52

1 Answers1

3
  1. when (in what circumstances) are these files created and
  2. whether there is a good reason to keep them.
  1. When the python script is imported from somewhere else
  2. Sure, as they'll be automatically created again the next time, otherwise. You'll save processing time if you keep them and the disk usage should be negligible.

In order to understand about the reason for these .pyc files, you should take a look at this question and the nice graphic in the last answer, too: If Python is interpreted, what are .pyc files?

0x01
  • 468
  • 2
  • 9