Avoid treating `psutil` as a hard requirement on Termux · Issue #15422 · ipython/ipython · GitHub
Skip to content

Avoid treating psutil as a hard requirement on Termux #15422

Open

Description

@1MLightyears

TL;DR: Avoid treating psutil as a hard requirement on Android/Termux platform(sys_platform=="android").


psutil is now only used in kitty graphic protocol deciding whether to show png images in terminal or not:

→grep -rn "psutil" **/*.py
IPython/core/kitty.py:12:#: the process tree and is the reason IPython imports psutil at startup.
IPython/core/kitty.py:46:    outcome as the `psutil.AccessDenied` the psutil walk below has to handle.
IPython/core/kitty.py:48:    The kernel truncates the name in ``stat`` to 15 characters, where psutil
IPython/core/kitty.py:72:def _psutil_ancestor_names() -> Iterator[str]:
IPython/core/kitty.py:73:    """Yield ancestor process names, nearest first, using psutil."""
IPython/core/kitty.py:74:    import psutil
IPython/core/kitty.py:77:        process = psutil.Process()
IPython/core/kitty.py:80:    except (psutil.Error, OSError):
IPython/core/kitty.py:83:        # ancestor processes owned by other users are inaccessible and psutil
IPython/core/kitty.py:92:    On Linux this reads ``/proc`` directly: importing psutil costs upwards of
IPython/core/kitty.py:94:    interactive start. ``/proc/<pid>/stat`` holds both the name psutil would
IPython/core/kitty.py:98:    psutil, which IPython depends on anyway.
IPython/core/kitty.py:103:        yield from _psutil_ancestor_names()
tests/test_kitty.py:59:def test_supports_kitty_graphics_handles_psutil_access_denied(monkeypatch):
tests/test_kitty.py:64:    another user makes psutil raise AccessDenied. This must be treated as
tests/test_kitty.py:67:    import psutil
tests/test_kitty.py:71:    # "darwin" so that detection takes the psutil branch rather than /proc.
tests/test_kitty.py:77:            raise psutil.AccessDenied(pid=1)
tests/test_kitty.py:80:            raise psutil.AccessDenied(pid=1)
tests/test_kitty.py:82:    monkeypatch.setattr(psutil, "Process", lambda *args, **kwargs: DeniedProcess())
tests/test_kitty.py:89:    import psutil
tests/test_kitty.py:98:    monkeypatch.setattr(psutil, "Process", fail)
tests/test_kitty.py:106:    import psutil
tests/test_kitty.py:115:    monkeypatch.setattr(psutil, "Process", fail)
tests/test_kitty.py:226:    reason="the psutil-free walk is the /proc one, and needs a real /proc",
tests/test_kitty.py:228:def test_supports_kitty_graphics_does_not_import_psutil_on_linux():
tests/test_kitty.py:229:    """The /proc walk exists to keep psutil -- and its import cost -- out.
tests/test_kitty.py:232:    is that a real Linux with a real ``/proc`` answers without psutil.
tests/test_kitty.py:239:def no_psutil(name, *args, **kwargs):
tests/test_kitty.py:240:    assert name != "psutil", "psutil must not be imported by kitty detection"
tests/test_kitty.py:243:builtins.__import__ = no_psutil
tests/test_kitty.py:261:assert "psutil" not in sys.modules

This is a handy function, but it has a hard requirement psutil, which is not widely supported on multiple platforms. Actually, IPython itself runs on more platforms than psutil, as Cygwin users have reported similar issues including this and this.

Android/Termux is now found as an unsupported platform as well. Termux is a famous open-source Linux simulator on Android platforms and has a large group of users.

On Termux, there is actually a workaround for this issue-the python-psutil package can be found on the official Termux package manager pkg. Though, python-psutil is a system-wide package, meaning it does not also build correct dependency for virtual environments, e.g. those created by venv and uv.

I suggest that the psutil should be an optional dependency on Android/Termux platform.

If agreed, I can start drafting a PR for this.

Activity

  1. changed the title [-]Avoid treating `psutil` hard requirement on Termux[/-] [+]Avoid treating `psutil` as a hard requirement on Termux[/+] on Oct 7, 2026
  2. kaanisthatyou commented on Oct 7, 2026

    @kaanisthatyou
    Contributor

    @1MLightyears are you planning to send a PR for this? If not, I can open one that adds sys_platform != "android" to the psutil marker, the same way #15247 did for Cygwin (on Android _supports_kitty_graphics() returns before psutil is imported).

  3. 1MLightyears commented on Oct 7, 2026

    @1MLightyears
    Author

    Thank you @kaanisthatyou . I've been reading kitty.py and found that it wasn't that simple; the logic is a bit problematic. Simply adding a clause in pyproject.toml is trivial and efficientless.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions