博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
什么是错误的幻数错误?
阅读量:2289 次
发布时间:2019-05-09

本文共 3412 字,大约阅读时间需要 11 分钟。

本文翻译自:

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 
from Normalization import Normalizer

#1楼

参考:


#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文件)

转载地址:http://czjnb.baihongyu.com/

你可能感兴趣的文章
有了阿里P8大牛手写的720页Java高级面试手册,offer稳了
查看>>
面试官都爱问的Spring源码:Spring与Mybatis高级整合
查看>>
好文推荐:微服务架构下的分布式事务基础入门
查看>>
Spring 异步实现原理与实战分享
查看>>
JVM源码分析之jstat工具原理完全解读
查看>>
面经分享:如何一招破解 Java 集合类面试题?
查看>>
Java注解是如何玩转的,面试官和我聊了半个小时
查看>>
一位阿里P7的面试题经验分享,希望能帮助到你
查看>>
一文Get所有 Redis 性能问题分析手段
查看>>
什么是架构?架构的本质和作用!
查看>>
Java总结:Java多线程
查看>>
从Java码农到年薪八十万的架构师
查看>>
python psutil结合钉钉报警
查看>>
一键升级python
查看>>
python 99乘法表
查看>>
一个可以拿来直接用的资产管理项目
查看>>
Centos 7 firewalld 基本操作
查看>>
passwd:只能指定一个用户的名称。
查看>>
nginx日志切割和日志清理
查看>>
RPC与分布式服务框架Dubbo
查看>>