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

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

Related

FileNotFoundError in subprocess.check_output [duplicate]

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.

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)

How to run test command and get no exception in Python? [duplicate]

This question already has answers here:
"OSError: [Errno 2] No such file or directory" while using python subprocess with command and arguments
(3 answers)
Closed 4 years ago.
If I run this
test -d a
in Linux command line, it returns nothing and prints no error even if directory a doesn't exist, because the result is returned via exit code.
But if I want to grab this exit code with Python, I get
import subprocess
subprocess.call('test -d a')
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/opt/anaconda3/envs/python27/lib/python2.7/subprocess.py", line 168, in call
return Popen(*popenargs, **kwargs).wait()
File "/opt/anaconda3/envs/python27/lib/python2.7/subprocess.py", line 390, in __init__
errread, errwrite)
File "/opt/anaconda3/envs/python27/lib/python2.7/subprocess.py", line 1025, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
How to overcome this? Changing command text is not allowed, anly python caller code should change. It shoudl return 0 or 1 depending on directory existence as described in manual.
Your issue might be that Python cannot find test -d a rather than test failing and subprocess.call identifying the error and rasing an seemingly appropriate OSError exception. See the exceptions region of the subprocess.call/Popen docs. The right way to call it would be subprocess.call(['test', '-d', 'a'])

call function not working with cd command [duplicate]

This question already has answers here:
Why can't I change directories using "cd" in a script?
(33 answers)
Closed 6 years ago.
I am trying to execute some shell commands using python :
The command is cd /home/n1603031f/Desktop/parsec/wd/
It works fine through the shell, but when executed through python it does not work :
path_to_wd = "/home/n1603031f/Desktop/parsec/wd/"
call(["cd",path_to_wd])
Error :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child
raise child_exception
I need this command to work as the original command I want to execute is :
cd ./parsec/wd/ && tar -cf ../abcd.tar *
which works correctly only when you change directories to not create the top-level folders in the .tar file
Even if you had the right call to change directories it wouldn't accomplish what you want because each subprocess.call creates a separate process.
What you really need is the cwd argument to subprocess.Popen to say what directory you want to work in. Additionally you need to use os.listdir since the subprocess call won't go through a shell to expand the * glob. This is the right way to do what you're trying to do:
d = './parsec/wd'
subprocess.Popen(['tar', '-cf', '../abcd.tar'] + os.listdir(d), cwd=d).wait()
However, os.listdir will list hidden files as well, if you want to you can filter them out beforehand:
files = [f for f in os.listdir(d) if not f.startswith('.')]
If you really need to (and you don't,) you can use shell=True to get this to work with *. Though unless you're working with trusted input shell=True is widely considered a security vulnerability.
subprocess.Popen('tar -cf ../abcd.tar *', shell=True, cwd='./parsec/wd').wait()
If you need your python process to change it's current working directory, use
os.chdir('./parsec/wd')

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.

Categories

Resources