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 head scratching. What I needed to do is documented below.
Links to the sources of my information is at the bottom.

Install libraries/addon

  • First install the necessary packages:
pip install --upgrade pip --user
pip install --upgrade micropy-cli --user
pip install micropy-cli[create_stubs]
pip install rshell --upgrade
  • To prevent the error message Source directory /pyboard/stubs does not exist. after creation, open pybwrapper.py and 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 1, and reboot afterwards)

Get the right stubs

…by creating them yourself

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

Replace XX by your com port number in the commands below.

  • Connect the PyBoard over USB
  • Find out the serial port number (represented from here on by comXX)
  • Run micropy stubs create comXX -> 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 README.md for more information as well.

  • Pick a directory that will hold the files and 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",];'
  • Reload the window (CTRL + SHIFT +P) -> 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 ``` you just specified

  • Open the <project name>\pymakr.conf file. 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 behaviour: “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


  1. Node.js installer
    Node.js installer. Reboot after installation. ↩︎