I am working with a library that primarily reads and write files and needs to be interoperable in python2 and python3. I have found that sometimes a file written in python3 cannot be opened in python2 due to, for instance, string representation. I've also seen similar problems occur with different versions of pickle. I'd like to write tests for py2/py3 interoperability. What is the best way to write a test that uses multiple python sessions for a single test? Would subprocess be useful here?
Asked
Active
Viewed 33 times
1 Answers
-1
files can be opened the same for both python 2 and 3. Now while file contents may differ if you store string representations and/or pickle data, that can be avoided by not using those constructs - use some common data format instead, like json or a sql database or even a simple csv text file. Then your file will read the same on python 2 and 3.

nosklo
- 217,122
- 57
- 293
- 297
-
Unfortunately I am locked to using HDF5 and cannot use the constructs mentioned. – ben.dichter Jul 12 '18 at 19:51
-
1@ben.dichter you can still use HDF5 - HDF5 works the same in python 2 and 3 - just don't store pickled objects or string representations in it - store in a common, shareable format, I suggest json – nosklo Jul 12 '18 at 19:53
-
Unfortunately I do not have much say in the file format, only the library around it, so while I agree that these constructs might have been a better choice, they cannot help me in this circumstance – ben.dichter Jul 12 '18 at 21:16
-
@ben.dichter then unfortunately you might be doomed with your **artificial** restrictions. There is no way to make pickle and/or string representations work in a way that is compatible with both python 2 and 3. You have to recheck your constraints and maybe make some compromise - you can always change the libraries or choose to not use them. – nosklo Jul 12 '18 at 21:35
-
@ben.dichter I'd like to add that both pickle and string representations are not meant to be shared in any way - that is well known and stated in python official documentation - so just by using them to share data is already wrong in first place. – nosklo Jul 12 '18 at 21:36