I would like to alter my PYTHONPATH
at runtime (using Python 2), put a new PYTHONPATH
, execute a subprocess
and run my specific code which requires Python3, and then set it back to what it was.
Here is the logic I came up with:
# save the current PYTHONPATH to a variable
pythonpath = os.environ["PYTHONPATH"]
print('PYTHONPATH (Before Execution): ', pythonpath)
# alter the PYTHONPATH
# append or insert would not be the best
# but rather emptying it and putting the new `PYTHONPATH` each time
# use the following
# subp.Popen("")
# subp.call("") : python3 mycode.py -i receivedImgPath", shell=True
# to call the code with the new PYTHONPATH
# after the code execution is done, set the PYTHONPATH to back
# since we have saved it, use directly the variable pythonpath
Is there someone who has done something similar? Is my logic correct?
P.S: I know there are threads like In Python script, how do I set PYTHONPATH? but they only give information about appending or inserting, which does not work with my logic.