Guest User

Untitled

a guest
May 25th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. import subprocess as sb
  2. from time import sleep
  3.  
  4. bat_file="C:\...\python_env.bat"
  5.  
  6. def executor(commands:list,mode=0):
  7. #initiate the process with the batch file
  8. proc=sb.Popen(bat_file, shell=False, stdin=sb.PIPE, stdout=sb.PIPE, stderr=sb.PIPE,)
  9. sleep(18)#Make sure python gets initiated
  10. if mode==0:
  11. for command in commands:#send commands
  12. proc.stdin.write((command+'rn').encode(encoding='utf-8',errors='strict'))
  13. outp=proc.communicate('print("done") rn'.encode(encoding='utf-8',errors='strict'),timeout=999999999)
  14. elif mode:
  15. commands="rn".join(commands)+"rn"
  16. outp=proc.communicate(commands.encode(encoding='utf-8',errors='strict'),timeout=999999999)
  17. #print all the console outputs
  18. print(outp[0].decode(encoding='utf_8', errors='strict'))
  19. print('done')
  20.  
  21. commands=['import numpy as np','a=np.rand(3,2,1)','print(a)']
Add Comment
Please, Sign In to add comment