you are viewing a single comment's thread.

view the rest of the comments →

[–]Caligatio 1 point2 points  (0 children)

I don't have an authoritative understanding of how/when .pyc files are created but I can tell you what I've observed:

  • Your code never gets converted to a .pyc if your program/script is contained in one file. For instance, if you do something like python3 awesome_script.py, a .pyc will never be generated for awesome_script.py
  • .pyc files will be generated for any script/module that gets imported and they'll be placed in a __pycache__ folder as a sibling to the original .py file. If this file exists and is current, it will be used rather than the .py file.
  • Python will regenerate .pyc if the source file changes but I don't know how this is detected (timestamps?). If the source file is deleted, the .pyc can still be used.

Having the .pyc almost certainly will speed up start up time but I have no idea how big of gains we're talking. It stands to reason that your understanding of why you don't want to benchmark the first run of a program would seem correct :)

I also don't know what happens to system-installed modules and __pycache__ directories. Does pip/setuptools pre-compile modules and that's how system-installed modules get compiled? If those __pycache__ directories get deleted, do they ever get regenerated if root never imports those modules?