Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import subprocess as sb
- from time import sleep
- bat_file="C:\...\python_env.bat"
- def executor(commands:list,mode=0):
- #initiate the process with the batch file
- proc=sb.Popen(bat_file, shell=False, stdin=sb.PIPE, stdout=sb.PIPE, stderr=sb.PIPE,)
- sleep(18)#Make sure python gets initiated
- if mode==0:
- for command in commands:#send commands
- proc.stdin.write((command+'rn').encode(encoding='utf-8',errors='strict'))
- outp=proc.communicate('print("done") rn'.encode(encoding='utf-8',errors='strict'),timeout=999999999)
- elif mode:
- commands="rn".join(commands)+"rn"
- outp=proc.communicate(commands.encode(encoding='utf-8',errors='strict'),timeout=999999999)
- #print all the console outputs
- print(outp[0].decode(encoding='utf_8', errors='strict'))
- print('done')
- commands=['import numpy as np','a=np.rand(3,2,1)','print(a)']
Add Comment
Please, Sign In to add comment