James Lin James Lin - 9 months ago 83
Python Question

Can a specific Python version bytecode run on different interpreters?

I have been reading about Python interpreters, especially CPython and PyPy, I know that there are 2 steps to run a Python code:

  1. Convert to Bytecode

  2. Interpre to MachineCode

My question is, if bytecode is generated by CPython, lets say Python version 2.7.0, will the bytecode run on PyPy for Python 2.7.0?

Answer Source

No. The byte code changes between versions, and is tagged with a magic number to make it clear which interpreters it will work with. And that's just within CPython, between CPython and PyPy they don't even have to agree on where you'd look for the magic number, let alone what it means. .pyc files are largely an optimization, not portable in the way the source files are; they're only distributable if they're distributed with the interpreter that understands them.

Basically, the Python language standards covers source syntax and libraries, not byte code formats