Learn EV3 Python‎ > ‎

Basics

Once again, let me stress that this site is not intended to teach you basic Python programming. There are hundreds of free courses to help you learn about standard Python and some are listed on the Learn Python page. This site exists to help people who already have a basic knowledge of standard Python to make the extra step of learning how to use the robotic functions that EV3 Python makes available.

Also, this site is written only to help people using VS Code with the EV3 extension and who are using the old (version 1) EV3 Python library. If you are using VS Code with the EV3 extension but are using the much superior new version (version 2) of the EV3 Python library then click HERE to visit the new site that is focused on version 2 of the Python library. (The new site is not accessible just yet - patience!)

Launch Python programs from VS Code
To see how to write and run Python scripts in VS Code please visit the page 'The VS Code workflow'.

Launch Python programs from the EV3's Brickman menu
Before you can launch a Python program from the Brickman interface on the brick you must make sure that your program includes a special instruction called a 'shebang' as the first line of your program (it MUST be the first line in order to work). That shebang is 

 #!/usr/bin/env python3

I have included that shebang as the first line in every program on this site. Since the line begins with a hash character you could assume that this is simply a comment that will be ignored by the compiler when the program is run. This would be a false assumption because in fact lines beginning with #! are actually special instructions called 'shebangs' that DO affect how the program runs since they tell the compiler which program to use to open the file. You may also come across programs beginning with #!/usr/bin/python which is an instruction to use Python 2 to open the program but you should favour Python 3 since Python 2 is now obsolescent. All the Python programs on this site are written in Python 3 and use the Python 3 shebang.
A shebang is comparable to a file association in Windows. In Windows you can use 'Open with...' to specify that all files with a certain file extension are to be opened within a certain program. For example, you could specify that programs with the .txt extension are to be opened in MS Word. Shebangs have a similar effect as a file association but the shebang must be included in every file that is to be opened by a certain program.

Stopping a program that was launched from VS Code
Most programs will of course stop by themselves. If you launched a program that does not stop by itself, perhaps because you deliberately included an infinite loop, for example, or because something went wrong, force the program to stop by clicking the stop button (a red square) which appears in VS Code when you launch a program on the EV3 using the EV3 extension.

Stopping a program that was launched from Brickman
Most programs will of course stop by themselves. If you launched a program that does not stop by itself, perhaps because you deliberately included an infinite loop, for example, or because something went wrong, force the program to stop by long-pressing the Back button.


Start the Python 3 interpreter from the command line
Since you are using VS Code with the EV3 extension you can easily start the Python interpreter and then you will be able to run Python commands one by one without having to create and save a Python script. You can either run the Python interpreter on the PC, in which case you will not be using the EV3 Python on the EV3 so robot-specific functions will not be available, or you can run the Python interpreter on the EV3 itself in which case you can use the robot-specific functions.

To run the python interpreter locally on the PC
In the bottom-right panel of the VS Code window click TERMINAL and then, at the prompt, type Python (not Python3). You should then see this prompt: >>> which means you can start typing Python commands that will be run as soon as you press the Enter key. For example you can type print('Hello, world') and press the Enter key and the text will immediately be printed to the terminal of the PC. To close the Python interpreter type exit() and press the Enter key.

To run the python interpreter on the EV3 from VS Code
Right-click the green dot that shows the EV3 is connected to your PC and choose 'Open SSH Terminal'. When the terminal has opened, type Python3 (not Python) at the prompt. You should then see this prompt: >>> which means you can start typing Python commands that will be run as soon as you press the Enter key. For example you can type these three lines in order to make the left LED pair of the EV3 glow amber:
from ev3dev2.led import Leds
leds = Leds()
leds.set_color('LEFT', 'AMBER')
To close the Python interpreter type exit() and press the Enter key.

If, while are using the SSH terminal, you are ever asked for the password for the user called 'robot' (you) it is 'maker' (without the quotes).
Comments