os.getlogin() Error

20/20 Lab lab at pacbell.net
Fri May 5 14:30:41 EDT 2017


I'm not sure if this will help you, but I found some stuff on accident 
looking at something related.

Not sure if it will help, but looked promising

https://github.com/parmentelat/apssh/issues/1

==Some snippets from the page

 From the os.getlogin() docs: "Returns the user logged in to the 
controlling terminal of the process." Your script does not have a 
controlling terminal when run from cron. The docs go on to suggest: "For 
most purposes, it is more useful to use the environment variable LOGNAME 
to find out who the user is, or pwd.getpwuid(os.getuid())[0] to get the 
login name of the currently effective user id."


I suggest you to replace os.getlogin with:

import pwd
import os

getlogin = lambda: pwd.getpwuid(os.getuid())[0]
default_username = getlogin()

==


On 05/04/2017 01:03 PM, Wildman via Python-list wrote:
> I wrote a Linux only GUI program using Tk that reports various system
> information using a tabbed Notebook.  I have tested the program on
> Debian, SoldyX and MX-15 and the program runs perfectly.
>
> I tried testing on Mint and Ubuntu and the program would crash.  The
> GUI would appear briefly and disappear.  On Ubuntu a crash report was
> created so I was able to figure out what was going on.  It had the
> traceback and showed that os.getlogin threw an error.  This is from
> the crash report:
>
> PythonArgs: ['/opt/linfo-tk/linfo-tk.py']
> Traceback:
>   Traceback (most recent call last):
>     File "/opt/linfo-tk/linfo-tk.py", line 1685, in <module>
>       app = Window(root)
>     File "/opt/linfo-tk/linfo-tk.py", line 1393, in __init__
>       txt = function()
>     File "/opt/linfo-tk/linfo-tk.py", line 316, in userinfo
>       user = os.getlogin()
>   OSError: [Errno 25] Inappropriate ioctl for device
>
> The program installs using the Debian package system (.deb) and an
> entry is created in the Applications Menu.  The strange thing is
> that the crash only occurs when the program is run from the menu.
> If I open a terminal and run the program from there, the program
> runs fine.
>
> I found a little info on the web about this but it was not clear
> whether it is a bug in Linux or a bug in the os module.  I also
> found a couple of work-arounds but neither of them will work for
> my purposes.
>
>      user = pwd.getpwuid(os.getuid())[0]
>      user = getpass.getuser()
>
> I will try to explain...
> The program reports system information based on the user's name.
> Things such as passwd, groups and shadow info.  However, the
> program must have elevated privileges to get the shadow info so
> the program has the option to 'restart as root' so the shadow
> information will be obtainable.
>
> If the program is restarting as root, the work-arounds report
> the user as 'root'.  Then the system information for passwd,
> groups and shadow will be reported for 'root' and not the
> user that ran the program.  The actual user name that ran
> the program is needed for the program to report correct
> information.
>
> It seems that only os.getlogin() reports the true user name no
> matter if the program is run as a normal user or restarted as
> root.
>
> Is there a way to get the actual user name or is there a fix
> or a better work-around for the os.getlogin() function?
>




More information about the Python-list mailing list