简体   繁体   English

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

[英]Python Import Error: No module named Flask.myMethod

I've read documentation, but I still can't quite wrap my head around how Python handles imports.我已经阅读了文档,但我仍然无法完全理解 Python 如何处理导入。 I get the following error:我收到以下错误:

I've checked for circular imports (serv imports class_definitions: from class_definitions import * as well as a few other typical python modules) class_definitions.py only imports pickle I've tried rebuilding the venv I consolidated code to these two files to try to make it go away.我检查了循环导入(serv 导入 class_definitions: from class_definitions import *以及其他一些典型的 python 模块) class_definitions.py只导入 pickle 我试过重建 venv 我将代码合并到这两个文件中以尝试制作它消失了。

The most headway I got in debugging it was that if I get rid of a call to a function I defined in class_definitions.py called load_pumps() it would typically go away.我在调试中获得的最大进展是,如果我摆脱了对在class_definitions.py定义的名为load_pumps()的函数的调用,它通常会消失。 I could replace the function call to a set of dummy data that represents the data load_pumps() should return and I would not get this error.我可以将函数调用替换为一组代表数据 load_pumps() 应该返回的虚拟数据,并且我不会收到此错误。 load_pumps is called in other parts of the code throughout serv.py and I didn't get this error until earlier today. load_pumps被称为在整个代码的其他部分serv.py ,我没有得到这个错误在今天早些时候,直到。 Any ideas?有任何想法吗?

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

And file tree looks like this文件树看起来像这样

âââ 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

It seems that you have a py file at this path:看来您在此路径上有一个 py 文件:

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

And you are trying to import something named Flask.class_definitions when you pickle.load .当你pickle.load时,你试图导入名为Flask.class_definitions东西。

This fails because class_definitions is not in the Flask package.这将失败,因为class_definitions不在Flask包中。

Since you are loading a pickle, I would say you had a class_definitions module within a Flask package at some time in the past, when you created the pickle.由于您正在加载泡菜,我会说您在过去某个时间创建泡菜时在Flask包中有一个class_definitions模块。

The way pickle works is that it saved the exact names of the classes which were used when it saves the instances, so that it can find them and reconstruct the instances when loading. pickle 的工作方式是它保存了保存实例时使用的类的确切名称,以便它可以在加载时找到它们并重建实例。 If you change your code in the meantime, the pickle can become incompatible.如果在此期间更改代码,pickle 可能会变得不兼容。

There are ways to implement compatibility for such situations, but instead of that I suggest that, for now at least, you dont change your class names and directory structures after saving a pickle.有一些方法可以为这种情况实现兼容性,但我建议至少现在,在保存泡菜后不要更改类名和目录结构。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM