If Python is interpreted, what are .pyc files?

前端 未结 11 750
夕颜
夕颜 2020-11-22 08:39

I\'ve been given to understand that Python is an interpreted language...
However, when I look at my Python source code I see .pyc files, w

相关标签:
11条回答
  • 2020-11-22 09:30

    Machines don't understand English or any other languages, they understand only byte code, which they have to be compiled (e.g., C/C++, Java) or interpreted (e.g., Ruby, Python), the .pyc is a cached version of the byte code. https://www.geeksforgeeks.org/difference-between-compiled-and-interpreted-language/ Here is a quick read on what is the difference between compiled language vs interpreted language, TLDR is interpreted language does not require you to compile all the code before run time and thus most of the time they are not strict on typing etc.

    0 讨论(0)
  • 2020-11-22 09:32

    THIS IS FOR BEGINNERS,

    Python automatically compiles your script to compiled code, so called byte code, before running it.

    Running a script is not considered an import and no .pyc will be created.

    For example, if you have a script file abc.py that imports another module xyz.py, when you run abc.py, xyz.pyc will be created since xyz is imported, but no abc.pyc file will be created since abc.py isn’t being imported.

    If you need to create a .pyc file for a module that is not imported, you can use the py_compile and compileall modules.

    The py_compile module can manually compile any module. One way is to use the py_compile.compile function in that module interactively:

    >>> import py_compile
    >>> py_compile.compile('abc.py')
    

    This will write the .pyc to the same location as abc.py (you can override that with the optional parameter cfile).

    You can also automatically compile all files in a directory or directories using the compileall module.

    python -m compileall
    

    If the directory name (the current directory in this example) is omitted, the module compiles everything found on sys.path

    0 讨论(0)
  • 2020-11-22 09:35

    There is no such thing as an interpreted language. Whether an interpreter or a compiler is used is purely a trait of the implementation and has absolutely nothing whatsoever to do with the language.

    Every language can be implemented by either an interpreter or a compiler. The vast majority of languages have at least one implementation of each type. (For example, there are interpreters for C and C++ and there are compilers for JavaScript, PHP, Perl, Python and Ruby.) Besides, the majority of modern language implementations actually combine both an interpreter and a compiler (or even multiple compilers).

    A language is just a set of abstract mathematical rules. An interpreter is one of several concrete implementation strategies for a language. Those two live on completely different abstraction levels. If English were a typed language, the term "interpreted language" would be a type error. The statement "Python is an interpreted language" is not just false (because being false would imply that the statement even makes sense, even if it is wrong), it just plain doesn't make sense, because a language can never be defined as "interpreted."

    In particular, if you look at the currently existing Python implementations, these are the implementation strategies they are using:

    • IronPython: compiles to DLR trees which the DLR then compiles to CIL bytecode. What happens to the CIL bytecode depends upon which CLI VES you are running on, but Microsoft .NET, GNU Portable.NET and Novell Mono will eventually compile it to native machine code.
    • Jython: interprets Python sourcecode until it identifies the hot code paths, which it then compiles to JVML bytecode. What happens to the JVML bytecode depends upon which JVM you are running on. Maxine will directly compile it to un-optimized native code until it identifies the hot code paths, which it then recompiles to optimized native code. HotSpot will first interpret the JVML bytecode and then eventually compile the hot code paths to optimized machine code.
    • PyPy: compiles to PyPy bytecode, which then gets interpreted by the PyPy VM until it identifies the hot code paths which it then compiles into native code, JVML bytecode or CIL bytecode depending on which platform you are running on.
    • CPython: compiles to CPython bytecode which it then interprets.
    • Stackless Python: compiles to CPython bytecode which it then interprets.
    • Unladen Swallow: compiles to CPython bytecode which it then interprets until it identifies the hot code paths which it then compiles to LLVM IR which the LLVM compiler then compiles to native machine code.
    • Cython: compiles Python code to portable C code, which is then compiled with a standard C compiler
    • Nuitka: compiles Python code to machine-dependent C++ code, which is then compiled with a standard C compiler

    You might notice that every single one of the implementations in that list (plus some others I didn't mention, like tinypy, Shedskin or Psyco) has a compiler. In fact, as far as I know, there is currently no Python implementation which is purely interpreted, there is no such implementation planned and there never has been such an implementation.

    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. Whoever told you that, obviously doesn't know what he is talking about.

    In particular, the .pyc files you are seeing are cached bytecode files produced by CPython, Stackless Python or Unladen Swallow.

    0 讨论(0)
  • 2020-11-22 09:36

    They contain byte code, which is what the Python interpreter compiles the source to. This code is then executed by Python's virtual machine.

    Python's documentation explains the definition like this:

    Python is an interpreted language, as opposed to a compiled one, though the distinction can be blurry because of the presence of the bytecode compiler. This means that source files can be run directly without explicitly creating an executable which is then run.

    0 讨论(0)
  • 2020-11-22 09:43

    To speed up loading modules, Python caches the compiled content of modules in .pyc.

    CPython compiles its source code into "byte code", and for performance reasons, it caches this byte code on the file system whenever the source file has changes. This makes loading of Python modules much faster because the compilation phase can be bypassed. When your source file is foo.py , CPython caches the byte code in a foo.pyc file right next to the source.

    In python3, Python's import machinery is extended to write and search for byte code cache files in a single directory inside every Python package directory. This directory will be called __pycache__ .

    Here is a flow chart describing how modules are loaded:

    For more information:

    ref:PEP3147
    ref:“Compiled” Python files

    0 讨论(0)
提交回复
热议问题