【问题标题】:Is byte-compiling Python platform-specific?字节编译 Python 平台是特定的吗?
【发布时间】:2015-10-29 22:41:02
【问题描述】:

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

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

我正在尝试了解此预字节码翻译步骤是否对其运行位置(我的笔记本电脑与另一台设备)有任何依赖关系。 如果我在 Ubuntu 机器(基于 x86)上使用 compileall 对我的 python 应用程序进行字节编译,然后将放置在 __pycache__ 目录中的那些字节码翻译文件带到嵌入式 linux 机器(基于 ARM),将他们工作?字节编译平台是特定的吗?

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

【问题讨论】:

标签: python bytecode


【解决方案1】:

如果我在 Ubuntu 机器(基于 x86)上对我的 python 应用程序 [...] 进行字节编译,然后将这些字节码翻译文件 [...] 带到嵌入式 linux 机器(基于 ARM),将他们工作吗?

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

引用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 文件将在它们之间工作。

【讨论】:

猜你喜欢
  • 2010-11-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-04
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多