How to fix psutil import error "permission denied" on linux? - android

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

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 (i.e. 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__
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).

Related

CommandFailedError when running systrace on Android 6.0 device

I want to capture a system trace, using the systrace tool.
I have this device (not-rooted) :
Pixi 4.5 Alcatel
Android 6.0 (API 23)
And I use Android Studio 4.2.2 and I have installed Python 2.7.18, as required (the version 3+ does not match).
Here is the command line to view the list of available categories on my device :
C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace>C:\Users\myUser\AppData\Local\Programs\Python27\python systrace.py -l
I chose this command with the -l option for my here question because it results (see below) in the expected list plus an extra set of lines which suggests that the error CommandFailedError is related to an exception on HasRoot :
gfx - Graphics
input - Input
view - View System
webview - WebView
wm - Window Manager
am - Activity Manager
sm - Sync Manager
audio - Audio
video - Video
camera - Camera
hal - Hardware Modules
app - Application
res - Resource Loading
dalvik - Dalvik VM
rs - RenderScript
hwui - HWUI
perf - Performance
bionic - Bionic C Library
power - Power Management
sched - CPU Scheduling
freq - CPU Frequency
idle - CPU Idle
load - CPU Load
CRITICAL:root:(TimeoutThread-1-for-MainThread) Exception on HasRoot(myDeviceId, retries=3, timeout=30), attempt 1 of 4: CommandFailedError("(device: myDeviceId) one line of output was expected, but got: ['uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcar
d_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0', '']",)
CRITICAL:root:(TimeoutThread-2-for-MainThread) Exception on HasRoot(myDeviceId, retries=3, timeout=30), attempt 2 of 4: CommandFailedError("(device: myDeviceId) one line of output was expected, but got: ['uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcar
d_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0', '']",)
CRITICAL:root:(TimeoutThread-3-for-MainThread) Exception on HasRoot(myDeviceId, retries=3, timeout=30), attempt 3 of 4: CommandFailedError("(device: myDeviceId) one line of output was expected, but got: ['uid=2000(shell) gid=2000(shell) groups=2000(shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcar
d_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0', '']",)
Traceback (most recent call last):
File "systrace.py", line 49, in <module>
sys.exit(run_systrace.main())
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\run_systrace.py", line 205, in main
main_impl(sys.argv)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\run_systrace.py", line 177, in main_impl
atrace_agent.list_categories(options)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\tracing_agents\atrace_agent.py", line 65, in list_categories
if not devutils.HasRoot():
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 63, in timeout_r
etry_wrapper
impl, timeout, retries, desc=desc, retry_if_func=retry_if_func)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\timeout_retry.py", line 164, in Run
error_log_func=error_log_func)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py", line 199, in JoinA
ll
self._JoinAll(watcher, timeout)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py", line 171, in _Join
All
thread.ReraiseIfException()
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py", line 94, in run
self._ret = self._func(*self._args, **self._kwargs)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\timeout_retry.py", line 156, in <lambda
>
lambda: func(*args, **kwargs), name=thread_name)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 51, in impl
return f(*args, **kwargs)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\device_utils.py", line 562, in HasRoo
t
output = self.RunShellCommand(['id'], single_line=True)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 56, in timeout_r
etry_wrapper
return impl()
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 51, in impl
return f(*args, **kwargs)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\device_utils.py", line 1556, in RunSh
ellCommand
raise device_errors.CommandFailedError(msg % output, str(self))
devil.android.device_errors.CommandFailedError: (device: myDeviceId) one line of output was expected, but got: ['uid=2000(shell) gid=2000(shell) groups=2000(
shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0', '']
By comparison, here is the error CommandFailedError obtained with a command which should generate an html report :
C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace>C:\Users\myUser\AppData\Local\Programs\Python27\python systrace.py
Traceback (most recent call last):
File "systrace.py", line 49, in <module>
sys.exit(run_systrace.main())
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\run_systrace.py", line 205, in main
main_impl(sys.argv)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\run_systrace.py", line 185, in main_impl
controller.StartTracing()
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\systrace_runner.py", line 46, in StartTracing
self._tracing_controller.StartTracing()
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\tracing_controller.py", line 156, in StartTracing
timeout=self._controller_config.timeout):
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\common\py_utils\py_utils\__init__.py", line 103, in RunWithTimeout
return timeout_retry.Run(func, timeout, 0, args=args)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\timeout_retry.py", line 164, in Run
error_log_func=error_log_func)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py", line 199, in JoinA
ll
self._JoinAll(watcher, timeout)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py", line 171, in _Join
All
thread.ReraiseIfException()
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\reraiser_thread.py", line 94, in run
self._ret = self._func(*self._args, **self._kwargs)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\utils\timeout_retry.py", line 156, in <lambda
>
lambda: func(*args, **kwargs), name=thread_name)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\tracing_agents\android_cgroup_agent.py", line 59, in StartAgent
Tracing
if not self._device_utils.HasRoot():
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 56, in timeout_r
etry_wrapper
return impl()
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 51, in impl
return f(*args, **kwargs)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\device_utils.py", line 562, in HasRoo
t
output = self.RunShellCommand(['id'], single_line=True)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 56, in timeout_r
etry_wrapper
return impl()
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\decorators.py", line 51, in impl
return f(*args, **kwargs)
File "C:\Users\myUser\AppData\Local\Android\Sdk\platform-tools\systrace\catapult\systrace\systrace\..\..\devil\devil\android\device_utils.py", line 1556, in RunSh
ellCommand
raise device_errors.CommandFailedError(msg % output, str(self))
devil.android.device_errors.CommandFailedError: (device: deviceId) one line of output was expected, but got: ['uid=2000(shell) gid=2000(shell) groups=2000(
shell),1004(input),1007(log),1011(adb),1015(sdcard_rw),1028(sdcard_r),3001(net_bt_admin),3002(net_bt),3003(inet),3006(net_bw_stats) context=u:r:shell:s0', '']
I am sure that there is a proper way to run systrace on an old unrooted device but I just didn't find it.
Does anyone have a clue or a solution? Thanks.
EDIT :
Thanks a lot for your analysis and your answer #yi-yang.
As you recommended, I set single_line to False in the RunShellCommand call. Furthermore, I also modified the return line, which gives the final code in the device_utils.py file, lines 562-563 :
output = self.RunShellCommand(['id'], single_line=False)
return output[0].startswith('uid=0(root)')
Note that I select the first line because there are still two lines, which means that some optional newline at the end needs to be stripped somehow...
Now it works but only for a few categories, as in the following example:
systrace.py -o mynewtrace.html perf idle load
However, for any atrace data, including the very helpful "sched" (see atrace_agent.py), the cpu profiling not possible, as shown by the following messages:
Warning: Only 2 of 3 tracing agents started.
IOError('Unable to get atrace data. Did you forget adb root?')
So the root privileges do matter, despite what the file comments (l. 556-558) claim :-(.
I guess rooting is a must for the full experience!

subprocess error on qpython (OSerror)

I'm trying to send command through socket with subprocess (python 2.7 (qpython android)).
here 's the code :
#reponse is my command
x = subprocess.Popen(reponse,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
stdin=subprocess.PIPE)
y = str(x.stdout.read() + x.stderr.read())
But when i'm trying, i've got this error :
Traceback (most recent call last):
File "ser.py", line 72, in <module>
stdin=subprocess.PIPE)
File "/QPython/QPython2-core/build/python-install/lib/python2.7/subprocess.py",
line 679, in __init__
File "/QPython/QPython2-core/build/python-install/lib/python2.7/subprocess.py",
line 1228, in _execute_child
OSError: [Errno 2] No such file or directory
So, can someone tell me what's wrong, and how to fixe this .. Thx in advance for help...
I found that commands don't work if you specify shell=True.
I had the same error.
You should use the commands and its arguments as a list; i.e.:
your_cmd = ['examplecmd', '-a', 'foo', '-b', 'bar']

