Reputation: 75
We use the micro:bit with the accessory Bit:bot XL. I have a module (robot.py) with just a single function for the Bit:bot (for now). In my script (my.py) I import this function. But after flashing the micro:bit (uflash my.py), there's an error on the LED matrix saying: "Import error: No module named robot". However, if I just use standard Python functions (like print()) in my module and run my script in the VSCode terminal, it works fine. Putting my function directly into my script also works fine. Why can't I transfer both my script and my module to the micro:bit?
A workaround can be found at Flash microbit embedding a class .py file. The workaround requires you to flash the micro:bit first and then transfer modules with ufs. It works, but makes things more complicated for our target group which is blind children.
My module robot.py is stored in a folder called bitbot. The folder path is specified in sys.path. In the package there is also an empty file __init__.py
.
# my.py
from microbit import *
from robot import drive
drive(500, 3000)
# bitbot/robot.py
from microbit import *
def drive(speed, duration):
"""Makes the Bit:bot move forwards with a given speed for a number of milliseconds"""
pin8.write_digital(0) #Left motor direction
pin12.write_digital(0) #Right motor direction
pin16.write_analog(speed) #Left motor speed
pin14.write_analog(speed) #Right motor speed
sleep(duration)
I have tried different ways to import the module but they all give the same import error: import bitbot, import robot, from bitbot import *, from robot import *, from bitbot import drive, from robot import drive, from bitbot.robot import drive.
I use:
Upvotes: 3
Views: 950
Reputation: 1
Sadly, there does not seem to be a way to flash other libraries with uflash. I would recommend using the official python editor
Upvotes: 0
Reputation: 2247
You will likely need to install a VSCode extension to write the files to your MicroBit. Have a look at micro:bit by Joseph Fergusson
Upvotes: 0