Python Forum
run part of a script with a different version of Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
run part of a script with a different version of Python
#1
In my work environment, I have very little leeway with my the software configuration. I concurrently use two versions of python: a 2.xx version to access the API of another software and a 3.xx version for my personal developments.

I am developing a little software for myself and coworkers, and I'd like to keep the interface as simple as possible. My goal -- I'm starting to doubt it's feasible (let alone elegant...) -- is that the user can seamlessly write his custom script using functions relying on the 2.xx version (i.e. function relying on the external software) and functions for python 3.xx.

To make the matter a little more complicated, the 2.xx version needs to be invoked by the external software interpreter. The one thing that might save me is that the execution is quite sequential: pre-processing, processing and post-processing. Only the post-processing needs to be executed with python 3.xx.

So, if I had to reformulate my question, would anyone have an idea/suggestion how to write a script that will be executed by some python 2 interpreter, and at some point of the execution, will switch to another python (3) interpreter? The only avenue I can think of at the moment is to use a subprocess, but I don't think it fulfills all my requierements.

Thank you.
Reply
#2
I think you need two processes. You might consider using remote procedure calls (RPC) to allow the Python 3 process to make calls in the Python 2 process. You could use XMLRPC wich exists in the standard library for both Python 2 and Python 3.

For example here is my Python 2 process
#!/usr/bin/env python2

from SimpleXMLRPCServer import SimpleXMLRPCServer

def is_even(n):
    return n % 2 == 0

server = SimpleXMLRPCServer(("localhost", 8000))
print("Listening on port 8000...")
server.register_function(is_even, "is_even")
server.serve_forever()
Output:
λ python2 paillasse/pf/xserver.py Listening on port 8000... 127.0.0.1 - - [09/Jan/2024 18:02:33] "POST / HTTP/1.1" 200 - 127.0.0.1 - - [09/Jan/2024 18:02:33] "POST / HTTP/1.1" 200 -
And here is my Python 3 process
import xmlrpc.client

with xmlrpc.client.ServerProxy("http://localhost:8000/") as proxy:
    print("3 is even: %s" % str(proxy.is_even(3)))
    print("100 is even: %s" % str(proxy.is_even(100)))
Output:
λ python3 paillasse/pf/xclient.py 3 is even: False 100 is even: True
For more complete example, look in pymotw3 and also the Python 2 version pymotw2
« We can solve any problem by introducing an extra level of indirection »
Reply
#3
Thanks for the suggestion. I had never heard of these, so worse case scenario, I have learned something new. But this gave me food for thoughts, I might be able to introduce this concept in my code.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to find out from outside Python (in Windows) the current version of Python? pstein 4 753 Oct-04-2023, 10:01 AM
Last Post: snippsat
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,283 Jun-29-2023, 11:57 AM
Last Post: gologica
  How to resolve version conflicts in Python? taeefnajib 0 935 Apr-27-2023, 08:37 PM
Last Post: taeefnajib
  Python venv and PIP version issue JanOlvegg 2 1,281 Feb-22-2023, 02:22 AM
Last Post: JanOlvegg
  Python Version upgrade nitinkukreja 1 913 Feb-04-2023, 10:27 PM
Last Post: Larz60+
  Can't update new python version on Pycharm GOKUUUU 6 3,871 Jul-23-2022, 09:24 PM
Last Post: GOKUUUU
  Building python (3.9.5) with different libexpat version (2.4.6) raghupcr 0 1,321 Feb-25-2022, 11:29 AM
Last Post: raghupcr
  Python keeps running the old version of the code quest 2 3,789 Jan-20-2022, 07:34 AM
Last Post: ThiefOfTime
  db migration(db version control) for python lubaz 2 2,780 May-30-2021, 01:36 PM
Last Post: lubaz
  How to use a variable in Python (2.x) to define decimal part? MDRI 4 2,344 May-07-2021, 12:39 AM
Last Post: MDRI

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020