Python os.getlogin问题

gis*_*art 19 python gitpython

如果我创建一个文件,如:

import os
print os.getlogin()
Run Code Online (Sandbox Code Playgroud)

并使用cron运行它,我得到一个例外

print os.getlogin()
OSError: [Errno 22] Invalid argument
Run Code Online (Sandbox Code Playgroud)

如果我在shell中手动运行它 - 它的工作原理.

问题是,commit()中的GitPython 0.3.1使用了这个函数,我需要使用它.

有没有解决方法?

我在Ubuntu10.10/python2.6.6和Debian5.0.6/python2.5.2上测试过它.

kin*_*all 37

os.getlogin() 文档:"返回登录到进程控制终端的用户." 运行时,您的脚本没有控制终端cron.文档继续提出:"对于大多数用途,使用环境变量LOGNAME来查找用户是谁,或者pwd.getpwuid(os.getuid())[0]获取当前有效用户ID的登录名更有用."

由于您不想修改GitPython,您可以编写一个执行此操作的脚本:

import os, pwd

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

import git

# do whatever you need to do with GitPython here
Run Code Online (Sandbox Code Playgroud)

不过,我建议用GitPython提交一个bug(或者更好的是提交一个补丁).

  • 使用`pwd`模块的一个更简单的替代方法可能是[`getpass.getuser()`](https://docs.python.org/2/library/getpass.html#getpass.getuser) (3认同)