python - 字节编译 Python 平台特定吗?

标签 python bytecode

我想对我在嵌入式设备上使用的 python 库进行字节编译。

基本上,我想将所有 *.py 文件预处理为 __pycache__/*.pyc 文件,以避免在第一次使用此应用程序时发生这种情况并减慢一切.

我试图了解这个预字节码翻译步骤是否依赖于它运行的地方(我的笔记本电脑与另一台设备)。 如果我用 compileall 字节编译我的 python 应用程序在 Ubuntu 盒子(基于 x86)上,然后将那些放置在 __pycache__ 目录中的字节码转换文件带到嵌入式 linux 盒子(基于 ARM)上,它们会工作吗?字节编译平台特定吗?

我不太关心 .pyc 文件是否与不同版本的 python 以及不同的底层架构兼容。我的机器和设备都使用python3.4。

最佳答案

If I byte-compile my python app [...] on an Ubuntu box (x86-based) and then take those bytecode-translated files [...] to an embedded linux box (ARM-based), will they work?

假设解释器版本使用兼容的字节码形式(仅在主要或次要版本号不同时更改),是的,它们会工作。

引自an excellent answer to a related question by John La Rooy :

# python:  file(1) magic for python
0   string      """ a python script text executable
0   belong      0x994e0d0a  python 1.5/1.6 byte-compiled
0   belong      0x87c60d0a  python 2.0 byte-compiled
0   belong      0x2aeb0d0a  python 2.1 byte-compiled
0   belong      0x2ded0d0a  python 2.2 byte-compiled
0   belong      0x3bf20d0a  python 2.3 byte-compiled
0   belong      0x6df20d0a  python 2.4 byte-compiled
0   belong      0xb3f20d0a  python 2.5 byte-compiled
0   belong      0xd1f20d0a  python 2.6 byte-compiled

...如果您的平台使用足够兼容的版本来共享相同的魔法,.pyc 文件将在它们之间工作。

关于python - 字节编译 Python 平台特定吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33425899/

相关文章:

c - 汇编、机器码、字节码和操作码之间的实际关系是什么?

Python从字典中删除单引号

Python:使用 Pandas 从数据框中选择特定日期

python - 如何在 QTablewidget 列的列上应用验证器?

python - django:将表单字段添加到从另一个表生成的表单中

python - 为什么 Python CONTINUE LOOP 允许外循环,而 BREAK LOOP 不允许?

java - noverify 和 Xverify :none? 的区别

python - 通过列表列表中的索引计算最小值或最大值

Java Agent 对低版本目标 JVM 的支持

java - 为什么调试String拼接的时候会弹出StringBuilder?