1

So my issue is the following:

I am using a version of python with my main libraries, to do something I need to interface with a program's python libraries. That program comes with a .bat file that sets up the variables and start python.

What I want to do from my main python is the following:

  1. call the .bat file
  2. From the python session created import my custom function
  3. Send inputs to me custom functions (nested lists of strings mostly)
  4. Stop the new python instance once the data is processed

How can such a thing be done from python? Should I anchor myself to the cmd prompt that is created in order to be able to send command to the new python instance? Would os or subprocess still be viable or would I need to create something like a powershell script to hand it all?

Thanks.

  • This sounds like something I've done in the past but not 100% certain but for example.... Pid = os.getpid() and DETACHED_PROCESS = 0x00000008 and RunScript = "C:/Folder/Path/VerifyPID.cmd" and Cmd = ["cmd.exe", "/C", RunScript, sys.argv[0], TagName, host, RootPath, str(Pid)] and subprocess.Popen(Cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE, creationflags=DETACHED_PROCESS).... Maybe that will give you a rough idea but I have more examples if needed. Again I'm not 100% certain but potentially something along these lines will help. – Pimp Juice IT May 21 '18 at 23:55
  • Here's a link with more detail of what the code would look like to help clarify some potentially..... justpaste.it/663m5 .... The logic I use is a lot more complex using MySQL and PLC modules, etc. and calling functions and such.... maybe you will find helpful to at least get you pointed in a direction that may help. In the end, the batch script will accept arguments as %~1 thru %~5 essentially. – Pimp Juice IT May 22 '18 at 0:04
  • @PimpJuiceIT Thanks! I'll look into it and see if it can be done that way. – Al rl May 22 '18 at 13:22
  • 1
    @PimpJuiceIT Your cues helped for my solution, though I made some changes but the concept should be familiar to you. Thanks again! – Al rl May 24 '18 at 1:00
1

So I think I found a way to solve this as it appears that the subprocess is continuous it was quite straightforward.

Here is the code I used:

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+'\r\n').encode(encoding='utf-8',errors='strict'))
        outp=proc.communicate('print("done") \r\n'.encode(encoding='utf-8',errors='strict'),timeout=999999999)
    elif mode:
        commands="\r\n".join(commands)+"\r\n"
        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')

I used stdin.write since it was the only way to send multiple commands instead of having to restart my python process for each instance, and I have also made a mode that concatenate everything together in order to all be handled by communicate.

As an example the input of the function could be :

commands=['import numpy as np','a=np.rand(3,2,1)','print(a)']

EDIT_ important things to consider

For anyone planning to rely on this, if you plan to send string there are 2 important things to consider!

  1. You must find a way to preserve your quotation marks on each end of the string, here are the possible ways to preserve them ['"',"'",'\'',"\"","\'",'\"',"""'""",'''"''']
  2. Another thing that is important to consider is if you plan on using string that indicate paths or contain \ in them, add 'r' to the start of the string so the encode will interpret it as a raw string and not throw an error cause by \ and the characters surrounding it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.