Manage cookies

We use cookies to improve our services. Read more about how we use cookies and how you can refuse them.

The necessary cookies help to make the website work properly.

Anonymous statistical cookies help to understand how visitors use the website.

  • MicroPython
    4 min | 48760

    #MicroPython: VSCode IntelliSense, Autocompletion & Linting capabilities

    MicroPython | 4 min | 48760


    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-cli solution, which I am very happy with!

    Software

    For this tutorial, you need the following software and optional the following ESP32 board:

    VSCode
    • VSCode
    GitHub
    • micropy-cli
    VSCode Extensions
    • PyMakr extension
    LILYGO® TTGO ESP32
    • LILYGO® TTGO ESP32

      WROVER-B 8MB PSRAM

    Installing micropy-cli

    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 micropy-cli as:

    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-2.11.0.1
    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 init on 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.

    test_1.gif
    Fig. 1: Checking the micropy-cli functionalities

    Installing MicroPython Extensions

    micropy-cli has 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.

    test_2.gif
    Fig. 2: Checking the micropy-cli with MicroPython extensions (picoweb)

    The line micropy install <PACKAGE_NAMES> also creates a requirements.txt file 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 init and this time select Pymakr Configuration. This creates the pymakr.conf with 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-cli problem, but a PyMakr problem. 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.

    electron.png
    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 working
    Note 1: Depending on your platform, go to the extension location in the following folder:
    • - Windows %USERPROFILE%\.vscode\extensions
    • - macOS ~/.vscode/extensions`
    • - Linux ~/.vscode/extensions`

    Conclusions

    The micropy-cli module 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-cli module and the Pymakr extension.


    Comments

    jtuttas 08.24.2019

    Hello, such a nice tutorial! But execution "micropy stubs search esp32" on my windows 10 machine cased the following error:

    Traceback (most recent call last):
      File "c:\users\jtutt\appdata\local\programs\python\python36-32\lib\runpy.py", line 193, in _run_module_as_main
        "__main__", mod_spec)
      File "c:\users\jtutt\appdata\local\programs\python\python36-32\lib\runpy.py", line 85, in _run_code
        exec(code, run_globals)
      File "C:\Users\jtutt\AppData\Local\Programs\Python\Python36-32\Scripts\micropy.exe\__main__.py", line 5, in 
      File "c:\users\jtutt\appdata\local\programs\python\python36-32\lib\site-packages\micropy\cli.py", line 13, in 
        from micropy.main import MicroPy
      File "c:\users\jtutt\appdata\local\programs\python\python36-32\lib\site-packages\micropy\main.py", line 9, in 
        from micropy import data, utils
      File "c:\users\jtutt\appdata\local\programs\python\python36-32\lib\site-packages\micropy\utils\__init__.py", line 12, in 
        from .pybwrapper import PyboardWrapper
      File "c:\users\jtutt\appdata\local\programs\python\python36-32\lib\site-packages\micropy\utils\pybwrapper.py", line 14, in 
        import rshell.main as rsh
      File "c:\users\jtutt\appdata\local\programs\python\python36-32\lib\site-packages\rshell\main.py", line 67, in 
        if 'libedit' in readline.__doc__:
    TypeError: argument of type 'NoneType' is not iterable

    Any idea what is going wrong? best regards jtuttas

    Patrice 08.31.2019

    Thanks a lot for your help to retry my pymakr console lost after update And also I will quickly install micropython-cli with your help. Best regards,

    Ramprasad Rao 09.20.2019

    When I try to install something like picoweb with 'micropy install picoweb', I get the following error. Also error when installing any packages.

    $ micropy install picoweb
    
    MicroPy  Loading Project
    MicroPy  esp32-micropython-1.11.0 is already installed!
    
    MicroPy  Fetching Requirements
    Traceback (most recent call last):
      File "/anaconda3/bin/micropy", line 10, in 
        sys.exit(cli())
      File "/anaconda3/lib/python3.7/site-packages/click/core.py", line 764, in __call__
        return self.main(*args, **kwargs)
      File "/anaconda3/lib/python3.7/site-packages/click/core.py", line 717, in main
        rv = self.invoke(ctx)
      File "/anaconda3/lib/python3.7/site-packages/click/core.py", line 1137, in invoke
        return _process_result(sub_ctx.command.invoke(sub_ctx))
      File "/anaconda3/lib/python3.7/site-packages/click/core.py", line 956, in invoke
        return ctx.invoke(self.callback, **ctx.params)
      File "/anaconda3/lib/python3.7/site-packages/click/core.py", line 555, in invoke
        return callback(*args, **kwargs)
      File "/anaconda3/lib/python3.7/site-packages/micropy/cli.py", line 120, in install
        project = Project.resolve('.')
      File "/anaconda3/lib/python3.7/site-packages/micropy/project/project.py", line 388, in resolve
        proj.load(stub_manager=micropy.STUBS, verbose=verbose)
      File "/anaconda3/lib/python3.7/site-packages/micropy/project/project.py", line 195, in load
        self.load_packages()
      File "/anaconda3/lib/python3.7/site-packages/micropy/project/project.py", line 163, in load_packages
        meta = utils.get_package_meta(name, spec=spec)
      File "/anaconda3/lib/python3.7/site-packages/micropy/utils/helpers.py", line 259, in get_package_meta
        data = resp.json()
      File "/anaconda3/lib/python3.7/site-packages/requests/models.py", line 897, in json
        return complexjson.loads(self.text, **kwargs)
      File "/anaconda3/lib/python3.7/json/__init__.py", line 348, in loads
        return _default_decoder.decode(s)
      File "/anaconda3/lib/python3.7/json/decoder.py", line 337, in decode
        obj, end = self.raw_decode(s, idx=_w(s, 0).end())
      File "/anaconda3/lib/python3.7/json/decoder.py", line 355, in raw_decode
        raise JSONDecodeError("Expecting value", s, err.value) from None
    json.decoder.JSONDecodeError: Expecting value: line 7 column 1 (char 6)

    Sander 12.14.2019

    Thank you very much for this information. It's exactly what I needed to be able to do decent Micropython development.

    Greg Woods 06.09.2020

    Is the VSCode PyMakr Error still needed? (2020-06) I got everything installed and working without this step. Thanks for a great article

    bill 10.31.2020

    You gave instructions in one font, but then it seemed you also typed some things down in the console in the middle of all the output. This did not work for us, so we're going back over it. What is obvious for you needs to be spelled out better for some others.