什么是错误的幻数错误?

本文翻译自:What's the bad magic number error?

What's the "Bad magic number" ImportError in python, and how do I fix it? 什么是python中的“错误魔术数字” ImportError,如何解决?

The only thing I can find online suggests this is caused by compiling a .py -> .pyc file and then trying to use it with the wrong version of python. 我在网上可以找到的唯一东西表明,这是由于编译.py-> .pyc文件,然后尝试将其与错误版本的python一起使用而引起的。 In my case, however, the file seems to import fine some times but not others, and I'm not sure why. 但是,就我而言,该文件似乎有时可以很好地导入,而其他时候则不能,并且我不确定为什么。

The information python's providing in the traceback isn't particularly helpful (which is why I was asking here...), but here it is in case it helps: python在回溯中提供的信息不是特别有用(这就是为什么我在这里询问...的原因),但是在这里,以防它有所帮助:

Traceback (most recent call last):
  File "run.py", line 7, in <module>
    from Normalization import Normalizer

#1楼

参考:https://stackoom.com/question/29oJ/什么是错误的幻数错误


#2楼

Take the pyc file to a windows machine. 将pyc文件放入Windows机器。 Use any Hex editor to open this pyc file. 使用任何十六进制编辑器打开此pyc文件。 I used freeware 'HexEdit'. 我使用了免费软件“ HexEdit”。 Now read hex value of first two bytes. 现在读取前两个字节的十六进制值。 In my case, these were 03 f3. 就我而言,这些是03 f3。

Open calc and convert its display mode to Programmer (Scientific in XP) to see Hex and Decimal conversion. 打开calc并将其显示模式转换为Programmer(在XP中为Scientific),以查看十六进制和十进制转换。 Select "Hex" from Radio button. 从单选按钮中选择“十六进制”。 Enter values as second byte first and then the first byte ie f303 Now click on "Dec" (Decimal) radio button. 首先输入值作为第二个字节,然后输入第一个字节,即f303现在单击“ Dec”(十进制)单选按钮。 The value displayed is one which is correspond to the magic number aka version of python. 显示的值是一个,对应于python的幻数(又称魔术版本)。

So, considering the table provided in earlier reply 因此,考虑先前答复中提供的表

  • 1.5 => 20121 => 4E99 so files would have first byte as 99 and second as 4e 1.5 => 20121 => 4E99,因此文件的第一个字节为99,第二个字节为4e
  • 1.6 => 50428 => C4FC so files would have first byte as fc and second as c4 1.6 => 50428 => C4FC,因此文件的第一个字节为fc,第二个字节为c4

#3楼

I had a strange case of Bad Magic Number error using a very old (1.5.2) implementation. 使用非常旧的(1.5.2)实现,我遇到了一个奇怪的错误数字魔术错误的案例。 I generated a .pyo file and that triggered the error. 我生成了一个.pyo文件,并触发了错误。 Bizarrely, the problem was solved by changing the name of the module. 奇怪的是,通过更改模块名称解决了该问题。 The offending name was sms.py. 令人反感的名称是sms.py。 If I generated an sms.pyo from that module, Bad Magic Number error was the result. 如果我从该模块生成了sms.pyo,则结果是错误的幻数错误。 When I changed the name to smst.py, the error went away. 当我将名称更改为smst.py时,错误消失了。 I checked back and forth to see if sms.py somehow interfered with any other module with the same name but I could not find any name collision. 我来回检查了一下sms.py是否以某种方式干扰了其他任何同名模块,但是我找不到任何名称冲突。 Even though the source of this problem remained a mistery for me, I recommend trying a module name change. 即使这个问题的根源对我来说仍然是莫名其妙,但我还是建议您尝试更改模块名称。


#4楼

删除所有.pyc文件将解决“错误的魔术数字”错误。

find . -name "*.pyc" -delete

#5楼

This is much more efficent than above. 这比上面的要有效得多。

find {directory-of-.pyc-files} -name "*.pyc" -print0 | xargs -0 rm -rf

where {directory-of-.pyc-files} is the directory that contains the compiled python files. 其中{directory-of-.pyc-files}是包含已编译p​​ython文件的目录。


#6楼

In my case it was not .pyc but old binary .mo translation files after I renamed my own module, so inside this module folder I had to run 在我的情况下,重命名自己的模块后,它不是.pyc而是旧的二进制.mo转换文件,因此在此模块文件夹中,我必须运行

find . -name \*.po -execdir sh -c 'msgfmt "$0" -o `basename $0 .po`.mo' '{}' \;

(please do backup and try to fix .pyc files first) (请备份并尝试首先修复.pyc文件)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值