0

I searched on stack overflow but I did'nt find a correct answer.

I use subprocess popen to call and run an entire python script and it works fine, like that :

subprocess.Popen(['python3', '/home/pc/Dossier/Dossier2/Nom.py'], )

But in my system I want to modify a variable in the script Nom.py when I call it with subprocess popen. I heard about inputs parameters for subprcess popen. Do you think that is possible and do you have any examples ?

Thanks for your future answer

Mathias
  • 7
  • 3

1 Answers1

0

There is a much better way to call Python code from within Python, and it's to... call Python code from within Python :)

In other words, make sure that Nom.py can be imported without side effects and has a main function that you can call, and then simply import it and call the function.


But if, for whatever reason, you must do it through subprocess, you can add command line arguments after the name of the script:

subprocess.Popen(['python3', '/home/pc/Dossier/Dossier2/Nom.py', 'some_value'], )

These arguments can be accessed (as strings) in the called script through sys.argv.

Thomas
  • 174,939
  • 50
  • 355
  • 478
  • Exactly ! thank you, that was I looked for regarding sys.argv, I will investigate this part and I will priorize the classic way with Nom.py. Thanks! I'm going to try it – Mathias Jun 09 '22 at 08:25