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 9132385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:18:12+00:00 2026-06-17T08:18:12+00:00

If I have a script that defines a class: script = class myClass: def

  • 0

If I have a script that defines a class:

script = """

class myClass:
    def __init__(self):
        self.name = 'apple'
        self.color = 'green'

"""

and then exec this script in its own namespace dict:

NS = {}
exec script in NS

and then create an instance of the class and pickle it:

a = NS['myClass']()

import pickle

save = pickle.dumps(a)

Now if I try to unpickle it:

load = pickle.loads(save)

I get the error

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

I gather that this doesn’t work because python doesn’t know where to find myClass in order to rebuild the object. But myClass does exist in the NS dict. Is there a way to tell pickle where to find the class for the object it is loading?

  • 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-06-17T08:18:12+00:00Added an answer on June 17, 2026 at 8:18 am

    I discovered a solution this. It seems the problem is executing code in a dict prevents python from figuring out where the class is defined. The solution is to create an empty module, execute the code in the module, and then add the module to sys.modules so python knows about it.

    script = """
    class myClass:
        def __init__(self):
            self.name = 'apple'
            self.color = 'green'
    """
    
    import imp, sys
    
    moduleName = 'custom'
    
    module = imp.new_module(moduleName)
    
    exec script in module.__dict__
    
    sys.modules[moduleName] = module
    

    Now it is possible to pickle and unpickle an instance of the class:

    import pickle
    a = module.myClass()
    s = pickle.dumps(a)
    b = pickle.loads(s)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written the following class: class myClass(object): def __init__(self): pass def foo(self, arg1,
I have a Ruby script that defines a class. I would like the script
I have a python script that I am writing for a class assignment which
I have a simple example script constructed that defines three separate processes using multiprocessing
I have a script that generates DDL scripts to define materialized views for a
I have an awk script that I have defined thus: #!/usr/bin/env awk BEGIN {
I have a jQuery search script that uses tabs for the user to define
I have script that reads remote file content and writes it to local server.
I have this script that run to fix my menu bar to the browser
I have a script that recursively loops through all the sub directories and compresses

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.