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 new (version 2, released summer 2018) EV3 Python library. 

The 'shebang'

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 and where to find that program. 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.

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

To launch a program from Brickman, simply open the file browser, use the up and down arrows to navigate to the desired script and then press the Enter button. Be aware that this process will of course not cause the latest version of the script to be downloaded to the EV3 so if you have modified the script and wish to launch the modified script from Brickman rather than from VS Code then you should first click the 'Send project to device' icon in the header of the EV3 Device Browser and then launch the script from Brickman.

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 VS Code's terminal panel. 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 EV3's left pair of LEDs 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).