0

I am attempting to use the Subprocess module to try and send a command line instruction to the command prompt. Here is the relevant code:

def get_primers(self):
    if self.inputbool == False or self.outputbool == False:
        tkinter.messagebox.showinfo('AUTOPRIMER', 'No input file and/or output destination detected!')
    else:
        if self.parambool == False:
            outputlocation = '-output=' + '"' + self.output + 'primer3output.txt' + '"'
            cmd = ['primer3_core', outputlocation, self.input]
            cmd2 = 'primer3_core' + ' ' + outputlocation + ' ' + self.input
            print(cmd2)
            subprocess.call(cmd)
            tkinter.messagebox.showinfo('AUTOPRIMER', 'Please check output file for desired content. If it is incorrect, please alter settings to achieve desired output.')
        else: #parameters present
            outputlocation = '-output=' + '"' + self.output + 'primer3output.txt' + '"'
            p3filesettings = self.p3filestring + '"' + self.param + '"'
            cmd = ['primer3_core', p3filesettings, outputlocation, self.input]
            cmd2 = 'primer3_core' + ' ' + p3filesettings + ' ' + outputlocation + ' ' + self.input
            print(cmd2)
            subprocess.call(cmd)
            tkinter.messagebox.showinfo('AUTOPRIMER', 'Please check output file for desired content. If it is incorrect, please alter settings to achieve desired output.')

The general format of the command is a call to the application itself, an output filepath, and an input filepath. There may be a filepath to a file that contains additional settings if the user desires to add one.

Example:

primer3_core -p3_settings_file="C:/Users/mqian/Desktop/CGIProject/autoprimercode/output/test files/testingsettingscopy.txt" -output="C:/Users/mqian/Desktop/CGIProject/autoprimercode/output/test files/10-3/test2primer3output.txt

Typing that command into the command prompt runs the program properly, but when I try to pass the components as a list or even one whole string to the subprocess.call() function, the output I get is as follows:

C:\Users\mqian\AppData\Local\Programs\Python\Python37-32\python.exe C:/Users/mqian/Desktop/CGIProject/autoprimercode/windowsversion/workingwindowsautoprimer.py
primer3_core -p3_settings_file="C:/Users/mqian/Desktop/CGIProject/autoprimercode/output/test files/testingsettingscopy.txt" -output="C:/Users/mqian/Desktop/CGIProject/autoprimercode/output/test files/10-3/test2primer3output.txt" "C:/Users/mqian/Desktop/CGIProject/autoprimercode/output/test files/primerflowinput"
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\mqian\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1702, in __call__
    return self.func(*args)
  File "C:/Users/mqian/Desktop/CGIProject/autoprimercode/windowsversion/workingwindowsautoprimer.py", line 77, in get_primers
    subprocess.Popen(cmd)
  File "C:\Users\mqian\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 756, in __init__
    restore_signals, start_new_session)
  File "C:\Users\mqian\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1155, in _execute_child
    startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified

I tried setting shell=True to see if that would make a difference, and now it is not recognizing primer3_core as a valid command even though I have added it to the PATH.

'primer3_core' is not recognized as an internal or external command,
operable program or batch file.

Currently, my theory is that the way quote marks are handled by the command prompt in windows might be the issue as their are double quotes in the command, but I'm not sure. Any help would be great!

Thunderpurtz
  • 189
  • 2
  • 12
  • _it is not recognizing primer3_core as a valid command even though I have added it to the PATH_ How, exactly, did you add it to `PATH`? – John Gordon Oct 04 '18 at 17:09
  • This is telling you that it can't find your executable. Try providing a full path to the executable. – g.d.d.c Oct 04 '18 at 17:09
  • @JohnGordon Followed a few SO guides and google like so: https://stackoverflow.com/questions/44272416/how-to-add-a-folder-to-path-environment-variable-in-windows-10-with-screensho – Thunderpurtz Oct 04 '18 at 17:27
  • @g.d.d.c isn't the point of adding it to the PATH so that you don't have to provide a full path? When I type primer3_core without the full path in the command prompt it works fine. I will try your suggestion though. – Thunderpurtz Oct 04 '18 at 17:28
  • I mean, ostensibly, yes. But, if you're getting an error that it can't find your program, then you either don't have it added to the path properly, it's not in the system path, it's only in the user path, or something else is broken. If it works with a full path, then you might try with `shell = True` in `subprocess.call`, but that has other implications as well. – g.d.d.c Oct 04 '18 at 17:29
  • @g.d.d.c so it seems to work with the full filepath. Would you be able to recommend a way to make this customizable to the user though? Let's say on another machine, the user installs that particular executable somewhere else? How would I go about grabbing out without needing to ask the user explicitly where it is everytime? – Thunderpurtz Oct 04 '18 at 17:58
  • Nope. That's a _bad idea_ ™. Your choices would involve: a. `os.walk` or b. `while prog not in os.listdir('.'): os.chdir('..')`. Those are not good ideas at all, ever. Don't do this. If you aren't the person providing the executable and in control over where it lives, someone _has to tell you_. Don't just go try to find it. – g.d.d.c Oct 04 '18 at 19:39
  • Check the value of the `PATH` environment variable in the script, e.g. `print(*os.environ['PATH'].split(';'), sep='\n')` will list it with one directory per line. Note that no directory in `PATH` should be quoted with double quotes. – Eryk Sun Oct 05 '18 at 03:14

0 Answers0