Search code examples
python-2.7uname

Error: No module named os.uname under python 2.7


I'm running python 2.7.3 on a system that has anaconda. I recently pip installed internetarchive and when I run the installation program from command line I see:

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

I also tried this from within python's idle command line. The module loads fine, but I get the same error. Apparently os.uname() is missing from my installation, as it is documented as part of os in python here: https://docs.python.org/2/library/os.html#os.uname

My installation:

>>> 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', 'file', 'name', 'package', '_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', 'kill', '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']

Everything else in python seems fine and has been. Where did I go wrong? Are there version of python.os that lack uname? I'm on a windows machine; is that an issue?

Here is the relevant code in the module (session.py in internetarchive):

 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()

So seems like (as mentioned in the answer below) the coder was lazy and didn't make this windows-compatible. They supply an optional 'self.headers['User-Agent']' to the API and it ought to work with any string I provide. So I can hack this.


Solution

  • Yes, be on a windows machine is an issue (here): os.uname is available only on unix-like systems. From the doc:

    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.

    As said by the doc:

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

    A portable way to get some informations about the system is sys.platform, and the platform package.