__init__() got an unexpected keyword argument 'timeout' in python subprocess - python

I am trying to run following code which uses pythos 'subprocess' module.
subprocess.call(cli_args, stdout=client_log, stderr=client_log, timeout=10)
I am using timeout arguement, which is mentioned here to skip from this line if the subprocess stuck at the middle, by killing it.
But when I run this, I am getting following error.
Traceback (most recent call last):
File "test.py", line 152, in <module>
ret = runServiceTest(test_name, server_executable, server_extra_args, client_executable, client_extra_args, protocol, transport, 9090, 0, 0)
File "test.py", line 102, in runServiceTest
ret = subprocess.call(cli_args, stdout=client_log, stderr=client_log, timeout=10)
File "/usr/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
TypeError: __init__() got an unexpected keyword argument 'timeout'
What is the reason for this? How can I solve this? My complete code can be found here.

Judging by your print statements, you are using Python2.x, where subprocess.call does not have a timeout argument:
subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
https://docs.python.org/2/library/subprocess.html

Related

pass selenium webdriver as argoument for python program

i want to run a python program first.py by another program and pass the webdriver
element as an argoument using subprocess doing so:
driver = webdriver.Firefox(executable_path="geckodriver.exe")
process = subprocess.Popen(["python.exe", 'first.py', driver], shell=False)
but python refuses to pass a webdriver element as an argoument
and gives this error:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\bo\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:/Users/bo/Desktop/instagrambot/run_bot.py", line 23, in <lambda>
run_button = tk.Button(root , text="Run", command=lambda: Run())
File "C:/Users/bo/Desktop/instagrambot/run_bot.py", line 12, in Run
close_fds=True)
File "C:\Users\bo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 775, in __init__
restore_signals, start_new_session)
File "C:\Users\bo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 1119, in _execute_child
args = list2cmdline(args)
File "C:\Users\bo\AppData\Local\Programs\Python\Python37-32\lib\subprocess.py", line 530, in list2cmdline
needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: argument of type 'WebDriver' is not iterable
I'm working on windows 10 and python 3.7
The arguments to the subprocess.Popen call are supposed to be strings whereas you are passing a Python object. This is why you are getting an iteration error - Python attempts to parse the string arguments (that are separated either with a space or a tab) and fails when he gets a Python object.
You have to unfortunately to rethink your strategy. You could construct the driver within the first.py module or have a separate Python module which you could import to get a driver set up when executing the first.py module.

Python3.5 subprocess error

I am using below function to read my python scripts output line by line and save in parallel. But getting Traceback error in end.
Code:
def myrun(cmd):
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = []
while True:
line = p.stdout.readline()
stdout.append(line)
print (line),
if line == '' and p.poll() != None:
break
return ''.join(stdout)
When call function as :
myrun(os.system("./tests_run.py"))
I get below error:
Error:
Traceback (most recent call last):
File "./Portability_Tests.py", line 38, in <module>
myrun(os.system("./tests_run.py"))
File "./Tests.py", line 11, in myrun
p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
File "/usr/local/lib/python3.5/subprocess.py", line 676, in __init__
restore_signals, start_new_session)
File "/usr/local/lib/python3.5/subprocess.py", line 1171, in _execute_child
args = list(args)
TypeError: 'int' object is not iterable
Anyone know how can i fix this error ?
The subprocess.Popen function receives a "sequence of program arguments or else a single string" as its args argument.
What you are passing in the args argument is the output of the os.system() call which according to the documentation is the "exit status of the process", thus an int number. Instead in the cmd variable you should directly pass the string (or an other iterator) of your file /tests_run.py. If you want this path to be relative to your current project you can use the os.path module.

how to get rid of the duplicate timeout exception message from Python's subprocess?

This test code
import subprocess
out=subprocess.run(["sleep","10"], timeout=1)
generates under Python 3.5.3 the following output:
Traceback (most recent call last):
File "/usr/lib/python3.5/subprocess.py", line 385, in run
stdout, stderr = process.communicate(input, timeout=timeout)
File "/usr/lib/python3.5/subprocess.py", line 801, in communicate
stdout, stderr = self._communicate(input, endtime, timeout)
File "/usr/lib/python3.5/subprocess.py", line 1476, in _communicate
self.wait(timeout=self._remaining_time(endtime))
File "/usr/lib/python3.5/subprocess.py", line 1385, in wait
raise TimeoutExpired(self.args, timeout)
subprocess.TimeoutExpired: Command '['sleep', '10']' timed out after 0.9999505979940295 seconds
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.5/subprocess.py", line 390, in run
stderr=stderr)
subprocess.TimeoutExpired: Command '['sleep', '10']' timed out after 1 seconds
This first of these two exception messages is inappropriate and confusing. It contains internals a normal user of the Python API does not want to see. It suggests there is something wrong within subprocess.py although it is expected regular behavior for subprocess to silently handle a timeout imposed by the application programmer.
Is there a way to get a cleaner exception message?
Should I raise this complaint on the Python issue tracker?

python problam subprocess fore rub script bash

i have problem in subprocess
i have this script bash which good work and is name kamel.sh
to=$1
subject=$1
/root/Desktop/telegram/tg/bin/./telegram-cli -k /root/Desktop/telegram/tg/tg-server.pub -WR -e "msg $to $subject"
but i want use python fore work.is 1.sh give 2 argv fore work and argv1 = user and argv2 = hello
but is have problem
import subprocess
subprocess.call("1.sh", user, hello, shell=True )
and i see this eroore
Traceback (most recent call last):
File "/root/Desktop/telegram-log/kamel.py", line 27, in <module>
subprocess.call("kamel.sh testt",kol,shell=True )
File "/usr/lib/python2.7/subprocess.py", line 523, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 660, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integer
import subprocess
subprocess.call("1.sh user hello", shell=True)
or
import subprocess
subprocess.call(["1.sh", "user", "hello"])

Can't rm -r directory using python subprocess.call

Welp, I need to remove some huge temporary directories from python and I can't seem to use rm -r. I'm working thought a big dataset (on s3) I don't have the disc space to leave them around.
The usual way I would call a command from python is
import subprocess
subprocess.call('rm','-r','/home/nathan/emptytest')
That gives me
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
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
What's this all about?
You're calling it the wrong way. The first argument should be a list:
import subprocess
subprocess.call(['rm','-r','/home/nathan/emptytest'])
You might also just want to try and use shutitl.rmtree
According to the documentation,
>> In [3]: subprocess.call?
Type: function
Base Class: <type 'function'>
String Form: <function call at 0x01AE79F0>
Namespace: Interactive
File: c:\python26\lib\subprocess.py
Definition: subprocess.call(*popenargs, **kwargs)
Docstring:
Run command with arguments. Wait for command to complete, then
return the returncode attribute.
The arguments are the same as for the Popen constructor. Example:
retcode = call(["ls", "-l"])
So try:
subprocess.call(['rm','-r','/home/nathan/emptytest'])

Categories

Resources