Python Import Error: No module named Flask.myMethod
<p>I've read documentation, but I still can't quite wrap my head around how Python handles imports. I get the following error:</p>

<p>I've checked for circular imports (serv imports class_definitions: <code>from class_definitions import *</code> as well as a few other typical python modules) <code>class_definitions.py</code> only imports pickle
I've tried rebuilding the venv
I consolidated code to these two files to try to make it go away.</p>

<p>The most headway I got in debugging it was that if I get rid of a call to a function I defined in <code>class_definitions.py</code> called <code>load_pumps()</code> it would typically go away. 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. <code>load_pumps</code> is called in other parts of the code throughout <code>serv.py</code> and I didn't get this error until earlier today. Any ideas?</p>

<pre><code>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
</code></pre>

<p>And file tree looks like this</p>

<pre><code>Ã¢Ã¢Ã¢ 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
</code></pre>

<p>It seems that you have a py file at this path:</p>

<pre><code>"/home/pi/Desktop/barbot/flask-files/class_definitions.py"
</code></pre>

<p>And you are trying to import something named <code>Flask.class_definitions</code> when you <code>pickle.load</code>.</p>

<p>This fails because <code>class_definitions</code> is not in the <code>Flask</code> package.</p>

<p>Since you are loading a pickle, I would say you had a <code>class_definitions</code> module within a <code>Flask</code> package at some time in the past, when you created the pickle.</p>

<p>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. If you change your code in the meantime, the pickle can become incompatible.</p>

<p>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.</p>

python flask