python subprocess.call arguments - python

on Linux I have the following python source file called visca.py:
from subprocess import call
def recall(preset):
call(["visca-cli", "memory_recall", str(preset)])
I open python interpreter in shell and import visca, then i type visca.recall(0) and get
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "visca.py", line 13, in recall
subprocess.call(["visca-cli", "memory_recall", str(preset)]) File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait() File "/usr/lib/python2.7/subprocess.py", line 629, in __init__
raise TypeError("bufsize must be an integer") TypeError: bufsize must be an integer
However, if I type directly in python shell
>>> from subprocess import call
>>> call(["visca-cli", "memory_recall", "0"])
10 OK - no return value
0
it works. What's the problem?

It's telling you that bufsize must be an integer. I am going to guess that whatever value you set for preset in the script is not an integer (keep in mind that 0.0 is a float, not an integer). Do a quick check for what your argument is by printing it out in the function.

Related

How to determine the value of a variable causing a TypeError in a library?

I want to be able to see what values are causing the TypeError to avoid reading the libraries code.
For Example my Traceback is:
Traceback (most recent call last):
File "/censored/trunk/censored-cli/censored.py", line 945, in <module>
detection(filelist, []) #empty options for now because we dont need any
File "/censored/trunk/censored-cli/censored.py", line 770, in detection
subprocess.run(analysis_cmd,env=env,check=True)
File "/home/name/.julia/conda/3/lib/python3.9/subprocess.py", line 505, in run
with Popen(*popenargs, **kwargs) as process:
File "/home/name/.julia/conda/3/lib/python3.9/subprocess.py", line 951, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "/home/name/.julia/conda/3/lib/python3.9/subprocess.py", line 1741, in _execute_child
env_list.append(k + b'=' + os.fsencode(v))
File "/home/name/.julia/conda/3/lib/python3.9/os.py", line 810, in fsencode
filename = fspath(filename) # Does type-checking of `filename`.
TypeError: expected str, bytes or os.PathLike object, not int
I would much rather see the value of filename on line 810 in fsencode, than to know it was interpreted as an int.
I know I can find fsencode in my install and change it to print the value, but I would prefer a built-in way to check the value of an error causing variable.
Is there a way to do this?
Use an IDE with an integrated debugger, like PyCharm.
Set a break point at the line of code which produces the error
Debug the program - it will stop at the break point and then you will see all the variables

What is the correct way to use stdin, stdout, and stderr in Python 2.7?

Does anyone have experience using the subprocess.call command in Python?
I keep getting errors whenever a line like this is in my code:
INFILE1 = open(script_dir+"/Scripts/plot_TSS_profile.R","r")
subprocess.call("Rscript","--slave","--args",filenames["housekeeping_profile"]+" "+filenames["unexpressed_profile"]+" "+filenames["profile_plot"],stdin=INFILE1, stderr=ERR_LOG,stdout=OUT_LOG,shell=True)
INFILE1.close().
If I leave the code as is, I get an error that the program finds multiple values for each th stdin, stderr, and stdout for some reason even though those are the only ones in the code. If I take out these parameters and just put the infile early in the brackets after “--args”, it doesn’t seem to read the file as it says the ‘buffer should be an integer.’
For example, this way gives the buffer error:
INFILE1 = script_dir+"/Scripts/plot_TSS_profile.R"
subprocess.call("Rscript",INFILE1,"--slave","--args",filenames["housekeeping_profile"]+" "+filenames["unexpressed_profile"]+" "+filenames["profile_plot"],stderr=ERR_LOG,shell=True)
INFILE1.close()
Here are my error outputs for more specific information:
The one on buffsize:
Traceback (most recent call last):
File "/mnt/work1/users/pughlab/projects/IEG_MiSEQ/Inferring_DNA_Expression/ExpressionPrediction-master/expression_prediction.py", line 277, in
step5(ERR_LOG,OUT_LOG,args,proj_dir,script_dir,filenames)
File "/mnt/work1/users/pughlab/projects/IEG_MiSEQ/Inferring_DNA_Expression/ExpressionPrediction-master/expression_prediction.py", line 165, in step5
subprocess.call("Rscript",INFILE1,"--slave","--args",filenames["housekeeping_profile"]+" "+filenames["unexpressed_profile"]+" "+filenames["profile_plot"],stderr=ERR_LOG,shell=True)
File "/mnt/work1/software/centos7/python/2.7.15/lib/python2.7/subprocess.py", line 172, in call
return Popen(*popenargs, **kwargs).wait()
File "/mnt/work1/software/centos7/python/2.7.15/lib/python2.7/subprocess.py", line 343, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
And the other error on there being multiple values:
Traceback (most recent call last):
File "/mnt/work1/users/pughlab/projects/IEG_MiSEQ/Inferring_DNA_Expression/ExpressionPrediction-master/expression_prediction.py", line 272, in <module>
step5(ERR_LOG,OUT_LOG,args,proj_dir,script_dir,filenames)
File "/mnt/work1/users/pughlab/projects/IEG_MiSEQ/Inferring_DNA_Expression/ExpressionPrediction-master/expression_prediction.py", line 165, in step5
subprocess.call("Rscript","--slave","--args",filenames["housekeeping_profile"]+" "+filenames["unexpressed_profile"]+" "+filenames["profile_plot"],stdin=INFILE1,stderr=ERR_LOG,stdout=OUT_LOG,shell=True)
File "/mnt/work1/software/centos7/python/2.7.15/lib/python2.7/subprocess.py", line 172, in call
return Popen(*popenargs, **kwargs).wait()
TypeError: __init__() got multiple values for keyword argument 'stdin'
Thank you
The main problem is that call takes a list of arguments or a single string to be parsed by the shell. You don't appear to need shell=True here; just create a single list of all the positional arguments you attempting to pass.
with open(script_dir+"/Scripts/plot_TSS_profile.R","r") as INFILE1:
cmd = [
"Rscript",
"--slave",
"--args",
filenames["housekeeping_profile"],
filenames["unexpressed_profile"],
filenames["profile_plot"]
]
subprocess.call(cmd, stdin=INFILE1, stderr=ERR_LOG, stdout=OUT_LOG)
Passing multiple positional arguments means that parameters that were meant to be set via keyword arguments (like stdin, etc) are being assigned values that were meant as part of the command to execute.

