0

I'm having issues with the subprocess module. I'm trying to run a terminal command in Python, which works perfectly fine in the terminal. The command is:

hrun SomeAction LogFile

I've tried a variety of options, including call(), run(), check_output(), and Popen(). No matter which method I use, I get the error:

FileNotFoundError: [Errno 2] No such file or directory: 'hrun': 'hrun'

My code is:

    output = Popen(["hrun", "SomeAction", log_file_name], stdout=PIPE, stderr=PIPE)

where "hrun" and "SomeAction" are strings and log_file_name is a string variable. I found other SO issues and, most (if not all) were resolved with either shell=True (which I dont want), or because the issue was due to a string instead of a list argument.

Thanks!

helloworld95
  • 366
  • 7
  • 17

1 Answers1

0

If you are just trying to run a command from a prompt within a script why not use something like

import os
os.system("your command")

You should be able to just run it like

os.system("hrun SomeAction LogFile")
Reedinationer
  • 5,661
  • 1
  • 12
  • 33
  • Thanks for the reply! I tried os.system(), and it still doesn't work, but at least it doesn't throw an error! I'm getting this output: sh: hrun: command not found. I'm also getting an unexpected output, which I'm assuming has to do with the command not found issue. – helloworld95 Mar 08 '19 at 01:49
  • I don't recognize `hrun` as a standard command, so maybe if it's a custom script and is missing from your `PATH` variable in your environment? Does the command work in a normal terminal? – Reedinationer Mar 08 '19 at 01:52
  • hrun I believe is a Perl script. I'm running it on Linux, and yes it works as a normal terminal command – helloworld95 Mar 08 '19 at 01:53
  • @helloworld95 I would try finding the Perl script on your machine and adding it to your PATH. You can do this through the `sys` module within your script. Alternatively you can `os.chdir()` to that location within your script and it should work (personally what I recommend cause changing PATH can have unexpected consequenses), or you can add it to your PATH following a process like https://stackoverflow.com/questions/46188012/how-do-i-add-directories-to-path – Reedinationer Mar 08 '19 at 02:03