Python导入错误:没有名为Flask.myMethod的模块

时间:2016-09-11 07:59:13

标签: python flask

我已阅读文档,但我仍然无法完全理解Python如何处理导入。我收到以下错误:

我检查了循环导入(serv进口class_definitions:from class_definitions import *以及其他一些典型的python模块)class_definitions.py只导入pickle 我试过重建venv 我将代码合并到这两个文件中,试图让它消失。

我在调试时取得的最大进展是,如果我摆脱对class_definitions.py中定义的函数load_pumps()的调用,它通常就会消失。我可以将函数调用替换为一组表示数据load_pumps()应该返回的虚拟数据,我不会得到这个错误。 load_pumps在代码的其他部分调用serv.py,直到今天早些时候我才收到此错误。有什么想法吗?

Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 2000, in __call__
  return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1991, in wsgi_app
response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1567, in handle_exception
reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request
rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/pi/Desktop/barbot/flask-files/serv.py", line 89, in admin
pump_list=load_pumps(),
  File "/home/pi/Desktop/barbot/flask-files/class_definitions.py", line 90, in load_pumps
read_pump_list = pickle.load(file_pump)
  File "/usr/lib/python2.7/pickle.py", line 1378, in load
return Unpickler(file).load()
  File "/usr/lib/python2.7/pickle.py", line 858, in load
dispatch[key](self)
  File "/usr/lib/python2.7/pickle.py", line 1090, in load_global
klass = self.find_class(module, name)
  File "/usr/lib/python2.7/pickle.py", line 1124, in find_class
__import__(module)
ImportError: No module named Flask.class_definitions

文件树看起来像这样

âââ admin_config.cfg
âââ class_definitions.py
âââ class_definitions.pyc
âââ __init__.py
âââ __init__.pyc
âââ lists
â   âââ bac_list.py
â   âââ bac_list.pyc
â   âââ cocktails_all.py
â   âââ cocktails_all.pyc
â   âââ garnishes_all.lst
â   âââ garnishes_selected.lst
â   âââ __init__.py
â   âââ __init__.pyc
â   âââ mixers_all.lst
â   âââ pumps.cfg
â   âââ spirits_all.lst
âââ requirements.txt
âââ serv.py
âââ serv.pyc
âââ templates
âââ admin_console.html

2 directories, 20 files

1 个答案:

答案 0 :(得分:0)

您似乎在此路径中有一个py文件:

"/home/pi/Desktop/barbot/flask-files/class_definitions.py"

您正试图在Flask.class_definitions时导入名为pickle.load的内容。

此操作失败,因为class_definitions包中不包含Flask

由于您正在加载一个泡菜,我会说您在过去的某个时间在class_definitions包中有一个Flask模块,当您创建泡菜时。

pickle工作的方式是它保存了保存实例时使用的类的确切名称,以便它可以在加载时找到它们并重新构建实例。如果在此期间更改代码,则pickle可能会变得不兼容。

有很多方法可以实现这种情况的兼容性,但是我建议至少在现在保存一个pickle之后不要更改你的类名和目录结构。