MicroPython | 4 min | 29668
Last December, I wrote an article telling you that I was switching from Atom.io to Visual Studio Code to program MicroPython. Nowadays, I'm still using VSCode and the PyMakr extension. The latter enables the communication to a board running MicroPython using the built-in command line REPL. Thus, I can run a single file on my board, sync my entire project or directly type and execute commands using the Terminal on VSCode. However, I was missing some of the main features of VSCode, namely IntelliSense, autocompletion, and linting capabilities. Last month, BradenM contacted me and told me that he had programmed a new module that enables those features on VSCode for MicroPython! Then, this tutorial is to introduce the
micropy-clisolution, which I am very happy with!
For this tutorial, you need the following software and optional the following ESP32 board:
You need Python3 to run this extension and to install it you need
pip. You can check if it is installed typing the following:
user@lemariva:~$ pip --version pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
If not, just type on Linux:
user@lemariva:~$ sudo apt-get install python3-pip
on Windows, check this link.
Then, you need to install
user@lemariva:~$ pip install --upgrade micropy-cli Collecting micropy-cli [...]
Setting a Project
After installing the Python module, you can open VSCode and then, open the folder in which you have your project. Then, using the Terminal that you got on VSCode (if you don't see it, hit on
Ctrl+`-Ctrl + back-tick character-, or drag the lower horizontal bar up) and type the following to search and install a MicroPython stub (in my case for the ESP32):
# Searching for the MicroPython Stubs user@lemariva:~/uPyEink$ micropy stubs search esp32 MicroPy Searching Stub Repositories... MicroPy Results for esp32: MicroPy esp32-micropython-1.10.0 MicroPy esp32-micropython-1.11.0 (Installed) MicroPy esp32-micropython-1.9.4 MicroPy esp32-pycopy-1.11.0 MicroPy esp32-pycopy-126.96.36.199 MicroPy esp32_LoBo MicroPy esp32_LoBo-esp32_LoBo-3.2.24 # Installing the last version of the stub for the ESP32 user@lemariva:~/uPyEink$ micropy stubs add esp32-micropython-1.11.0 MicroPy Adding esp32-micropython-1.11.0 to stubs MicroPy Resolving stub... MicroPy esp32-micropython-1.11.0: 100%|██████████████████████████████████████████████████| [13.4k/13.4k @ 15.9kB/s] MicroPy Detected Firmware: micropython MicroPy Firmware not found locally, attempting to install it... MicroPy Resolving stub... MicroPy micropython: 100%|███████████████████████████████████████████████████████████████| [36.6k/36.6k @ 63.9kB/s] MicroPy ✔ micropython firmware added! MicroPy ✔ esp32-micropython-1.11.0 added!
As I said, in my case, I've installed the MicroPython stub v.1.11.0 for the ESP32. The stub files are the magic behind how micropy allows features such as linting, IntelliSense, and autocompletion to work (as described in the README of the repository). After doing this, you can initiate a project typing
micropy initon the VSCode Terminal. The extension asks you 3 questions: project name, type of template and stub that you need:
user@lemariva:~/uPyEink$ micropy init MicroPy Creating New Project ? Project Name uPyEink ? Choose any Templates to Generate (Use arrow keys to move, <space> to select, <a> to toggle, <i> to invert) » ● VSCode Settings for Autocompletion/Intellisense ○ Pymakr Configuration ○ Pylint Micropython Settings ○ main.py & boot.py files ? Which stubs would you like to use? (Use arrow keys to move, <space> to select, < a> to toggle, <i> to invert) » ● esp32-micropython-1.11.0
After answering those questions (select the option using
<space>- filled circle!), you get the following confirmation that everything is installed and working:
MicroPy Initiating uPyEink MicroPy Stubs: esp32-micropython-1.11.0 MicroPy Rendering Templates MicroPy Populating Stub info... MicroPy Vscode template generated! MicroPy ✔ Stubs Injected! MicroPy ✔ Project Created! MicroPy Created uPyEink at ./.
Then, you can start using the Python extension in your project. An example can be seen in Fig. 1.
Fig. 1: Checking the
Installing MicroPython Extensions
micropy-clihas also a dependency management system. If your project needs a MicroPython module, you can add it by typing, while in your project's root directory, the following:
micropy install <PACKAGE_NAMES>.
# Example: picoweb user@lemariva:~/uPyEink$ micropy install picoweb MicroPy Loading Project MicroPy esp32-micropython-1.11.0 is already installed! MicroPy ✔ Project Ready! MicroPy Installing Packages MicroPy Adding picoweb to requirements... MicroPy picoweb-1.7.2.tar.gz: 100%|█████████████████████| [7.61k/7.61k @ 3.98MB/s] MicroPy ✔ Package installed!
After installing the package, autocompletion and IntelliSense help you to program your code as you can see in Fig. 2.
Fig. 2: Checking the
micropy-cliwith MicroPython extensions (picoweb)
micropy install <PACKAGE_NAMES>also creates a
requirements.txtfile inside the directory listing the modules that you've installed.
However, you still need to install the packages on the target board (ESP32, ESP8266, etc.). To do that, you can follow this tutorial.
Uploading the Code
To upload your code, you still need the PyMakr extension. You can type again
micropy initand this time select
Pymakr Configuration. This creates the
pymakr.confwith the needed settings to connect to your target board.
Be aware that the standard configuration is using
"address": "192.168.4.1", which is not always the case if you're just starting to program the board. Change the option value to
/dev/ttyUSBx(Linux, macOS) or the corresponding
COMxx(Windows) if you're connected to the target board using the USB port, and read this article to learn how to upload the files. Additionally, check the option
"sync_folder": "src", it should point to the folder to sync, if you don't need it (upload the whole project), you can delete it.
VSCode PyMakr Error (Update 01.08.2019) - Only version 1.36.1:
Today, I tried to upload the files to an ESP32 and I got following error on VSCode::
There was an error with your serialport module, Pymakr will likely not work properly. Please try to install again or report an issue on our github (see developer console for details)
This is not a
micropy-cliproblem, but a
PyMakrproblem. Apparently this is caused by VSCode switching to a newer version of Electron (4.2.5), which requires a different binding for the serialport module in order to work. If you go to
VSCode > Help > About, you can see your Electron version as in Fig. 3.
Fig. 3: VSCode About
To solve the problem you need to rebuild the binding. Thus, open a CMD or Terminal and type the following:
npm install -g prebuild-install # use sudo if required cd ~/.vscode/extensions/ # depeding on your platform this changes (see Note 1) cd pycom.pymakr-1.1.3 cd node_modules/@serialport/bindings prebuild-install --runtime electron --target 4.2.5 --tag-prefix @serialport/bindings@ --verbose --force # Restart VSCode, then all is workingNote 1: Depending on your platform, go to the extension location in the following folder:
- - Windows
- - macOS
- - Linux
micropy-climodule is an excellent option to program MicroPython using VSCode. It's an optimal complement to PyMakr and allows you to use the features of VSCode (IntelliSense, autocompletion, and linting capabilities). If you are programming MicroPython, you need to switch to VSCode right now and install the
micropy-climodule and the
- - Windows