FileNotFoundError in subprocess.check_output [duplicate] - python

This question already has answers here:
Subprocess.Call fails "The System Cannot Find the File Specified"
(2 answers)
Closed 25 days ago.
I want to connect and use the functions of adb in a python environment.
In the terminal, adb is connected well as shown below.
$ adb --version
Android Debug Bridge version 1.0.41
Version 31.0.3-7562133
Installed as /opt/homebrew/bin/adb
$ command -v adb
/opt/homebrew/bin/adb
To use this in the python environment, subprocess.check_output was used,
I wrote python code as below.
import subprocess
cmd = f"adb --version"
print(f"cmd:{cmd}")
res = subprocess.check_output(cmd).decode("utf-8")
print(f"res:{res}")
I got an error as below.
$ python test.py
cmd:adb --version
Traceback (most recent call last):
File "/Users/hhd/project/hhdpy/test.py", line 5, in <module>
res = subprocess.check_output(cmd).decode("utf-8")
File "/opt/homebrew/anaconda3/envs/hhdpy/lib/python3.9/subprocess.py", line 424, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "/opt/homebrew/anaconda3/envs/hhdpy/lib/python3.9/subprocess.py", line 505, in run
with Popen(*popenargs, **kwargs) as process:
File "/opt/homebrew/anaconda3/envs/hhdpy/lib/python3.9/subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/opt/homebrew/anaconda3/envs/hhdpy/lib/python3.9/subprocess.py", line 1821, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'adb --version'
In my opinion, subprocess.check_output does not seem to be able to import the PATH environment variable,
How should I solve it?

As suggested by John, one possible solution would be to provide --version as an argument.
To do so, you have to split the cmd command into a list, for instance:
res = subprocess.check_output(["adb", "--version"]).decode("utf-8")
tested with Python3.

Related

I get FileNotFound error while using psutil library

I'm trying to run and kill site.py python script every 5 minutes using webreset.py script.
webreset.py:
from psutil import Process, Popen
from time import sleep
pid = 0
while True:
if pid:
Process(pid).terminate()
process = Popen('python3 site.py')
pid = process.pid
sleep(300)
But when I run webreset.py on Ubuntu 20.04, I get the following Error:
Traceback (most recent call last):
File "webreset.py", line 7, in <module>
process = Popen('python3 site.py')
File "/usr/local/lib/python3.8/dist-packages/psutil/__init__.py", line 1316, in __init__
self.__subproc = subprocess.Popen(*args, **kwargs)
File "/usr/lib/python3.8/subprocess.py", line 858, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/usr/lib/python3.8/subprocess.py", line 1704, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'python3 site.py'
I run the script using this command :
sudo python3 webreset.py
You need to separate the command and arguments.
process = Popen(['python3', 'site.py'])
Otherwise it is looking for a command with the filename "python3 site.py"
using this:
Popen(['python3', 'site.py'])
instead of:
process = Popen('python3 site.py')
fixed my problem.

subprocess.run fails to call executable [duplicate]

This question already has answers here:
Get java version number from python
(6 answers)
Actual meaning of 'shell=True' in subprocess
(7 answers)
Closed 1 year ago.
I'm trying to execute a Java program from within a Python program.
import subprocess
subprocess.run("ls") # ok
subprocess.run("whoami") # ok
subprocess.run("java --version") # not ok
I can run standard shell commands but not the Java executable. Why is that?
Traceback (most recent call last):
File "syscall.py", line 4, in <module>
subprocess.run("java --version") # not ok
File "/usr/lib/python3.6/subprocess.py", line 423, in run
with Popen(*popenargs, **kwargs) as process:
File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'java --version': 'java --version'
You can't pass complete commands to subprocess.run. It accepts the parsed list of tokens that it uses to start the subprocess.
subprocess.run(["java", "--version"])
subprocess.run(["ls", "-l"])
One way to bypass would be to pass shell=True to run, but that's not recommended.
You can also do the splitting automatically using shlex.split
subprocess.run(shlex.split("java --version"))
java --version is a shell command, not an executable of its own. You need to pass shell=True.
subprocess.run("java -version", shell=True)
By the way, java uses a single dash for everything, so I changed the command to java -version.
Example output:
>>> import subprocess
>>> subprocess.run("java -version", shell=True)
openjdk version "1.8.0_292"
OpenJDK Runtime Environment (build 1.8.0_292-8u292-b10-0ubuntu1~20.04-b10)
OpenJDK 64-Bit Server VM (build 25.292-b10, mixed mode)
CompletedProcess(args='java -version', returncode=0)

