PyQt from py27 to py38

Florian Bruhin me at the-compiler.org
Tue Oct 26 10:47:37 BST 2021


Hey,

On Tue, Oct 26, 2021 at 08:12:13AM +0000, Nicola Raffo wrote:
> I have a question about pyqt4 and pyqt5. I have an object serialized
> using python 2.7 and pyqt4 library, and I would like to reopen it in
> python 3.8 using pyqt5.
> 
> I receive an error ("no module named pyqt4") when calling
> pickle.loads() reading the file containing the pickle data.
>
> What should I do in order to be able to reopen the serialized object
> as a Python Object?

Pickle isn't really suitable for (de)serializing across different 
library versions:
https://nedbatchelder.com/blog/202006/pickles_nine_flaws.html

For "normal" Python data, consider using e.g. JSON instead, either via
manual (de)serialization, or perhaps with something like marshmallow:
https://marshmallow.readthedocs.io/en/stable/

With most Qt objects, it's also possible to use Qt's own serialization
via QDataStream.

As for loading the existing pickled data: The easiest way is probably to
load it using the old configuration, and then serialize it into a more
proper format. The still-quite-easy way would be to get a copy of PyQt4
for Python 3.8 and load it using that. The harder way is to somehow
coerce pickle into giving you the "raw" data without the PyQt object
involved...

Florian

-- 
            me at the-compiler.org | https://www.qutebrowser.org 
       https://bruhin.software/ | https://github.com/sponsors/The-Compiler/
       GPG: 916E B0C8 FD55 A072 | https://the-compiler.org/pubkey.asc
             I love long mails! | https://email.is-not-s.ms/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://www.riverbankcomputing.com/pipermail/pyqt/attachments/20211026/cbc34b8a/attachment.sig>


More information about the PyQt mailing list