More on python ImportError No module named

Following the suggestion here , my package (or directory containing my modules) is located in the C: / Python34 / Lib / site-packages folders. The directory contains __init__.py

and sys.path

contains the directory path as shown.

enter image description here

However, I am getting the following error:

Traceback (most recent call last):
  File "C:/Python34/Lib/site-packages/toolkit/window.py", line 6, in <module>
    from catalogmaker import Catalog
  File "C:\Python34\Lib\site-packages\toolkit\catalogmaker.py", line 1, in <module>
    from patronmaker import Patron
  File "C:\Python34\Lib\site-packages\toolkit\patronmaker.py", line 4, in <module>
    class Patron:
  File "C:\Python34\Lib\site-packages\toolkit\patronmaker.py", line 11, in Patron
    patrons = pickle.load(f)
ImportError: No module named 'Patron'

      

I have a class in patronmaker.py named "Patron" but no Patron module, so I'm not sure what the last statement in the error message means. I really appreciate your thoughts on what is missing.

Python version 3.4.1 on a 32-bit Windows machine.

+3


source to share


1 answer


You store all instances of ammo (i.e. self

) in a class attribute Patron

Patron.patrons

. Then you try to sort the class attribute within the class. It may choke pickle

, but I believe I dill

can handle it. Is it really necessary to store all instances of a class in the Patrons list? It's a bit strange...

pickle

serializes classes by reference and doesn't work well with __main__

for many objects. In dill

you do not need to serialize classes by reference and better deal with problems with __main__

. Get it dill

here: https://github.com/uqfoundation

Edit: I tried your code (with one minor change) and it worked.

dude@hilbert>$ python patronmaker.py

      

Then start python ...

>>> import dill
>>> f = open('patrons.pkl', 'rb')
>>> p = dill.load(f)
>>> p
[Julius Caeser, Kunte Kinta, Norton Henrich, Mother Teresa]

      

The only change I made was to uncomment the lines at the end patronmaker.py

to save some patrons .... and I also replaced import pickle

with import dill as pickle

.

So, even while downloading and running my code, I cannot create an error with dill

. I am using the latest one dill

from github.

Additional edit: Your trace above is from ImportError

. Have you installed your module? If you haven't used setup.py to install it, or if you don't have your own module on PYTHONPATH

, then you won't find your module no matter how you serialize things.



Even more changes: Looking at your code, you should use a singleton pattern for patrons

... it shouldn't be inside class Patron

. A class-level block of code to load patrons in is Patron.patrons

bound to cause problems ... and will probably be the source of some bug. I can also see that you are tracing an attribute Patrons.patrons

(not even the class itself) from within the class patrons

- this is crazy - don't do that. Also note that when you are trying to get patrons you are usingPatron.patrons

... this calls the class object, not the instance. Move patrons out of class and use the singleton directly as a patron list. Also you should usually use a patron instance, so if you want each patron to know who all the other patrons are p = Patron('Joe', 'Blow')

, then p.patrons

to get all patrons ... but you need to write a Patrons.load

method that reads the single-user patron list ... you can also be used property

to load

give you something similar to an attribute.

If you create a singleton patrons (as a list) ... or a "register" of patrons (as a dict) if you like, then just check if the patron nest file exists ... to load into the registry ... and don’t do that from patrons class ... it should be much better. Your code is currently trying to load an instance of a class in a class definition when it creates that class object. This is bad...

Also, don't expect people to start downloading your code and debugging it for you when you don't provide a minimal test case or sufficient information about how the trace was generated. You may have encountered an acceptable etching error in the dill

dark corner case, but I can't tell b / c. I cannot reproduce your error. However, I can say that you need refactoring.

And just to be clear:

Move your patrons initializing clutter from Patrons to a new file patrons.py

import os
import dill as pickle

#Initialize patrons with saved pickle data
if os.path.isfile('patrons.pkl'):
    with open("patrons.pkl", 'rb') as f:
        patrons = pickle.load(f)
else: patrons = []

      

Then in the patronmaker.py file and everywhere you need a singleton ...

import dill as pickle
import os.path
import patrons as the

class Patron:

    def __init__(self, lname, fname):
        self.lname = lname.title()
        self.fname = fname.title()
        self.terrCheckedOutHistory = {}
        #Add any created Patron to patrons list
        the.patrons.append(self)
        #Preserve this person via pickle
        with open('patrons.pkl', 'wb') as f:
            pickle.dump(the.patrons, f)

      

And you should be fine if your code does not fall into one of the cases where attributes on modules cannot be serialized because they were added dynamically (see https://github.com/uqfoundation/dill/pull/47 ) , which must necessarily lead to an error pickle

, and in some cases dill

also ... perhaps with a module AtrributeError

. I just can't reproduce it ... and you're done.

+2


source







All Articles