Globally import uasyncio

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
Nico
Posts: 10
Joined: Wed Oct 28, 2015 6:56 am

Globally import uasyncio

Post by Nico » Tue Nov 17, 2015 9:59 pm

Hi,

I'm trying to add micropython support to HBMQTT, an MQTT client/broker I've been developing.
One task is to correctly import micropython renamed modules like uasyncio. Therefore, I've added this kind of statements to my python code:

Code: Select all

try:
    import uasyncio as asyncio
except:
    import asyncio
 
This works fine and allow to support both CPython and micropython within the same code.
Problems come with dependencies. For example, HBMQTT uses websockets module which also import asyncio. This fails when importing on microypthon, but I can't fix by modifying this third-party library.
So, i was wondering if there was a way in python to globally replace all "imports asyncio" to import "uasyncio as asyncio". May be byoverriding "__import__". Does anyone know how to do this ?

pfalcon
Posts: 1155
Joined: Fri Feb 28, 2014 2:05 pm

Re: Globally import uasyncio

Post by pfalcon » Tue Nov 17, 2015 10:11 pm

uasyncio isn't fully compatible with asyncio. If you depend on 3rd-party libraries, you will likely find that they don't work as expected and require porting themselves.

But otherwise, the (hackish) answer to your question should be obvious - create file "asyncio.py" on uPy search path with content "from uasyncio import *".

More sustainable approach is implementing sys.modules (on TODO for some time, patches welcome) and using it.
Awesome MicroPython list
Pycopy - A better MicroPython https://github.com/pfalcon/micropython
MicroPython standard library for all ports and forks - https://github.com/pfalcon/micropython-lib
More up to date docs - http://pycopy.readthedocs.io/

Post Reply