Using MicroPython (PyBoard V1.1) with Visual Studio Code

Warning

This still isn’t working as I think it should. Especially the linting and autocomplete still doesn’t work (leaving only the PyMakr module which works partly, in fact). For example: “import pyb” still does not add autocompletion when typing something like “s = pyb.<CTRL + SPACE>”. Most likely this is because I used a combinations of tools that shouldn’t be used together. Need to spend more time on this…

Intro

I’ve recently started using a PyBoard for a project that I am working on. Writing code in an IDE, saving the files, then manually copying them to the board to test them gets tedious rather quickly. If you’re using Visual Studio Code, there are options to do almost everything from within the IDE itself. Getting things to work as they should took a bit of effort and headscratching. What I needed to do is documented below.
Links to the sources of my information is at the bottom.

Install libraries/addon

  • pip install --upgrade pip --user
  • pip install --upgrade micropy-cli --user
  • pip install micropy-cli[create_stubs]
  • pip install rshell --upgrade
  • To prevent “Source directory /pyboard/stubs does not exist.” after creation open pybwrapper.py, change line 63 from
    pyb_path = f"{self.pyb_root}{_path}"
to:
    pyb_path = f"/flash/{_path}"

and save the file.

  • Inside VSC, install the Pymakr plugin
  • Install node.js (see link below, and reboot afterwards)

Create the stub yourself…

(This did not work properly for me. See below for another option.)

  • Connect the PyBoard over USB
  • Find out the serial port number (represented from here on by comXX)
  • Run
    micropy stubs create comXX

    (replace XX by com port number) -> this takes a couple of minutes

  • the result will be copied to ~/.micropy/stubs (in my case in a sub directory “pyboard-1.13.0”

…Or get them from someone else

I got mine from Josverl on Github. See his read.me for more information as well.

  • Pick a directory that will hold the files. Clone the repo:
    git clone git@github.com:Josverl/micropython-stubs.git
  • create a symlink in your project folder:

    mklink /d all-stubs <directory holding the stubs>
  • create a settings.json file in your project folder containing (change the references to the stubs you need, in the desired order):
{
    "python.linting.enabled": true,
    "python.linting.pylintEnabled": true,
    "python.languageServer": "Microsoft",
    "python.jediEnabled": false,
    "python.autoComplete.extraPaths": [
         "all-stubs/cpython_core",
         "all-stubs/pyboard_1_13_95",
         "all-stubs/mpy_1_13-nightly_frozen/GENERIC",
         "all-stubs/cpython_pyboard"
     ],
     "python.autoComplete.typeshedPaths": [
          "all-stubs/cpython_core",
         "all-stubs/pyboard_1_13_95",
          "all-stubs/mpy_1_13-nightly_frozen/GENERIC",
          "all-stubs/cpython_pyboard"
     ],
     "python.analysis.typeshedPaths": [
          "all-stubs/cpython_core",
          "all-stubs/pyboard_1_13_95",
          "all-stubs/mpy_1_13-nightly_frozen/GENERIC",
         "all-stubs/cpython_pyboard"
    ]
}
  • Copy the .pylintrc file from the sample folder and adapt the init-hook line to reflect the chosen folders (in last step):
[MASTER]
init-hook='import sys;sys.path[1:1] = ["all-stubs/cpython_core", "all-stubs/pyboard_1_13_95", "all-stubs/mpy_1_13-nightly_frozen/GENERIC", "all-stubs/cpython_pyboard",];'
  • Restart VSC:
Developer: Reload Window

Set up your project (in VSC terminal, with project open)

  • To check that your stub (config file for your board) has been installed run
    micropy stubs list 
  • If not, run
    micropy stubs add <path to stub> 

    (where the path is the directory that contains the info.json file.)

  • run
    micropy init <project name>
  • Select what to generate (‘a’ selects all)
  • select which stub to use (the one you just created)
  • A subdirectory will created with <project name> you just specified
  • Open the pymakr.conf file in the subdirectory <project name>. Change
    "address":"192.168.4.1" 

    to your comXX. To do this system wide for Pymakr, open the command palette (CTRL+SHIFT+P) and select/type “Pymakr > Global settings”

  • In the same config file, I had to set “auto_connect”: false to resolve some vague
    "> Failed to connect (Error: Port is not open). Click here to try again." 

    errors when connecting to the board. Setting this to “true” the following behavior: “ignores any ‘address’ setting and automatically connects to the top item in the serial port list”

  • See here for more explanation on the other options

Using the combination

  • To add package dependencies to your project:
    micropy install <PACKAGE_NAMES>
  • Functionality in the Pymakr addon can be used by opening the command palette (CTRL+SHIFT+P) and typing Pymakr. There are options to (dis)connect, run the current file, up/download the whole project, get the FW version,…
  • Note the new terminal “Pymakr Console”:
    Pymakr console
  • And the “Pymakr toolbar”:
    Pymakr toolbar

Shortcuts (from the Pymakr addon info)

ctrl-shift-c : (Re)connect
ctrl-shift-g : Global settings
ctrl-shift-s : Synchronize project
ctrl-shift-r : Run current file
ctrl-shift-enter : Run current Line

References