ImportError:错误的幻数,自 OSX Lion 以来

发布于 2024-11-30 19:49:33 字数 932 浏览 2 评论 0原文

每次使用 PyDev 在 Eclipse 中运行任何 python 文件时,我都会收到此错误:

Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/site.py", line 73, in <module>
__boot()
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/site.py", line 2, in __boot
    import sys, imp, os, os.path   
ImportError: Bad magic number in /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.pyc

我正在使用 python 2.6。当我从终端(2.7 或 2.6)运行 python 时,不会出现此问题。自从上次一切正常以来,我唯一改变的就是从 Snow Leopard 到 OSX Lion 的更新。

与此类似的讨论似乎建议删除 .pyc 文件,因为最初使用 .pyc 文件的内容之间存在某种不匹配(我不完全确定幻数是什么......)。但我对从 Frameworks 目录中删除 os.pyc 的想法有点谨慎。当唯一的其他文件是 os.pyo 文件(不确定有什么区别),而不是 os.py 时。

我已经安装了所有 OSX Lion 更新、Eclipse 更新和 PyDev 更新。

即使使用以下代码也会出现此问题:

if __name__ == '__main__':
    pass

任何解决此问题的帮助将不胜感激!

I'm getting this error every time I run any python file in Eclipse using PyDev:

Traceback (most recent call last):
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/site.py", line 73, in <module>
__boot()
  File "/System/Library/Frameworks/Python.framework/Versions/2.6/Extras/lib/python/site.py", line 2, in __boot
    import sys, imp, os, os.path   
ImportError: Bad magic number in /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.pyc

I'm using python 2.6. This problem does not occur when I run python from the terminal (2.7 or 2.6). The only substantial thing I've changed since everything last worked, is an update to OSX Lion from Snow Leopard.

Similar discussions to this seem to suggest some kind of removal of the .pyc file, because of some kind of mismatch between what was originally using the .pyc files (I'm not entirely sure what a magic number is...). But I was a bit cautious of the idea of deleting os.pyc from the Frameworks directory. When the only other file is an os.pyo file (not sure what the difference it), rather than an os.py.

I've installed all OSX Lion updates, Eclipse updates and PyDev updates.

This problem occurs even with code such as :

if __name__ == '__main__':
    pass

Any help resolving this would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

弥繁 2024-12-07 19:49:33

将 Python 升级到 2.7.1,运行位于 Python 目录中的“Update Shell Profile”命令文件,并根据新安装更改 Netbeans 中的 Python 设置,这对我有用。

Upgrading Python to 2.7.1, running "Update Shell Profile" command file which is located in Python directory and changing the Python settings in Netbeans according to new installation worked for me.

忆伤 2024-12-07 19:49:33

是的,您需要从 *.py 文件重新生成所有 *.pyc *.pyo 文件。

如何执行此操作首先取决于它们最初是如何生成的。某些 Python 打包(及其附加组件),例如在某些 Linux 发行版中,为了自身利益而变得有点过于聪明,并将原始 *.py 文件保留在其他地方,并拥有自己的构建系统来生成和放置 * .pyc 和/或 *.pyo 文件。在这种情况下,您必须使用那个构建系统从原始 *.py 文件重新生成它们。

仅供参考,这里有一个 夫妇 *.pyo 文件上的链接。它们是已编译的 python 模块的优化版本。

Yeah, you'll need to regenerate all your *.pyc and *.pyo files from the *.py files.

How you do this depends on how they were generated in the first place. Some packaging of python (and it's add-ons), such as in some Linux distros, gets a little too clever for its own good and keeps the original *.py files somewhere else and have their own build system for generating and placing the *.pyc and/or *.pyo files. In a case like that, you have to use that build system to regenerate them from the original *.py files.

FYI, here are a couple links on *.pyo files. They are the optimized versions of compiled python modules.

夜空下最亮的亮点 2024-12-07 19:49:33

在 OS X Lion 上,您应该有一个 os.py 文件。这可能是您错误的根本原因。 os.pyc 文件是由与您现在运行的不同版本的 python 生成的。通常,我想 python 解释器只会从 os.py 重新生成文件。但无论出于何种原因,您的系统没有该文件。

我怀疑这是一大堆问题中的一个小数据点,通常会建议重新安装操作系统。

为了进行比较,我运行的是 10.7.1,并且有以下内容:

[2:23pm][wlynch@orange workout] ls /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.*
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.py
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.pyc
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.pyo

顺便说一句,*.pyo 文件是 python 字节码的优化版本。

On OS X Lion, you should have a os.py file. This is likely the root cause of your error. The os.pyc file was generated from a different version of python than you are running now. Normally, I imagine the python interpreter would just regenerate the file from os.py. But for whatever reason, your system does not have that file.

I suspect that this is a small data point in a larger set of issues and would, in general, recommend a reinstallation of your operating system.

For comparison, I'm running 10.7.1, and I have the following:

[2:23pm][wlynch@orange workout] ls /System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.*
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.py
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.pyc
/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/os.pyo

As an aside, the *.pyo file is an optimized version of the python bytecode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文