I'm trying to get the pid with the help of process name. i have tried this solution. But it gives me this error
Traceback (most recent call last):
File "pidName.py", line 10, in <module>
getPIDs("safari")
File "pidName.py", line 4, in getPIDs
pidlist = map(int, s.check_output(["pidof", process]).split())
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 567, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/local/Cellar/python/2.7.12_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
and the code is this :
import subprocess as s
def getPIDs(process):
try:
pidlist = map(int, s.check_output(["pidof", process]).split())
except s.CalledProcessError:
pidlist = []
print 'list of PIDs = ' + ', '.join(str(e) for e in pidlist)
if __name__ == '__main__':
getPIDs("safari")
Since, i'm new to these things. I'm unable to solve this problem. But in the solution it works. But somehow it doesn't on my Mac. I'm using mac. and running the python script from terminal.
which python gives me this result:-
/usr/local/bin/python
so, i'm using the inbuilt python version i think.
can anyone help me with this problem.
Related
I am trying to use a Python scrip that converts Latex to a png. The library is at https://github.com/cptdeadbones/pytex2png.
When I run the command python examples.py I get the following error:
Traceback (most recent call last):
File "./examples.py", line 14, in <module>
main()
File "./examples.py", line 11, in main
pytex2png.convert("examples/"+file,"output/"+file+".png")
File "/Users/kekearif/Desktop/pytex2png-master/pytex2png.py", line 44, in convert
make_transparent_bg(output,display)
File "/Users/kekearif/Desktop/pytex2png-master/pytex2png.py", line 31, in make_transparent_bg
exe_command(command_line)
File "/Users/kekearif/Desktop/pytex2png-master/pytex2png.py", line 11, in exe_command
p = subprocess.Popen(args,stdout=subprocess.PIPE)
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 390, in __init__
errread, errwrite)
File "/usr/local/Cellar/python/2.7.13/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1024, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory'
What does this error mean? Is it an error in how the arguments are being passed?
From the source:
def exe_command(cmd, display=False):
args = shlex.split(cmd)
if(display):
p = subprocess.Popen(args)
else:
p = subprocess.Popen(args,stdout=subprocess.PIPE)
p.wait()
Is there a mistake in the function above?
I hope you have followed the Usage as describe here : Usage which create Makefile.
Have you check the Disclaimers ?? It says the following :
This code was devloped and tested on a Linux based system. I have no
idea if it will work on another system. If you have tested it on
another system, please let me know.
I've search a while and still can not figure it out...
Here's part of my code that went wrong.
import subprocess as sp
import os
cmd_args = []
cmd_args.append('start ')
cmd_args.append('/wait ')
cmd_args.append(os.path.join(dirpath,filename))
print(cmd_args)
child = sp.Popen(cmd_args)
And the command prompt through out this.
['start ', '/wait ', 'C:\\Users\\xxx\\Desktop\\directory\\myexecutable.EXE']
Traceback (most recent call last):
File "InstallALL.py", line 89, in <module>
child = sp.Popen(cmd_args)
File "C:\Python34\lib\subprocess.py", line 859, in __init__
restore_signals, start_new_session)
File "C:\Python34\lib\subprocess.py", line 1114, in _execute_child startupinfo)
FileNotFoundError: [WinError 2]
It looks like the filepath is wrong with 2 backslashes.
I know if I do
print(os.path.join(dirpath,filename))
It'll return
C:\Users\xxx\Desktop\directory\myexecutable.EXE
I'm sure the file is there.
How can I debug this?
This is happening because Popen is trying to find the file start instead of the file you want to run.
For example, using notepad.exe:
>>> import subprocess
>>> subprocess.Popen(['C:\\Windows\\System32\\notepad.exe', '/A', 'randomfile.txt']) # '/A' is a command line option
<subprocess.Popen object at 0x03970810>
This works fine. But if I put the path at the end of the list:
>>> subprocess.Popen(['/A', 'randomfile.txt', 'C:\\Windows\\System32\\notepad.exe'])
Traceback (most recent call last):
File "<pyshell#53>", line 1, in <module>
subprocess.Popen(['/A', 'randomfile.txt', 'C:\\Windows\\System32\\notepad.exe'])
File "C:\python35\lib\subprocess.py", line 950, in __init__
restore_signals, start_new_session)
File "C:\python35\lib\subprocess.py", line 1220, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
Use this instead:
import subprocess as sp
import os
cmd_args = []
cmd_args.append(os.path.join(dirpath,filename))
cmd_args.append('start ')
cmd_args.append('/wait ')
print(cmd_args)
child = sp.Popen(cmd_args)
You might need to swap cmd_args.append('start ') and cmd_args.append('/wait ') around too depending on which order they are meant to be in.
I faced the same problem and just to add a note about Popen: As argument Popen takes a list of strings for non-shell calls and only a string for shell calls.
Details listed here: WinError 2 The system cannot find the file specified (Python)
So, I made a command called jel, which is executable as jel. It is run in Python, and when I run jel doctor, in jel.py it gives me a error(the main file). The code looks like this: Note that all necessary modules are already imported.
elif arg == 'doctor':
subprocess.call(['cd', 'js'])
ver = subprocess.call(['node', 'version.js'])
subprocess.call(['cd', '..'])
if not ver == version:
print 'jel doctor: \033[91found that version\033[0m ' + str(version) + ' \033[91mis not the current version\033[0m'
print 'jel doctor: \033[92mrun jel update\033[0m'
sys.exit()
The js file version.js is run on node, and looks like this: All necessary packages are installed
var latest = require('latest');
latest('jel', function(err, v) {
console.log(v);
// => "0.0.3"
if (err) {
console.log('An error occurred.');
}
});
It is giving me this error when the jel.py file uses subprocess to call cs js and node version.js:
Traceback (most recent call last):
File "/bin/jel", line 90, in <module>
subprocess.call(['cd', 'js'])
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
OSError: [Errno 2] No such file or directory
bjskistad:~/workspace (master) $ jel doctor
Traceback (most recent call last):
File "/bin/jel", line 90, in <module>
subprocess.call(['cd', 'js'])
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
OSError: [Errno 2] No such file or directory
I believe it is saying the directory doesn't exist, although it does. Do I need to call something else before?
There are at least three problems with your code snippet:
cd is a shell built-in, not an executable program. If you want to invoke cd, you'll need to invoke the shell.
The cd command only affects the shell in which it runs. It will have no effect upon the python program, or any subsequent subprocesses.
The return code from subprocess.call() is not the text that the program wrote to stdout. To get that text, try subprocess.check_output().
Try this:
#UNTESTED
elif arg == 'doctor':
ver = subprocess.check_output(['cd js && node version.js'], shell=True)
if not ver == version:
As already pointed out changing the directory is only reflected in the subprocess. You should use os.chdir to change your working directory but another alternative is to specify the cwd to subprocess which avoids any need to cd or os.chdir:
version = subprocess.check_output(['node', 'version.js'], cwd="js")
You should also use != in your if and you probably want to rstrip the newline:
if version != ver.rstrip():
I am trying to execute a program with python using subprocess
The format our professor gave us was subprocess(path/executableProgram)
File: OS377.py
I am doing it as subprocess(['/home/Joseph/OS377.py']) but I am getting errors
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python3.2/subprocess.py", line 471, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python3.2/subprocess.py", line 745, in __init__
restore_signals, start_new_session)
File "/usr/lib/python3.2/subprocess.py", line 1361, in _execute_child
raise child_exception_type(errno_num, err_msg)
OSError: [Errno 8] Exec format error
I need to execute a file using this format but am unsure how to go about it
Code:
def RUN(file):
pid = os.fork() #pid is non-zero in the parent process and 0 in the child
if pid: #parent
os.waitpid(pid,0)
elif pid == 0: #child
print("path is : ")
child(file)
def child(file):
#path = os.path.abspath(file)
#print(path)
subprocess.call(os.path.abspath('OS377.py'))
subprocess.call(['python', os.path.abspath('OS377.py')].
The file you're calling, a python script, may not be marked as executable (chmod u+x file.py). Alternatively, you should probably be executing it with $ python file.py - which is calling the python interpreter and passing it the name of your script as the first argument. So you should put it as subprocess.call(['python', '/home/Joe/file.py'].
Btw, did you mean subprocess.call() instead of just subprocess()?
I'm having issues with Python finding an available Executable on my Linux machine. My default PATH includes this Executable (svnlook) but when I run the python script the below function fails to find executable. Any ideas on how to fix this?
def command_output(cmd):
child = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
output = child.communicate()[0]
return output, child.returncode
def get_author():
cmd = "svnlook author %s %s %s" % (svn_opt, svn_txn, svn_repo)
author, return_code = command_output(cmd)
return author.strip()
Error:
Traceback (most recent call last):
File "/home/user/app/csvn/data/repositories/repo/hooks/pre-commit", line 82, in <module>
author = get_author()
File "/home/user/app/csvn/data/repositories/repo/hooks/pre-commit", line 53, in get_author
author, return_code = command_output(cmd)
File "/home/user/app/csvn/data/repositories/repo/hooks/pre-commit", line 36, in command_output
child = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE)
File "/home/user/app/activepython-2.7.2.5_x86_64/lib/python2.7/subprocess.py", line 679, in __init__
errread, errwrite)
File "/home/user/app/activepython-2.7.2.5_x86_64/lib/python2.7/subprocess.py", line 1228, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Error: [Errno 2] No such file or directory
You probably want to provide the full path to the executable, e.g. /usr/bin/svnlook or /usr/local/bin/svnlook instead of just svnlook.
See this answer to a related question for details.
Try running it from the console. Make sure the permissions/executability is correct. Try os.system().