If Python is interpreted, what are .pyc files?
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.
what is pycache folder in flask python?
.pyc
files are compiled Python files written by the Python interpreter. Read here.
why do people say python is slow because it is interpreted? It has .pyc files
I believe this is enough to correct your misunderstanding.
A program doesn’t run any faster when it is read from a .pyc or .pyo file than when it is read from a .py file; the only thing that’s faster about .pyc or .pyo files is the speed with which they are loaded.
source : https://docs.python.org/2/tutorial/modules.html#packages
How does the python interpreter know when to compile and update a .pyc file?
It looks like the timestamp is stored directly in the *.pyc
file. The python interpreter doesn't rely on the last modification attribute of the file, maybe to avoid incompatibe bytecode issues when copying source trees.
Looking at the python implementation of the import
statement, you can find the stale check in _validate_bytecode_header()
. By the looks of it, it extracts bytes 4 to 7 (incl) and compares it against the timecode of the source file. If those doesn't match, the bytecode is considered stalled and thus recompiled.
In the process, it also checks the length of the source file against the length of the source used to generate a given bytecode (stored in bytes 8 to 11).
In the python implementation, if one of those checks fails, the bytecode loader raises an ImportError
catched by SourceLoader.get_code()
that triggers a recompilation of the bytecode.
Note: That's how it's done in the python version of importlib
. I guess there's no functionnal difference in the native version, but my C is a bit too rusty to dig into compiler code
Use .py or .pyc file when sharing/backing up?
If you simply want to run your Python script, all you really need is .pyc
which is the bytecode generated from your source code. See here for details on running a .pyc
file. I will warn that some of the detials are bit twisty.
However I recommend including your source code and leaving out your .pyc
files as they are generated automatically by the Python Interpreter. Besides, if you, or another person would want to revise/revisit your source code at a later point, you would need the .py
files. Furthermore, it is usually best practice to just include your source code.
Related Topics
How to Upload File with Python Requests
Unicodeencodeerror: 'Charmap' Codec Can't Encode - Character Maps to <Undefined>, Print Function
How to Cross Compile Python Interpreter for Windows Under Linux
Crontab Failed to Run Python Script at Reboot
How to Print the Full Numpy Array, Without Truncation
What Is the Python Equivalent for a Case/Switch Statement
Visibility of Global Variables in Imported Modules
Pandas Get Topmost N Records Within Each Group
Serving a Request from Gunicorn
Error Installing Uwsgi in Virtualenv
How to Emulate a Do-While Loop
Calling Parent Class _Init_ with Multiple Inheritance, What's the Right Way
Unix Socket Credential Passing in Python
To Read Line from File Without Getting "\N" Appended at the End