I have a command to run from python program
python test.py arg1 arg2
If I run it using
os.system("python test.py arg1 arg2")
it runs.
I want to use subprocess.Popen() but when I do that
subprocess.Popen("python test.py arg1 arg2")
It gives the following error
raise child_exception
OSError: [Errno 2] No such file or directory
Any ideas?
Thanks a lot
If argument to Popen is a string, it tries to execute it as 'sh <argument>', so in your case it becomes 'sh python test.py arg1 arg2' that is obviously wrong. You can pass it a list (as twall suggested), or specify shell=True parameter. The next should work:
subprocess.Popen("python test.py arg1 arg2", shell=True)
try putting all your arguments in a list:
subprocess.Popen(["python", "test.py", "arg1", "arg2"])
see: http://docs.python.org/library/subprocess.html#subprocess.Popen
Related
I am trying to run the Perl script rtpc.pl from a Python script from the command line with two input arguments. Normally, I run the Perl script from the command line with two arguments:
perl rtpc.pl [ARG1] [ARG2]
What I want is a separate python script that I can call from the command line that I can input [ARG1] and [ARG2] into while compiling the Perl script rtpc.pl.
Something like:
python3 pyscript.py
So far, in my python script, I am using the subprocess module, but I am a little unsure how to pass arguments for my perl script.
pipe = subprocess.Popen(["perl", "rtpc.pl"], stdout=subprocess.PIPE)
How would I input the arguments needed to equivalently run the Perl script terminal command? I should also mention that the shell that I am using is tcsh.
Add them to the list you pass to subprocess.Popen().
arg1 = 'foo'
arg2 = 'bar'
pipe = subprocess.Popen(["perl", "rtpc.pl", arg1, arg2], stdout=subprocess.PIPE)
I have a file named "uscf" in /usr/local/bin:
#! /bin/sh
python3 ~/Desktop/path/to/uscf.py
I have already chmod +x this file so that I can run it from my terminal with the command "uscf". How can I run this with command line arguments so that the arguments are accessible through sys.argv in uscf.py?
EDIT: Added below example for clarification:
The uscf.py file:
import sys
if len(sys.argv) > 1:
print(sys.argv)
Running it from the command line:
Abraham$ uscf these are arguments
Expected output:
these are arguments
In sh the "$#" variable contains all the positional arguments passed to the script. You can use that to pass it to your python script:
#!/bin/sh
python3 $HOME/Desktop/path/to/uscf.py "$#"
This question already has answers here:
subprocess.Popen() error (No such file or directory) when calling command with arguments as a string
(2 answers)
Closed 2 years ago.
I am trying to write a program that opens a gnome-terminal window and executes a python file in it.
When I call the gnome-terminal subprocess with the subprocess module like this:
import subprocess
subprocess.call(['gnome-terminal', '-x', 'python3 '+filename])
I get the following error:
Failed to execute child process "python3 /home/user/Documents/test.py” (No such file or directory)
I have tried to cd to the directory /home/user/Documents/test.py first and then run the file, but it didn't work.
You're trying to execute the literal command python3 /home/user/Documents/test.py which obviously doesn't exist on your system.
When you type that line in a shell, the shell will split it on spaces and in the end it will call python3 with /home/user/Documents/test.py as argument.
When using subprocess.call, you have to do the splitting yourself.
I believe you need to pass your filename as another element in the array. I don't have gnome-terminal, but I replicated your issue with plain sh.
import subprocess
subprocess.call(['gnome-terminal', '-x', 'python3', filename])
Try this:
from os import system
system("gnome-terminal -e 'bash -c \"python3 %s\"'"%filename)
Add other commands using a semicolon:
system("gnome-terminal -e 'bash -c \"python3 %s; [second command]\"'")
Try this (i assume that python3 is set in PATH)
from subprocess import Popen
command="gnome-terminal -x python3"+filename
proc=Popen(command)
if this not works
then try to run your python file first , and see if it works or not
python filename
I would like to call a shell script from my python script. I need to pass 3 parameters/arguments to the shell script. I am able to call the shell script (that is in the same directory as that of the python script), but having some issue with the parameter passing
from subprocess import call
// other code here.
line = "Hello"
// Here is how I call the shell command
call (["./myscript.sh", "/usr/share/file1.txt", ""/usr/share/file2.txt", line], shell=True)
In my shell script I have this
#!/bin/sh
echo "Parameters are $1 $2 $3"
...
Unfortunately parameters are not getting passed correctly.
I get this message:
Parameters are
None of the parameter values are passed in the script
call ("./myscript.sh /usr/share/file1.txt /usr/share/file2.txt "+line, shell=True)
When you are using shell=True you can directly pass the command as if passing on shell directly.
Drop shell=True (you might need to make myscript.sh executable: $ chmod +x myscript.sh):
#!/usr/bin/env python
from subprocess import check_call
line = "Hello world!"
check_call(["./myscript.sh", "/usr/share/file1.txt", "/usr/share/file2.txt",
line])
Do not use a list argument and shell=True together.
I'm trying to use pdoc to document my python script but I'm having an issue due to the fact that my program requires the use of command line args.
My program is called as follows: myprogram.py -p arg1 -s arg2
And I try to run pdoc as follows: pdoc --html myprogram
But I get an error saying pdoc: error: the following arguments are required: -p
But if I try to run pdoc like such: pdoc --html myprogram.py -p arg1 -s arg2, I get this error:
pdoc: error: unrecognized arguments: -p arg1 -s arg2
IS there a way to run pdoc on a module that requires command line args?
Since pdoc imports the documented modules, you need to nest your CLI program execution under a main guard:
def main():
from argparse import ArgumentParser
parser = ArgumentParser(...)
args = parser.parse_args()
...
if __name__ == '__main__':
# Run main entry point only if the script was run as a program
# and not if it was merely imported
main()