1

I want to use a python2 module from python3. My idea was to have the python2 work in it's file, then use the subprocess module from python3 with args (to Popen) being ["python2", "script.py"]. The methods provided return all strings. Is it possible to also capture data structures directly from the running process, for instance if I have generated a final dictionary in the script.py and I want to get that dictionary Object as the result of the computations in script.py. Any helps would be appreciated.

4
  • 2
    You could serialize your dict to a JSON in Python2 and load the JSON in Python3... tutorialspoint.com/… Commented Apr 4, 2021 at 20:25
  • You Mean writing the json to a file from Py2 then reading the file fromPy3? Commented Apr 4, 2021 at 21:27
  • Or just write the JSON on stdout in Python2 and read it in Python 3 as the output from subprocess. Commented Apr 4, 2021 at 21:29
  • I've added it as an answer for completeness. Commented Apr 5, 2021 at 10:34

1 Answer 1

2

You could serialize your dict to a JSON in Python2 and load the JSON in Python3. There is an example here.

Rather than pollute the filesystem with unnecessary writes and files, you could just print the JSON on stdout in your Python 2 script and then read it as the output from subprocess() in your Python 3 script. That will depend on your local environment.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.