0

I am trying to run the below code as part of a larger Python project to display information on an OLED screen using a startup service on a Raspberry Pi Linux set-up.

However, the username will not appear unless I run the script as sudo

can you please suggest a work around to allow this code to run correctly without sudo.

Thank you in advance!

import psutil

def main():
    user = psutil.users()[0].name
    print(user)
main()

with sudo

*username*

without sudo

[]
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • The code doesn't print anything, how are you getting that output? – Barmar Jun 08 '23 at 17:58
  • Thank you Barmar, Forgot the print line. oops – Matt Clarke Jun 08 '23 at 18:21
  • See https://github.com/giampaolo/psutil/blob/962cb9ad58ca3bc21b20130f435f8d507d931051/psutil/_psutil_linux.c#L365 -- psutil is calling `getutent()`, which is documented at https://man7.org/linux/man-pages/man3/getutent.3.html, and depends on access to `/var/run/utmp`; you thus need to care about that file's permissions. – Charles Duffy Jun 09 '23 at 15:29

0 Answers0