Search code examples
pythonwindows-subsystem-for-linux

Are Python Unix-only APIs supported under WSL?


Some Python APIs, such as os.pread, are documented with availability "Unix", and indeed they are not visible when using native Windows Python.

Are they supported in Python installed via WSL (Windows subsystem for Linux)?


Solution

  • Yes. For most intents and purposes, when you run code through WSL (WSL2, at least), you are running code through the OS that you installed via WSL.

    Proof of running under WSL:

    >>> import platform; platform.uname().release
    '5.15.79.1-microsoft-standard-WSL2'
    

    Test data:

    >>> with open('temp.txt', 'w', encoding='utf-8') as f:
    ...     f.write('foo\nbar\nbaz\n')
    ...
    12
    

    Test results:

    >>> import os
    
    >>> f = os.open('temp.txt', os.O_RDONLY)
    
    >>> os.pread(f, 4, 0)
    b'foo\n'
    
    >>> os.pread(f, 4, 4)
    b'bar\n'
    
    >>> os.pread(f, 4, 0)
    b'foo\n'
    
    >>> os.close(f)