python-2.7 - python 2.7下uname报错: No module named os.

标签 python-2.7 uname

我在装有 anaconda 的系统上运行 python 2.7.3。我最近 pip 安装了 internetarchive,当我从命令行运行安装程序时,我看到:

AttributeError: 'module' object has no attribute 'uname'

我也在 python 的空闲命令行中尝试过这个。该模块加载正常,但我得到了同样的错误。显然我的安装中缺少 os.uname(),因为它在 python 中被记录为操作系统的一部分:https://docs.python.org/2/library/os.html#os.uname

我的安装:

>>> import os
>>> dir(os)

['F_OK', 'O_APPEND', 'O_BINARY', 'O_CREAT', 'O_EXCL', 'O_NOINHERIT', 'O_RANDOM', 'O_RDONLY', 'O_RDWR', 'O_SEQUENTIAL', 'O_SHORT_LIVED', 'O_TEMPORARY ', 'O_TEXT', 'O_TRUNC', 'O_WRONLY', 'P_DETACH', 'P_NOWAIT', 'P_NOWAITO', 'P_OVERLAY', 'P_WAIT', 'R_OK', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'TMP_MAX', 'UserDict', 'W_OK', 'X_OK', '_Environ', 'all', 'builtins', 'doc', '文件', '名称', '', '_copy_reg', '_execvpe', '_exists', '_exit ', '_get_exports_list', '_make_stat_result', '_make_statvfs_result', '_pickle_stat_result', '_pickle_statvfs_result', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'close', 'closerange', 'curdir', 'defpath', 'devnull', 'dup', 'dup2', 'environ', 'errno', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv ', 'execve', 'execvp', 'execvpe', 'extsep', 'fdopen', 'fstat', 'fsync', 'getcwd', 'getcwdu', 'getenv', 'getpid', 'isatty', '杀死','linesep','listdir' , 'lseek', 'lstat', 'makedirs', 'mkdir', 'name', 'open', 'pardir', 'path', 'pathsep', 'pipe', 'popen', 'popen2', ' popen3', 'popen4', 'putenv', 'read', 'remove', 'removedirs', 'rename', 'renames', 'rmdir', 'sep', 'spawnl', 'spawnle', 'spawnv' , 'spawnve', 'startfile', 'stat', 'stat_float_times', 'stat_result', 'statvfs_result', 'strerror', 'sys', 'system', 'tempnam', 'times', 'tmpfile', ' tmpnam', 'umask', 'unlink', 'unsetenv', 'urandom', 'utime', 'waitpid', 'walk', 'write']

python 中的其他一切似乎都很好,而且一直如此。我哪里做错了?是否有缺少 uname 的 python.os 版本?我在 Windows 机器上;这是个问题吗?

模块中的相关代码(internetarchive中的session.py):

 def _get_user_agent_string(self):
    """Generate a User-Agent string to be sent with every request."""
    uname = os.uname()
    try:
        lang = locale.getlocale()[0][:2]
    except:
        lang = ''
    py_version = '{0}.{1}.{2}'.format(*sys.version_info)
    return 'internetarchive/{0} ({1} {2}; N; {3}; {4}) Python/{5}'.format(
        __version__, uname[0], uname[-1], lang, self.access_key, py_version)

... <elsewhere> ...
self.headers['User-Agent'] = self._get_user_agent_string()

看起来(正如下面的答案中提到的)编码器很懒惰,并没有使它与 windows 兼容。他们向 API 提供了一个可选的“self.headers['User-Agent']',它应该可以与我提供的任何字符串一起使用。所以我可以破解这个。

最佳答案

是的,在 Windows 机器上是一个问题(此处):os.uname 仅在类 unix 系统上可用。 来自文档:

os.uname()

Return a 5-tuple containing information identifying the current operating system. The tuple contains 5 strings: (sysname, nodename, release, version, machine). Some systems truncate the nodename to 8 characters or to the leading component; a better way to get the hostname is socket.gethostname() or even socket.gethostbyaddr(socket.gethostname()).

Availability: recent flavors of Unix.

正如医生所说:

a better way to get the hostname is socket.gethostname() or even socket.gethostbyaddr(socket.gethostname())

sys.platformplatform package 是获取系统信息的一种可移植方式。 .

关于python-2.7 - python 2.7下uname报错: No module named os.,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36250558/

相关文章:

python - 将微秒格式化为小数点后两位(实际上是将微秒转换为几十微秒)

python - 如何从另一个 python 脚本执行 python 脚本并将其输出发送到文本文件中

c - 使用 uname(2) 检查错误?

python - 有没有一种Pythonic方法来检查操作系统是否是64位Ubuntu?

vim - 突出显示 python 文档字符串作为注释(vim 语法突出显示)

python-2.7 - NetworkX python : pagerank_numpy, pagerank 失败但 pagerank_scipy 有效

python-2.7 - 类 HttpTransport 没有属性 '_HttpTransport__get_request_url' python 错误

linux - 如何使用 uname -a 或 lsb_release -a 命令识别 linux 的风格?

windows - Windows 上 POSIX::uname() 版本号有哪些不同的可能值?