0

Python only saves .pyc files for code files which are being imported and not for top level script files. Why is this? Also, aren't the main source files compiled to byte code at all?

computronium
  • 445
  • 2
  • 11

1 Answers1

1

This has been asked before: Why does Python only save the bytecode for a script if it is imported?

A discussion about the intricacies of the various implementations of python execution: stackoverflow.com/questions/2998215/…

General concept for beginners to look up:

Python is generally executed through an interpreter, and the typical compilation process known from languages like C++ happens in the background and often at runtime. Have a look at this link for a more detailed explanation on compilation vs. interpretation: https://medium.com/@DHGorman/a-crash-course-in-interpreted-vs-compiled-languages-5531978930b6

Cerenia
  • 147
  • 7
  • Although everything happens in python at runtime, i.e. the compiler (which compiles source files to byte code behind the scenes) is always present at runtime and is a part of the system that runs programs; there absolutely is an implicit compilation step to byte code post which the PVM executes said byte code.That's what takes place under the hood. – computronium Apr 16 '20 at 05:35
  • Thank for clarifying the question! The way you phrased it before made me think this was a beginners question which is why I provided this oversimplified answer. I found this discussion on the intricacies of the different python backend implementations: https://stackoverflow.com/questions/2998215/if-python-is-interpreted-what-are-pyc-files I hope this will be helpful. – Cerenia Apr 16 '20 at 11:39
  • 1
    Thanks for editing your answer to make it more relevant to my question. Glad the explanatory comment helped. Though the link you pointed to doesn't answer my question _per se_, but it did add some valuable information to the discussion. After doing more research I found that all source code is compiled to byte code but not written to files. Imports are expensive processes, so it makes more sense to write their compiled bytecode into files (w/i the __pycache__ directory) for future use (until their source is altered). **The rest remain in memory to be discarded after the program ends.** – computronium Apr 17 '20 at 04:05