Python2.7“ pickle”无法导入模块,显示为“ ImportError:没有名为xxxxx的模块”

时间:2020-04-14 16:15:53

标签: python python-2.7 pickle

当前,我正在研究chromos一个由Python 2.7运行的项目“ touch_firmware_test”

(git clone https://chromium.googlesource.com/chromiumos/platform/touch_firmware_test

有一个脚本:“ / touch_firmare_test / touchbot / device_spec.py”

我有用于加载并打印所需内容的样本文件。并运行如下脚本:

from tkinter import *
import tkinter as tk
class MainWin:    
    def __init__(self, win, *args):
        self.win=win
        self.Reg=win 
    def First(self):
        self.Reg.title("1111111111 ")
        self.Reg.geometry("400x200+400+120")
        self.Bt=Button(self.Reg, text="Ok", command= self.Second)
        self.Bt.pack()      
    def Second(self):
        self.win.title(" 2222222222 ")
        self.win.geometry("700x400+400+150")
        self.win.resizable(0,0)
def main():
    master=Tk()
    App=MainWin(master)
    App.First()
    master.mainloop()    
if __name__ == '__main__':
    main()

但是,我无法通过运行此脚本来获得预期的输出结果。

修改“ device_spec.py”中的内容后,尝试获取遇到的问题,并弹出错误消息:

$python device_spec.py sample.p

接下来,我编写一个简单的代码来检查模块导入过程是否正确:

File "/usr/lib/python2.7/pickle.py", line 1130, in find_class
    __import__(module)
ImportError: No module named device_spec

运行该脚本,如下所示:

import os
import sys

def dump(module):
    print (module, "=>",)
    if module in sys.builtin_module_names:  #check Built-in module exist or not
        print ("<It's BUILTIN module>")
    else:
        module = __import__(module)         #print 3rd party module path
        print (module.__file__)

checkMU = dump(sys.argv[1])

如果我在“ / touch_firmare_test / touchbot /”下运行,此脚本将按预期正常运行(可以打印正确的模块路径)。

我还尝试了Python pickling after changing a module's directory

的答案

但是还是不行。...

我不知道如何处理...

每次回复都非常感谢。

0 个答案:

没有答案