OpenWrt Forum Archive

Topic: MicroPython/Micropython-Lib unable to import os

The content of this topic has been archived on 20 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

Latest trunk.

root@OpenWrt:~# micropython
MicroPython d5d146f on 2015-12-30; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
import os
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/micropython/os/__init__.py", line 22, in <module>
OSError: 2

Relevant code in micropython-lib (os/__init__.py):

libc = ffilib.libc()

try:
    errno__ = libc.var("i", "errno")
    def errno_(val=None):
        if val is None:
            return errno__.get()
        errno__.set(val)
except OSError:
    __errno = libc.func("p", "__errno", "")
    def errno_(val=None):
        if val is None:
            p = __errno()
            buf = ffi.as_bytearray(p, 4)
            return int.from_bytes(buf)
        raise NotImplementedError

It's still usable, but the 'os' library is essential, much of micropython-lib can't be used as a result, not to mention 'os' itself.

The error is specifically being thrown on the line '__errno = libc.func("p", "__errno", "")'.

(Last edited by ktennozu on 30 Dec 2015, 15:49)

Target System: Atheros AR7xxx/AR9xxx, Target Profile: TP-LINK TL-WR841N/ND. If you want anything else let me know.

This is baked into the build, by the way, not installed through opkg or anything.

Any idea what might be related to, so I have a starting point to see about workarounds? I looked into the source, maybe it can't find libc or libffi? Or there's possibly an issue with either of those two libraries?

Thanks for the quick reply.

Looks like a musl incompatibility with __errno.

Try replacing the code you pasted with:

   def errno_(val=None): return 0

If that works then I'd open an issue on GitHub for a better workaround.

That worked. Hopefully it won't impact my ability to use the library down the line...

__errno = libc.func("p", "__errno", "") --> __errno = libc.func("p", "__errno_location", "")

-----------------------------------------------------------------------------------------------------------------
root@OpenWrt:~# cat /lib/libc.so | egrep  errno
__errno_location
__h_errno_location
h_errno
-------------------------------------------------------------------------------
MicroPython b136759 on 2016-09-26; linux version
Use Ctrl-D to exit, Ctrl-E for paste mode
import os
os.mkdir('/tmp/test')
os.mkdir('/tmp/test')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/micropython/os/__init__.py", line 96, in mkdir
  File "/usr/lib/micropython/os/__init__.py", line 84, in check_error
OSError: 17
--------------------------------------------
EEXIST  = 17  # File exists from errno.py

The discussion might have continued from here.