Skip to content
Advertisement

Issue with Subprocess Popen Python

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement