python-for-android icon indicating copy to clipboard operation
python-for-android copied to clipboard

can't run python4andorid in debugger in vscode because shprint does not seem to work well in debugpy

Open guysoft opened this issue 7 months ago • 1 comments

The debgger used in vscode, debugpy, seems to not be able to run bulldozer because of code in python-for-android. When running a build sh.basename is called in the python3 recipe. The code is a more complex version of this code:

from pythonforandroid.logger import shprint
import sh

print(shprint(sh.basename, '/a/Python-3.11.5.tgz'))

When running without the debugger this works fine, but with the debugger I get:

 cd /home/guy/workspace/hand_tracker ; /usr/bin/env /home/guy/vpy/bin/python /home/guy/.vscode-oss/extensions/ms-python.debugpy-2025.6.0-linux-x64/bundled/libs/debugpy/adapter/../../debugpy/launcher 59539 -- /tmp/reproduce.py 
[INFO]:    -> running basename /a/Python-3.11.5.tgz
Traceback (most recent call last):                                                                                      
  File "/home/guy/.vscode-oss/extensions/ms-python.debugpy-2025.6.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 422, in _on_run
    cmd.send(self.sock)
  File "/home/guy/.vscode-oss/extensions/ms-python.debugpy-2025.6.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_net_command.py", line 111, in send
    sock.sendall(("Content-Length: %s\r\n\r\n" % len(as_bytes)).encode("ascii"))
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/guy/.vscode-oss/extensions/ms-python.debugpy-2025.6.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 53, in run
    self._on_run()
  File "/home/guy/.vscode-oss/extensions/ms-python.debugpy-2025.6.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 432, in _on_run
    self.py_db.dispose_and_kill_all_pydevd_threads()
  File "/home/guy/.vscode-oss/extensions/ms-python.debugpy-2025.6.0-linux-x64/bundled/libs/debugpy/_vendored/pydevd/pydevd.py", line 2558, in dispose_and_kill_all_pydevd_threads
    self._client_socket.close()
  File "/usr/lib/python3.11/socket.py", line 503, in close
    self._real_close()
  File "/usr/lib/python3.11/socket.py", line 497, in _real_close
    _ss.close(self)
OSError: [Errno 9] Bad file descriptor
6.71s - pydevd: Sending message related to process being replaced timed-out after 5 seconds
Python-3.11.5.tgz

Plain sh works fine.

guysoft avatar Jul 14 '25 21:07 guysoft

I found another way to debug this. You can also do it remotely.

Steps to reproduce.

  1. Run this script on your system:
from pythonforandroid.logger import shprint
import debugpy

# Add this at the beginning of your main.py, but after any Kivy imports
def enable_remote_debugging():
    try:
        debugpy.listen(("0.0.0.0", 5678))
        print("Waiting for debugger to attach at 0.0.0.0:5678...")
        debugpy.wait_for_client()
        print("Debugger attached!")
    except Exception as e:
        print(f"Debugpy error: {e}")

# Call this function early in your app initialization
enable_remote_debugging()

import sh

print(shprint(sh.basename, '/a/Python-3.11.5.tgz'))

  1. On vscode open a debug session with these launch settings:
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Remote Attach",
            "type": "debugpy",
            "request": "attach",
            "connect": {
                "host": "localhost",
                "port": 5678
            },
            "pathMappings": [
                {
                    "localRoot": "${workspaceFolder}",
                    "remoteRoot": "/app"  // This might vary based on your p4a/buildozer setup
                }
            ],
            "justMyCode": false
        },

    ]
}

The stdout of the python script above is:

python3 /tmp/reproduce.py 
0.00s - Debugger warning: It seems that frozen modules are being used, which may
0.00s - make the debugger miss breakpoints. Please pass -Xfrozen_modules=off
0.00s - to python to disable frozen modules.
0.00s - Note: Debugging will proceed. Set PYDEVD_DISABLE_FILE_VALIDATION=1 to disable this validation.
Waiting for debugger to attach at 0.0.0.0:5678...
Debugger attached!
[INFO]:    -> running basename /a/Python-3.11.5.tgz


Traceback (most recent call last):                                                                                                                                                         
  File "/home/guy/vpy/lib/python3.11/site-packages/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 422, in _on_run
    cmd.send(self.sock)
  File "/home/guy/vpy/lib/python3.11/site-packages/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_net_command.py", line 111, in send
    sock.sendall(("Content-Length: %s\r\n\r\n" % len(as_bytes)).encode("ascii"))
OSError: [Errno 9] Bad file descriptor

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/guy/vpy/lib/python3.11/site-packages/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_daemon_thread.py", line 53, in run
    self._on_run()
  File "/home/guy/vpy/lib/python3.11/site-packages/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 432, in _on_run
    self.py_db.dispose_and_kill_all_pydevd_threads()
  File "/home/guy/vpy/lib/python3.11/site-packages/debugpy/_vendored/pydevd/pydevd.py", line 2558, in dispose_and_kill_all_pydevd_threads
    self._client_socket.close()
  File "/usr/lib/python3.11/socket.py", line 503, in close
    self._real_close()
  File "/usr/lib/python3.11/socket.py", line 497, in _real_close
    _ss.close(self)
OSError: [Errno 9] Bad file descriptor
22.59s - pydevd: Sending message related to process being replaced timed-out after 5 seconds
Python-3.11.5.tgz

It looks like some failure due to debugpy. But it only happends if there is these lines:

import sh

print(shprint(sh.basename, '/a/Python-3.11.5.tgz'))

And they appear in buildozer

guysoft avatar Jul 23 '25 20:07 guysoft