Note that there are some explanatory texts on larger screens.

plurals
  1. POPython: Attribute error with shelve when using 2 py modules
    primarykey
    data
    text
    <p>I'm trying out the shelve module and I'm running into some trouble. My intention is to have one module call a function in a second one which will modify a file inside a shelve. I made a prototype (below) that simply tries to set a variable to something stored in the shelve and print that object.</p> <p>When I attempt to do this I get the following attribute error:</p> <pre><code>AttributeError: 'module' object has no attribute 'User' </code></pre> <p>My guess is it doesn't realize my 'User' class exists but I'm unsure of the reason why. File1 calls the same function as File2 does in it's main function and it correctly works in that instance. I'd appreciate any help with this problem, I'm confused on what the issue is.</p> <pre><code>import SE_ex2 SE_ex2.cacheTest() </code></pre> <hr> <pre><code>import shelve cache_users = shelve.open("usersTest.cache") class User(object): def __init__(self, name, num): self.name = name self.num = num def __str__(self): return "Name:{0}, Num={1}".format(self.name, self.num) def cacheTest(): obj = cache_users[ "user_red" ] print obj if __name__ == "__main__": cacheTest() #Used for creating the cache for the first time # red = User("red", 11) # green = User("green", 22) # blue = User("blue", 33) # # cache_users['user_red'] = red # cache_users['user_green'] = green # cache_users['user_blue'] = blue # print "DONE" </code></pre> <hr> <p>Full Traceback:</p> <pre><code>Traceback (most recent call last): File "C:\Users\akaGrim\My Documents\Aptana Studio 3 Workspace\ShelveTest\SE_ex1.py", line 3, in &lt;module&gt; SE_ex2.cacheTest() File "C:\Users\akaGrim\My Documents\Aptana Studio 3 Workspace\ShelveTest\SE_ex2.py", line 14, in cacheTest obj = cache_users[ "user_red" ] File "C:\Python27\lib\shelve.py", line 122, in __getitem__ value = Unpickler(f).load() AttributeError: 'module' object has no attribute 'User' </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload