ImportError: Import by filename is not supported when unpickleing

Larry Martell larry.martell at gmail.com
Wed Jul 27 22:47:15 EDT 2016


On Wed, Jul 27, 2016 at 10:39 PM, Larry Martell <larry.martell at gmail.com> wrote:
> On Wed, Jul 27, 2016 at 7:45 PM, Jason Benjamin <hexusnexus at gmail.com> wrote:
>> Look at this: https://wiki.python.org/moin/UsingPickle
>> It uses *pickle.dump* not *pickle.dumps*
>
> Yes that uses a file. I do not want to use a file. I want to pass the
> object as a string.
>
>> If you still don't get it send me the code for the function with a working
>> code example the calls that function revealing the error.
>>
>> I've had a lot of experience with Python and it looks like its the sequence
>> of functions that causes the problem and not just one Python function call
>> on its own.
>
> I have an object of type Target:
>
> (Pdb) type(target)
> <class 'workitem.Target'>
>
> And I pickle it like this:
>
> (Pdb) type(pickle.dumps(target))
> <type 'str'>
>
> And then it looks like this:
>
> (Pdb) pickle.dumps(target)
> "ccopy_reg\n_reconstructor\np0\n(cworkitem\nTarget\np1\nc__builtin__\nobject\np2\nNtp3\nRp4\n(dp5\nS'histologySections'\np6\n(lp7\nsS'registrationTransforms'\np8\nV\np9\nsS'valueMaps'\np10\n(dp11\nsS'targetID'\np12\nVRight-CarotidArtery\np13\nsS'targetInitializer'\np14\ng9\nsS'regions'\np15\n(dp16\nsS'bodySite'\np17\nVRightCarotid\np18\nsS'targetLocalFolderName'\np19\nV21597135/wi-54976537/Right-CarotidArtery\np20\nsS'readingsLocalFileName'\np21\ng9\nsS'probabilityMaps'\np22\n(dp23\nsS'targetPath'\np24\ng9\nsb."
>
> And I pass it into a subprocess.Popen call. Then in the program that
> is called, it comes in as a string:
>
> (Pdb) type(args.target)
> <type 'str'>
>
> With the same content:
>
> (Pdb) args.target
> "ccopy_reg\n_reconstructor\np0\n(cworkitem\nTarget\np1\nc__builtin__\nobject\np2\nNtp3\nRp4\n(dp5\nS'histologySections'\np6\n(lp7\nsS'registrationTransforms'\np8\nV\np9\nsS'valueMaps'\np10\n(dp11\nsS'targetID'\np12\nVRight-CarotidArtery\np13\nsS'targetInitializer'\np14\ng9\nsS'regions'\np15\n(dp16\nsS'bodySite'\np17\nVRightCarotid\np18\nsS'targetLocalFolderName'\np19\nV21597135/wi-54976537/Right-CarotidArtery\np20\nsS'readingsLocalFileName'\np21\ng9\nsS'probabilityMaps'\np22\n(dp23\nsS'targetPath'\np24\ng9\nsb."
>
> And when I unpickle it I get the error:
>
> (Pdb) pickle.loads(args.target)
> ***ImportError: Import by filename is not supported.
>
>
> Also, please keep the discussion on the list, and please don't top post.


Also let me add that initially I was calling Popen with shell=False
and the arguments in a list, and that was failing with:

arg 2 must contain only strings

And when I debugged I found that this was happening in the execvp call
in subprocess and arg 2 was a str. So then I changed it using
shell=False with the command line in a string, and that is when I get
the  ImportError error.

>> On Wed, Jul 27, 2016 at 4:29 PM, Larry Martell <larry.martell at gmail.com>
>> wrote:
>>
>> On Wed, Jul 27, 2016 at 6:54 PM, Jason Benjamin <hexusnexus at gmail.com>
>> wrote:
>>
>> If it has and 's' on the end it will only work on strings. *dumps* refers to
>> a string too.
>>
>> Yes, I know. I have an object, which I pickle with dumps, which turns it
>> into a string. Then I try to unpickle it with loads and I get that error.
>> (Pdb) type(args.target) <type 'str'> (Pdb) pickle.loads(args.target) ***
>> ImportError: Import by filename is not supported.
>>
>> On Wed, Jul 27, 2016 at 3:44 PM, Larry Martell <larry.martell at gmail.com>
>> wrote: On Wednesday, July 27, 2016, Jason Benjamin <hexusnexus at gmail.com>
>> wrote:
>>
>> On Wed, 27 Jul 2016 17:25:43 -0400, Larry Martell wrote: > When I try and
>> unpickle an object with pickle.loads it fails with: > > ImportError: Import
>> by filename is not supported when unpickleing > > I've never used pickle
>> before. Why do I get this and how can I fix it? Try using *pickle.load*
>> instead of *pickle.loads*. pickle.loads is for strings. Retrieved from
>> documentation: help(pickle) after importing pickle.
>>
>> I am unpickling a string created with pickle.dumps



More information about the Python-list mailing list