TypeError: bufsize must be an integer?

I am making a small program where I can open a file from any part of the computer with it's default editor. This is my code:
from os import *
import subprocess
print("Welcome to my File Finder. Here you can search for a file and open it.")
file_name = str(input("Your file's name:"))
print(subprocess.call(["xdg-open"], file_name))]
But instead of opening, it return this error:
Traceback (most recent call last):
File "Important_tester_projects.py", line 6, in <module>
print(subprocess.call(["xdg-open"], file_name))
File "/usr/lib/python3.6/subprocess.py", line 267, in call
with Popen(*popenargs, **kwargs) as p:
File "/usr/lib/python3.6/subprocess.py", line 609, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
I have googled to find a solution for this error, but I can't find any that seems to solve my Problem. How can fix my error?
NOTE: My Linux OS uses XFCE, not Gnome.
Instead, use subprocess.check_output(). Since your command has multiple words, parse your command with split() method from shlex lib.
Something like this:
import subprocess
import shlex
cmd=shlex.split('[find][2] root_dir -name file_name')
print subprocess.check_output(cmd)

subprocess, invoke C-program from within Python

I am trying to invoke a C-program, named “drule.c”, from within my Python-program “drulewrapper.py”. I am trying to use "subprocess" but cannot get it to work.
1) I compile “drule.c” on the Mac’s terminal and all works okay:
$ gcc -o drule drule c
$ ./drule D11
>P>Q>RQ
Fyi, the input -- “D11” -- are axioms in predicate logic; the output -- “>P>Q>RQ” -- is the theorem that is proven and which I then want to process further in my Python program.
2) I write a short Python program (drulewrapper.py) and compile it:
From subprocess import call
def CheckString():
call(“./drule”, “D11”)
3) But when I run CheckString() I get errors:
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
CheckString()
File "/Users/georgeszpiro/Dropbox/metamath/GApl/drulewrapper.py", line 3, in CheckString
call("./drule","D11")
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 267, in call
with Popen(*popenargs, **kwargs) as p:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 609, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integerT
Can anybody help?

Python subprocess call throws error when writing file

I'd like to use SVOX/pico2wave to write a wav-file from Python code. When I execute this line from a terminal the file is written just fine:
/usr/bin/pico2wave -w=/tmp/tmp_say.wav "Hello world."
I've verified that pico2wave is located in /usr/bin.
This is my Python code:
from subprocess import call
call('/usr/bin/pico2wave -w=/tmp/tmp_say.wav "Hello world."')
... which throws this error:
Traceback (most recent call last):
File "app/app.py", line 63, in <module>
call('/usr/bin/pico2wave -w=/tmp/tmp_say.wav "Hello world."')
File "/usr/lib/python2.7/subprocess.py", line 168, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 390, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1024, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
From the documentation
Providing a sequence of arguments is generally preferred, as it allows
the module to take care of any required escaping and quoting of
arguments (e.g. to permit spaces in file names). If passing a single
string, either shell must be True (see below) or else the string must
simply name the program to be executed without specifying any
arguments.
So you might try with
call(['/usr/bin/pico2wave', '-w=/tmp/tmp_say.wav', '"Hello world."'])

Categories