infer -- gradle build is not working

I'm trying to use the Infer tool to analyze my app code. I followed these steps and every time I'm trying to run infer -- gradle build I'm getting the below errors :
infer -- gradle build
Running and capturing gradle compilation...
Traceback (most recent call last):
File "/usr/local/bin/infer", line 183, in <module>
main()
File "/usr/local/bin/infer", line 147, in main
capture_exitcode = imported_module.gen_instance(args,cmd).capture()
File "/usr/local/Cellar/infer/0.8.1/libexec/infer/lib/python/inferlib/capture/gradle.py", line 87, in capture
cmds = self.get_infer_commands(util.get_build_output(self.build_cmd))
File "/usr/local/Cellar/infer/0.8.1/libexec/infer/lib/python/inferlib/capture/util.py", line 25, in get_build_output
proc = subprocess.Popen(build_cmd, stdout=subprocess.PIPE)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
Any idea would be appreciated !!
I found that Infer is already included in the new android studio 2.2 where you can run it directly from Analyze->Infer Nullity then specify the scope.

android python errno 10053

I am trying to learn creating the android application using python. I am going through the tutorials.
when I am running the command :
adb forward tcp:9999 tcp:4321
set AP_PORT=9999
and then running the code,
import android
droid = android.Android()
droid.makeToast("Hello from my computer!")
It gives me error as :
Traceback (most recent call last):
File "<pyshell#5>", line 1, in <module>
droid.makeToast("Hello from my computer!")
File "C:\Python27\lib\site-packages\android.py", line 58, in rpc_call
return self._rpc(name, *args)
File "C:\Python27\lib\site-packages\android.py", line 47, in _rpc
response = self.client.readline()
File "C:\Python27\lib\socket.py", line 447, in readline
data = self._sock.recv(self._rbufsize)
error: [Errno 10053] An established connection was aborted by the software in your host machine
how can I solve the problem?

Getting SL4A (ASE) to GNU/Linux box

Without emulator, is there a way to execute the scripts. as any other normal py code?
Tried to port, but /me gets :
droid = android.Android()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/android.py", line 34, in __init__
self.conn = socket.create_connection(addr)
File "/usr/lib/python2.6/socket.py", line 547, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
Yes, you can use a remote connection to your phone and execute the script local, see RemoteControl in the SL4A Wiki.

Categories

Resources