Accessing librarys on SD card

  • Hello,
    I want to access a mysql library which is on SD card but when I write: import mysql it says that this module could not be found. So how can I access this library?

  • @soren You have to mount the SD card first, before you can see it in ftp. The format I'm using is FAT32. A sample mount sequence is below, taken from the docs.

    import os
    import machine
    sd = machine.SD()
    os.mount(sd, '/sd')

  • @robert-hh EDIT: nevermind, it's working now. Gotta remember to "mount" the sd card.

    ah cool, thank you robert! when i ftp into the lopy, i can't see the SD card - does this mean that formatting isn't correct or something else is wrong? I formatted it as FAT32 and named it LOPY

  • @soren The places used by import are listed in sys.path. The default value is ['', '/flash', '/flash/lib']. Once you have mounted the sd card, e.g. in /sd, you can issue:
    Then it looks like ['', '/flash', '/flash/lib', '/sd', '/sd/lib']
    After that, scripts in /sd and /sd/lib are found by import.

  • @jmarcelino wait, does this mean that you can't use the SD card for extra library space? that makes it pretty useless if that's the case. I have mqtt, onewire and a few other small one-file libraries on the internal and I'm already out of space..

  • @jmarcelino
    Thanks for an answer. Do you have an example code that runs MySQL in a way you have described?

  • @michalt38
    As far as I know libraries have to be in the internal /flash/lib folder, you can put them there via FTP.

    However be aware that the standard Python MySQL library won't work in MicroPython as it's too resource heavy and has requirements such as the MySQL C libraries which aren't available here.

    Usually you'll do simple calls to a simpler web service/remote server which will have these libraries and access MySQL.

Log in to reply

Pycom on Twitter