MP syntax error

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ajocius
Posts: 83
Joined: Mon Feb 19, 2018 6:31 am

MP syntax error

Post by ajocius » Tue Feb 27, 2018 9:34 am

I am new to MP, starting with simple scripts on ESP32. I have learned that alignment of code is relevant in MP (loops, conditional statements). Are they any other rules? like spaces, empty lines, uppercase vs lower case, etc? I have been searching for mistake in my code and could not find it, but after deleting it and retyping it suddenly worked as expected. I suspect I had some unnecessary item (like space ) that was causing error as I retyped identical code.

Are they any user friendly editor that you can recommend that would highlight errors help to find them? I use notepad++ , it is quite helpful, but was not indicating any errors in my above attempt. Do you know any other editor that might help?

OutoftheBOTS_
Posts: 847
Joined: Mon Nov 20, 2017 10:18 am

Re: MP syntax error

Post by OutoftheBOTS_ » Tue Feb 27, 2018 10:49 am

Python is very sensitive about indention as this is the way a block is defined compared to other languages that use brackets. Python is also case sensitive so True and true are seen as different.

Most people will use a good IDE to help with the formatting. I haven't yet learned to use any of the IDEs so I use Notee++ myself. I have found there can be formatting that you don't realize without looking at the left of the line of code for the status of each line. Make sure that you save the file with a .py ending then note++ knows it is a python file and will format it much better for you.

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: MP syntax error

Post by stijn » Tue Feb 27, 2018 10:51 am

Doesn't Notepad++ have pluging for Flake8/PyLint/Pep8 Autoformat etc? That will save you a lot of work.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: MP syntax error

Post by pythoncoder » Tue Feb 27, 2018 11:17 am

If you're new to Python I'd recommend starting with standard Python on a PC, and following a book or online tutorial. MicroPython is effectively identical until you get on to fairly advanced topics, and there are a number of PC IDE's available including debugger support. It also saves the hassle of copying files to the device.
Peter Hinch
Index to my micropython libraries.

cefn
Posts: 230
Joined: Tue Aug 09, 2016 10:58 am

Re: MP syntax error

Post by cefn » Tue Feb 27, 2018 11:34 am

I routinely use PyCharm community edition (which has a Micropython plugin) with Geany as a lightweight editor for casual browsing/editing when creating a full project in PyCharm is too much hassle. Other editors on my radar as high-quality but which I haven't used in anger are Mu Editor, VSCode, WingIDE (which has some free versions). Mostly I don't use these because of issues with Python3 debugger support, as I think they are all fine editors which should help you with managing indentation and syntax.

Post Reply