Python indentation, SSH and Raspberry Pi
I try to optimize my work. Even it is about CAD designs for printing 3D parts, learning new things or writing software. I’m new in writing Python scripts and sometimes the syntax causes me headaches. The video below explains common mistakes that a programmer does when is writing Python scripts. It helps me to understand why I have so many errors on the indentation of the lines.
one of the mistake: using Tab key or spaces for indentation. Corey recommendation is to use an IDE for indentation.
Using an IDE to write Python scripts is the easier way. Usually, I write ROS nodes in Python via SSH. All these nodes are running on a Raspberry Pi. I use ‘nano’ to create and write the python files via SSH connection. I cannot use a Python IDE via SSH because this connection doesn’t provide GUI resources.
When you’re working on robots and don’t have too many hardware and software resources at your service, you have to find solutions. I have one recommendation and one idea for programmers who are writing Python scripts via SSH are:
1. don’t mix ‘tab’ and ‘space’ indentations in nano editor:
This is the usual mistake that I do by reflex. Sometimes I’m lost in writing the program and I mix the Tab and space keys for indentation. I change in nano the tab character spaces to 4 and everything works without syntax mistakes if I use only the tab key for indentation.
- Step 1: Go to your home directory and type the command: sudo nano /etc/nanorc
- Step 2: Navigate into the configuration file until the line with #set tabsize 8
- Step 3: Remove the # and put 4 instead of 8
- Step 4: – Ctrl + O – to save the file, and then – Ctrl + X – to close the file
2. change the nano editor with an IDE and use git:
This is an idea: write the Python script on my Windows PC, commit to git, and then connect via SSH to clone the git package. In this way, I will reduce syntax mistakes. In the same time, if I deliver, for example, only a new line of code, will take a little bit more time to check if it works or not. I didn’t test this method. If someone uses this method on Raspberry Pi, please leave a comment with the advantages and disadvantages of this method.