有没有办法在 ipython 笔记本中导入 .pyo 文件

Is there a way to import a .pyo file in an ipython notebook

在交互式 IPython notebook 会话期间,通常只需要正常导入,有时需要偶尔导入 .pyo 文件。

是否有任何 IPython 魔法或 python API 可以做到这一点?

Python 仅当 运行 带有 -O 选项时才导入 .pyo 文件:

python -O

使用此选项启动整个 IPython Notebook 似乎确实有效。至少我没有去上班。

正如这个 issue 指出的那样,解决方法是将 .pyo 文件重命名为 .pyc 文件。比 Python 将导入它:

Actually it's a lot easier than that, although it is very much a hack: just rename the .pyo files to .pyc, and python without -O will happily import them. Since the optimization happens when the bytecode is written, this does what you want.

R. David Murray (r.david.murray)