Subprocess use and inputs

Dear all,
I started python code in 2024 (a good resolution is not it ?) to use for a job project and i am currently blocked with the subprocess use.
Basically, i have to run python script (written in CPython with CPython package: numpy, scipy…) in an environnement which does not support CPython. No problem, i have a tricks : subprocess
Indeed, as you know this function allows to run a code with a specified python version for instance. There is just after my code :
[python]
import os
import subprocess
os.chdir(r"My_code_location")
Python312 = r"C:\Users…\AppData\Local\Programs\Python\Python312\python.exe"
subprocess.call([Python312,‘pyNukiyama.py’])
[/python]
With this code i can run my pyNukiyama.py script in my environnement which does not support this last code with python 3.12 installed on the computer. So great, it works !
However, now i need to interact with this pyNukiyama.py script to change the inputs used in this last script (basically, to perform loop with values sent in pyNukiyama.py and retrieve output values.
Thus my question how to do that ?
Maybe, modify the script pyNukiyama.py before run the subprocess with it ? Or, not use a script saved but directly the code in subprocess (pyNukiyama contains just 10 lines…) ? Any other idea ?

Thank you for your help !

Best regards,
Pierre

Instead of modifying the script itself at runtime, an easier method might be to just have your executing program write a data file somewhere, then pass the filename as a command line parameter to the script which will load and use that. Since both are Python you could use the pickle module, allowing you to write arbitrary objects to a file.

If you want bidirectional communication, create an instance of the subprocess.Popen directly instead of using call(). Then you can opt to have access to the stdin/stdout of the process, allowing you to send/receive data directly. A higher-level approach would be to use the Client and Listener classes in multiprocessing.connections. Those automatically pickle/unpickle data, and use either network sockets or named pipes depending on what’s more efficient for your OS.

I’m confused here. If you have access to (via subprocess) an environment that can run the pyNukiyama.py script, why is it not possible to run the first script in that environment?

Dear Spencer Brown & Karl Knechtel,
Thank you very much for your reply !
I think your both ideas : using pickle module (which i do not know how to use but i will dig in) or subprocess.Popen are relevant. I will try both and hopefully it will work.
Basically, at the end I would like to have a code roughly like this:

[Code]
import os
import subprocess
os.chdir(r"My_code_location")
Python312 = r"C:\Users…\AppData\Local\Programs\Python\Python312\python.exe"

#First i define the input for pyNukiyama
V=3 #Water velocity
P=10 #Water pressure
T=100 #Water temperature

subprocess.call([Python312,‘pyNukiyama.py’]) #Here i would like to be able to send the previous three values (V,P,T) needed for pyNukiyama to generate the data i am looking

#retrieve the output of pyNukiyama i will use afterwards

[\code]
Maybe this last idea of the code will help you to give me some more guidance…

Thank you for your help,
Best regards