Python error on standard library: Traceback (most recent call last): File "./trilliji.py", line 25, in ? main() File "./trilliji.py", line 16, in main Connections = TriCon() File "./clsTriCon.py", line 16, in __init__ self.login_user=os.getlogin() OSError: [Errno 2] No such file or directory Worked in 1.1a
python -c 'import os;print os.getlogin()' works for me on (all tested versions) of python, and I tried several across several platforms. The os.getlogin() function is a thin wrapper around the C function getlogin(3). What you are likely seeing is a problem with utmp. Do this: strace -o output python2.2 -c 'import os;print os.getlogin()' and attach the file "output" (or just paste it in here).
The following was sent to me directly: bash-2.05a# strace -o output python2.2 -c 'import os;print os.getlogin()' Traceback (most recent call last): File "<string>", line 1, in ? OSError: [Errno 2] No such file or directory
Created attachment 2049 [details] output of strace -o
Well. SOme investigation has lead me to believe that your utmp file is corrupt, *and* that the error returned by Python/getlogin (not sure which) is also wrong, or at least, misleading. Thus, please two things for me: 1. move the utmp file out of the way, and 2. run 'ltrace -o foo python -c "import os;print os.getlogin()" and attach to this bug the foo file, please (content-type text/plain, *uncompressed* please), if it still fails. Thanks!
Ok, I upgraded my system to current, moved utmp to a different location, and created a new one with touch utmp. I still have the same problem. Attached ltrace as you suggested. The bug submission system will not allow me to attach the ltrace output. It says it is too large for a non-patch submission.
Did a fresh install from scratch of gentoo 1.2 this weekend. After install the results are the same as previously. The bug is repeatable here.
I have the same problem. It only occurs when I run python -c 'import os;print os.getlogin()' in gnome-terminal/multi-gnome-terminal/rxvt/Eterm. When I run it in an xterm or on the virtual console, it works perfectly. Out of curiosity, I tried calling getlogin() from a C program running in the gnome-terminal, etc. It returns a null pointer. There is an errno of 2 (No such file or directory) associated with the getlogin() call, just as the Python interpreter reports. That's a strange error... When executing in the virtual console or xterm, the C program returns the userid as it should. Maybe it's a terminal emulation issue. I wonder why it works in the xterm...I'm not sure if this is a bug or not. It may jsimply be a getlogin() limitation. Let me know if I can help. I'm a wannabe gentoo developer, so I could use the experience. Workaround: This works in gnome-terminal, etc. as well as VC/xterm: import os, pwd print pwd.getpwuid(os.geteuid())[0]
Arun - thanks for the great debugging job! I've come to the conclusion that isn't is *not* a bug in python, as the exact same problem is experienced by the most minimal C program. More likely, this is a problem with some Xterm-like programs (gnome-terminal, the infamous rxvt, and most likely others) that don't update the utmp file. Using the code Arun provided is almost certainly a better solution, and more portable anyway.
Thanks to everyone! This strikes me as a problem that should be passed up to the python developers. The reason being that if a more robust method of determining the user id exists then the developers would be interested implementing it. It most certainly does not work as expected. Please let me know if the gentoo developers want to escalate it to the python folks, otherwise I'll do it myself. This feature was added to python2 to solve portability issues, since the old way of doing it was: python -c "import os; print os.getenv('USER')" Which was not necessarily portable.
Submitted to python bug tracking Request ID: 584566
*** Bug 20037 has been marked as a duplicate of this bug. ***