Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6654571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:24:20+00:00 2026-05-26T01:24:20+00:00

I’m trying out the shelve module and I’m running into some trouble. My intention

  • 0

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.

When I attempt to do this I get the following attribute error:

AttributeError: 'module' object has no attribute 'User'

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.

import SE_ex2

SE_ex2.cacheTest()

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"

Full Traceback:

Traceback (most recent call last):
  File "C:\Users\akaGrim\My Documents\Aptana Studio 3 Workspace\ShelveTest\SE_ex1.py", line 3, in <module>
    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'
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T01:24:21+00:00Added an answer on May 26, 2026 at 1:24 am

    Fix:

    import SE_ex2
    from SE_ex2 import User
    
    SE_ex2.cacheTest()
    

    Pickled class needs to be in the namespace

    The reason is shelf uses pickle, so it uses the User class:

    When working with your own classes, you must ensure that the class
    being pickled appears in the namespace of the process reading the
    pickle. Only the data for the instance is pickled, not the class
    definition. The class name is used to find the constructor to create
    the new object when unpickling. Take this example, which writes
    instances of a class to a file:

    quoted: http://www.doughellmann.com/PyMOTW/pickle/index.html#problems-reconstructing-objects

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I am trying to loop through a bunch of documents I have to put
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.