Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Importing script as module causes ImportError with pickle.load #58498

Closed
rj3d mannequin opened this issue Mar 13, 2012 · 3 comments
Closed

Importing script as module causes ImportError with pickle.load #58498

rj3d mannequin opened this issue Mar 13, 2012 · 3 comments
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@rj3d
Copy link
Mannequin

rj3d mannequin commented Mar 13, 2012

BPO 14290
Nosy @avassalotti

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = None
closed_at = <Date 2013-05-03.00:59:44.129>
created_at = <Date 2012-03-13.19:40:40.819>
labels = ['type-bug', 'library']
title = 'Importing script as module causes ImportError with pickle.load'
updated_at = <Date 2013-05-03.00:59:44.128>
user = 'https://bugs.python.org/rj3d'

bugs.python.org fields:

activity = <Date 2013-05-03.00:59:44.128>
actor = 'alexandre.vassalotti'
assignee = 'none'
closed = True
closed_date = <Date 2013-05-03.00:59:44.129>
closer = 'alexandre.vassalotti'
components = ['Library (Lib)']
creation = <Date 2012-03-13.19:40:40.819>
creator = 'rj3d'
dependencies = []
files = []
hgrepos = []
issue_num = 14290
keywords = []
message_count = 3.0
messages = ['155651', '164825', '188286']
nosy_count = 3.0
nosy_names = ['alexandre.vassalotti', 'flub', 'rj3d']
pr_nums = []
priority = 'normal'
resolution = 'works for me'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue14290'
versions = ['Python 2.7']

@rj3d
Copy link
Mannequin Author

rj3d mannequin commented Mar 13, 2012

I implemented a data-structure as an object in a script, let's call it objectScript.py. I'm using this data-structure in other scripts like so:

from objectScript import data-structure

Populating this data-structure requires quite a bit of time, so when I'm done with it, I like to pickle it. However, if I try to load it from the pickled file, I get the following error:

ImportError: No module named objectScript

However, if I replace my 'from objectScript import data-structure' statement with the actual data-structure class definition from the objectScript.py file when I am pickling the object but then revert to the import statement when I am unpickling the object, everything works fine.

@rj3d rj3d mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Mar 13, 2012
@flub
Copy link
Mannequin

flub mannequin commented Jul 7, 2012

Hi, I think this is a usage error and if not you should try to provide a test case with both files for this.

Pickle needs to be able to import the module which contains the classes by the same name as the original module. That means pickling an instance of a class defined in a script will not work unless it is the same script which did the pickling. The object is probably pickled under the name __main__.YourClass and when you import it in another script it will be objectScript.YourClass, hence pickle is unable to find the class for the object you are trying to unpickle.

@avassalotti
Copy link
Member

Without a test case, we cannot tell if this is a bug in pickle or not. Anyhow, Floris's explanation is pretty much on the dot as why you might see this error.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

1 participant