Coder Perfect

If Python is interpreted, what are .pyc files?


Python is an interpreted language, according to what I’ve been told… However, I see.pyc files in my Python source code, which Windows recognizes as “Compiled Python Files.”

What role do these play?

Asked by froadie

Solution #1

This widely circulated myth is erroneous, or rather, is based on a misunderstanding of (natural) language levels: a comparable error would be to state “the Bible is a hardback book.” Allow me to explain that metaphor…

“The Bible” is “a book” in the sense that it is a category of (actual, physical objects identified as) books; the books identified as “copies of the Bible” are supposed to have something fundamental in common (the contents, though even those can be in different languages, with different acceptable translations, levels of footnotes and other annotations) — but those books are perfectly fine to differ in a variety of aspects that are not considered fundamental —

After all, the Bible is a book that’s meant to be read over and over, bookmarked in various places, thumbed through seeking for certain chapter-and-verse hints, and so on, and a nice hardback covering can help a given copy endure longer under such use. These are, however, trivial (practical) considerations that cannot be utilized to evaluate whether or not a certain physical book object is a copy of the Bible: paperback printings are entirely possible!

Similarly, Python is “a language” in the sense of defining a class of language implementations that must all be similar in some fundamental respects (syntax, most semantics except those parts of those where they’re explicitly allowed to differ), but are fully allowed to differ in just about every “implementation” detail — including how they deal with the source files they’re given, whether they compile the sources to some lower level forms (and, if so, which form — and how they compile them —

CPython is the classic implementation, although it’s just one of several production-quality implementations, including Microsoft’s IronPython (which compiles to CLR codes, i.e. “.NET”), Jython (which compiles to JVM codes), and PyPython (which compiles to JVM codes) (which is written in Python itself and can compile to a huge variety of “back-end” forms including “just-in-time” generated machine language). They’re all Python (==”implementations of the Python programming language”), just as multiple seemingly unrelated book objects can all be Bibles (==”copies of The Bible”).

If you’re specifically interested in CPython, it compiles source files into a Python-specific lower-level form (known as “bytecode”), does so automatically when needed (when there is no bytecode file corresponding to a source file, or the bytecode file is older than the source or compiled by a different Python version), and saves the bytecode files to disk (to avoid recompiling them in the future). IronPython, on the other hand, will normally compile to CLR programs (which may or may not be saved to disk), and Jython to JVM codes (saving them to disk or not — it will use the .class extension if it does save them).

These lower-level forms are then executed by appropriate “virtual machines,” often known as “interpreters,” such as the CPython virtual machine, the.Net runtime, or the Java virtual machine (aka JVM), as needed.

In this sense (what conventional implementations do), Python is a “interpreted language” if and only if C# and Java are: they all have a common implementation technique of producing bytecode first, then executing it via a virtual machine or interpreter.

The focus is more than likely on how “heavy,” “slow,” and “ceremonious” the compilation process is. CPython is designed to compile as quickly as possible, with as little fuss as possible — the compiler performs very little error checking and optimization, allowing it to run quickly and efficiently in small amounts of memory, allowing it to be run automatically and transparently whenever needed, with the user rarely being aware that a compilation is taking place. In order to examine problems more thoroughly and do more optimizations, Java and C# often accept additional work during compilation (and thus do not perform automatic compilation). It’s a spectrum of gray scales rather than a black-and-white condition, and

Answered by Alex Martelli

Solution #2

They include byte code, which the Python interpreter uses to construct the source code. Python’s virtual machine then executes this code.

The term is explained in Python’s documentation as follows:

Answered by unwind

Solution #3

There is no such thing as a language that has been interpreted. The use of an interpreter or a compiler is entirely dependent on the implementation and has nothing to do with the language itself.

An interpreter or a compiler can be used to implement any language. Each type has at least one implementation in the vast majority of languages. (For example, there are compilers for JavaScript, PHP, Perl, Python, and Ruby, as well as interpreters for C and C++.) Furthermore, most current language implementations include both an interpreter and a compiler (or even multiple compilers).

A language is nothing more than a collection of abstract mathematical rules. An interpreter is one of many tangible language implementation options. Those two exist on very distinct planes of abstraction. The word “interpreted language” would be a type mistake if English were a typed language. The phrase “Python is an interpreted language” is not just incorrect (since being incorrect implies that the statement makes sense even if it is incorrect), but it also makes no sense, because a language can never be characterized as “interpreted.”

If you look at the current Python implementations, you’ll notice that they use the following implementation strategies:

You’ll notice that every single implementation in that list (along with a few others I didn’t name, such as tinypy, Shedskin, and Psyco) has a compiler. In fact, as far as I’m aware, there is currently no entirely interpreted Python implementation, no such implementation is planned, and no such implementation has ever existed.

Not only does the term “interpreted language” not make sense, even if you interpret it as meaning “language with interpreted implementation”, it is clearly not true. Obviously, whoever told you that has no idea what he’s talking about.

The.pyc files you’re seeing are cached bytecode files generated by CPython, Stackless Python, or Unladen Swallow, respectively.

Answered by Jörg W Mittag

Solution #4

When file is imported, the Python interpreter creates these files, which contain the “compiled bytecode” of the imported module/program. The idea is that the “translation” from source code to bytecode (which only needs to be done once) can be skipped on subsequent imports if the.pyc file is newer than the file, thus speeding up startup. However, it is still interpreted.

Answered by Tim Pietzcker

Solution #5

Python caches the built content of modules in.pyc files to speed up module loading.

CPython converts its source code into “byte code,” which it stores on the file system for performance reasons whenever the source file changes. Because the compilation phase may be skipped, loading Python modules becomes substantially faster. When you use as your source file, CPython saves the byte code in a foo.pyc file next to it.

Python’s import machinery has been expanded in Python 3 to allow users to write and search for byte code cache files in a single directory within each Python package directory. __pycache__ will be the name of this directory.

The following is a flow chart that shows how modules are loaded:

For more information:

ref:PEP3147 ref:“Compiled” Python files

Answered by hxysayhi

Post is based on