简体   繁体   中英

How to fix psutil import error “permission denied” on linux?

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.

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

Today I had the very same issue when importing psutil in termux on android. The solution / workaround that Mr. lindroid found is helping to avoid that annoying error message. Thanks for sharing!

I want to propose a small improvement:

import os,sys

sys.stderr = open(os.devnull, "w")
try:
  import psutil
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 (ie dependency module missing).

Do you know why the error posted originally cannot be caught by a normal try/except statement? Is it raised in a second thread maybe? I already tried manipulating the Thread class to handle the error but it didn't work. What else could be a reason?

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__

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM