0

I want to execute a batch file by calling it from a python script. The thing is that this batch needs JAVA_HOME to be set. I don't want to set this variable in the system env Variable, i want to do it from python. Is there anyway to execute two batch commands via Like this

p = Popen("""set JAVA_HOME=D:\GIT\G_ATC_FCT_SW_HSM_Daimler\Tools\jdk-8.0.242.08-hotspot;D:\\GIT\\G_ATC_FCT_SW_HSM_Daimler\\Tools\\plugins\\com.nxp.id.jcop.eclipse_6.0.0.8\\cmds\\converter.bat Java_Card_Converter-3.0.5""")
    stdout, stderr = p.communicate()

But i got this error FileNotFoundError: [WinError 2] The specified file could not be found. Is it possible to do it from popen ?

  • Your backslashes are not escaped (which is likely the source of the `FileNotFoundError`) – GPhilo Nov 22 '21 at 10:40
  • Also, if all else fails, you can make a batch file with the two commands and run that one via Popen – GPhilo Nov 22 '21 at 10:41
  • @GPhilo Thanks for your feed back i tested with double Backslashes but it's not fixing the problem i think that Popen can be used only for executing Batch or shell script. Also i don't want to create a batch script to do the two commands since i can do it from python. – Lyes kheffache Nov 23 '21 at 08:14
  • Then since you don't actually need to run two commands, but only run the script with a modified environment, use the solution from here: https://stackoverflow.com/a/4453495/3214872 – GPhilo Nov 23 '21 at 08:18
  • Thx @GPhilo the link that you gave me helped a lot, gonna validate the solution – Lyes kheffache Nov 29 '21 at 09:19

1 Answers1

0

The response provided by @GPhilo helped me. Here is solution to my problem:

Then since you don't actually need to run two commands, but only run the script with a modified environment, use the solution from here: Python subprocess/Popen with a modified environment – GPhilo