uos vs os

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
User avatar
Posts: 147
Joined: Tue Oct 17, 2017 5:18 am

uos vs os

Post by liudr » Thu Aug 09, 2018 7:35 pm

I am still uncertain after reading official doc on the uos vs os (or u-anything vs anything). Can someone give me a quick explanation?

http://docs.micropython.org/en/latest/e ... index.html

Anyway, trying to use uos instead of os in my MicroPython code but I still want to share as much code as I can with a regular Python script that runs on a PC. What I did was I defined functions to hide the differences, such as how to start a serial port on ESP32 is different than a USB-serial port on a PC. No problem. Just define start_port() differently so the main code is the same on ESP32 and PC.

So when it comes to importing u modules, I thought, why not "import uos as os" so the code that is shared between ESP32 and PC only uses os.listdir() but different import commands? Since I am not very experienced with Python, I wonder if this practice has some negative side effects I don't yet know. I can still import os as a different name but when do I have to import os instead of relying on uos?

Thanks in advance!

User avatar
Posts: 2054
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: uos vs os

Post by Roberthh » Fri Aug 10, 2018 8:49 am

In the ESP32 and STM32 (and others too) versions, os is defined as alias to uos, so you can also use:
import os
The definition is in the port specific file mpconfigport.h

User avatar
Posts: 3415
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada

Re: uos vs os

Post by dhylands » Sat Aug 11, 2018 12:25 am

The reasons for the u-modules is so that you can implement an os module which adds additional functionality to the uos module. The only module which should need to use uos is the os module which is extending the uos functionality. Without providing an os.py module, os will be a weak link to uos.

Post Reply