I am trying to write a script for termux. And i need to use psutil with python. And it's working fine. But every time i've tried to run the script it's giving me some errors, which not effects my code.
I've tried to use "try", "except" to catch the error. But it doesn’t work.
Note: See the last line on the error message. Script working fine. If you have other modules or solution to provide, remember 'I can't use os.kill on my script'.
Code:
try:
import psutil, os, signal
except Exception as e:
pass
print ("killing python")
proc = psutil.Process(os.getpid())
proc.send_signal(signal.SIGTERM)
Error + output :
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 110, in wrapper
return cache[key]
KeyError: (('/proc',), frozenset())
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 202, in <module>
scputimes = set_scputimes_ntuple("/proc")
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 112, in wrapper
ret = cache[key] = fun(*args, **kwargs)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 184, in set_scputimes_ntuple
with open_binary('%s/stat' % procfs_path) as f:
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 138, in open_binary
return open(fname, "rb", **kwargs)
PermissionError: [Errno 13] Permission denied: '/proc/stat'
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 110, in wrapper
return cache[key]
KeyError: (('/proc',), frozenset())
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/__init__.py", line 1435, in <module>
_last_cpu_times = cpu_times()
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/__init__.py", line 1429, in cpu_times
return _psplatform.cpu_times()
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 301, in cpu_times
set_scputimes_ntuple(procfs_path)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 112, in wrapper
ret = cache[key] = fun(*args, **kwargs)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 184, in set_scputimes_ntuple
with open_binary('%s/stat' % procfs_path) as f:
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 138, in open_binary
return open(fname, "rb", **kwargs)
PermissionError: [Errno 13] Permission denied: '/proc/stat'
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 110, in wrapper
return cache[key]
KeyError: (('/proc',), frozenset())
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/__init__.py", line 1442, in <module>
_last_per_cpu_times = cpu_times(percpu=True)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/__init__.py", line 1431, in cpu_times
return _psplatform.per_cpu_times()
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 314, in per_cpu_times
set_scputimes_ntuple(procfs_path)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_common.py", line 112, in wrapper
ret = cache[key] = fun(*args, **kwargs)
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 184, in set_scputimes_ntuple
with open_binary('%s/stat' % procfs_path) as f:
File "/data/data/com.termux/files/usr/lib/python3.8/site-packages/psutil/_pslinux.py", line 138, in open_binary
return open(fname, "rb", **kwargs)
PermissionError: [Errno 13] Permission denied: '/proc/stat'
killing python
Terminated
If i want to ignore the error messages. I can use following code, because it doesn’t interfere with my actual program.
import os, signal, sys
# set stderr to dev/null
sys.stderr = open(os.devnull, "w")
import psutil
# after importing, set stderr to original
sys.stderr = sys.__stderr__
print ("killing python")
proc = psutil.Process(os.getpid())
proc.send_signal(signal.SIGTERM)
Output :
killing python
Terminated
It can also be handled in a proper way as @TheoRet mentioned:
import os,sys
sys.stderr = open(os.devnull, "w")
try:
import psutil
#except:
#handle module not found
finally:
sys.stderr = sys.__stderr__
This way, all exceptions which actually prevent the "import psutil" line from execution will get shown. just in case there is a real problem with psutil (i.e. dependency module missing).