Python subprocess.call raise OSError with linux source command [duplicate]

This question already has answers here:
Calling the "source" command from subprocess.Popen
(9 answers)
Closed 3 years ago.
In python2.7 we can execute external linux commands using subprocess package.
import subprocess
subprocess.call(["ls", "-l"]) // or
subprocess.call("ls -l".split())
both works. I have a file test.sh in current work directory which contains just
date
so I tried
>>> subprocess.call("pwd".split())
/home/ckim
0
>>> subprocess.call("cat test.sh".split())
date
0
>>> subprocess.call("source test.sh".split())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 523, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
What's wrong?
ADD(ANSWER) : This question and answer has enough information (but I'll leave my question here..) Calling the "source" command from subprocess.Popen
source is a shell builtin command. Python's subprocess module is provided to spawn new process, not running a proper Bourne shell (or zsh or ksh or etc.). You cannot access shel builtins from subprocess.call.
To determine if you can or not run a specific command with subprocess module, you may want to use which to get information about the command you need to use:
user#machine: ~
$ which source [7:41:38]
source: shell built-in command
user#machine: ~
$ which cat [7:41:42]
/bin/cat
user#machine: ~
$ which ls [7:41:47]
ls: aliased to ls --color=tty

Errno 13: Passing Python variables to bash script [duplicate]

This question already has answers here:
Can't execute shell script from python subprocess: permission denied
(2 answers)
Closed 6 years ago.
Currently I am testing a very simple piece of code. I simply want to write a python script that sets a variable and then pass that variable into a bash script for use.
Python Script:
from subprocess import check_call
a = str(3)
check_call(["/home/desktop/bash2pyTest/test.sh", a], shell=False)
Bash Script:
#!/bin/bash
echo "This number was sent from the py script: " $1
I have read other Q&As that are related to this topic; however, I am not finding a solution that I am conceptually understand; thus, the syntax above might be incorrect. I have tried a few other methods as well; however, I keep receiving the following error:
Traceback (most recent call last):
File "/home/cassandra/desktop/bash2pyTest/callVar.py", line 3, in <module>
check_call(["/home/cassandra/desktop/bash2pyTest/test.sh", a], shell=False)
File "/usr/lib64/python2.7/subprocess.py", line 537, in check_call
retcode = call(*popenargs, **kwargs)
File "/usr/lib64/python2.7/subprocess.py", line 524, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
OSError: [Errno 13] Permission denied
Process finished with exit code 1
Any help would be greatly appreciated. I'm stumped.
Try
chmod +x /home/desktop/bash2pyTest/test.sh
in shell. The file you are trying to execute is not executable.
Or another option in python script:
check_call(["sh","/home/desktop/bash2pyTest/test.sh", a], shell=False)
The error says that permission is denied. I tested your code, and it works fine on my machine. However, you will need to be sure that the user running the command has sufficient privileges for the test.sh script. Most importantly, be sure that test.sh has execute permissions set. That's often the most easily missed permission.

Python process.call() error

I have OSX and am running the python script out of the Unix shell
I'm running a python code that should open an application. I've been testing with Firefox.app and have been getting
Traceback (most recent call last):
File "/Users/brennan/Desktop/Coding/Wilix/wilix.py", line 453, in <module>
subprocess.call(["open -a "+cp2])
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
My code is:
subprocess.call(["open -a "+cp2])
where cp2 is user input. (Firefox in this case)
if I cd into the programs directory and then do
open -a Firefox
firefox opens fine.
if I change my code to
subprocess.call(["open -a Firefox"])
I still get the error message.
You're passing open -a Firefox as one argument, as if you ran this in the shell:
$ "open -a Firefox"
You need to split up the items:
subprocess.call(['open', '-a', 'Firefox'])
Try giving the full path of firefox app.
It's wrong to use subprocess.call without shell=True or providing command as a list. Please, take a look at first examples in the docs:
http://docs.python.org/2/library/subprocess.html
Full path to Firefox may be needed or may be not needed.

Categories

Resources