Problem with Python and the LegoMindstorms when try to use "print()"

436 views Asked by At

I have a question to python. I'am trying to build someting with my LegoMindstorms EV3 and try Python with it. I never used Python before, so it is my first day with it. I tried a lot and looked around the web but I couldn't find a solution to my Proplem. When I want to print someting in the console in VS Code I use print() this works, when I use #!/usr/bin/env pybricks-micropython at the top, but I need #!/usr/bin/env python3 to use some library's. But when I use the Python3 header and call for example print('Hello') nothing comes in Output console, only when I use the micropython header. But in both cases the messages gets displayed on the EV3 screen but I want it in the simple Output terminal. I'am wondering if I'am missing something obvious here, I searched over two hours but couldn't find something to my proplem. I'am using the latest EV3Dev software on the EV3 and VS Code with Python 3.8.7 64-bit

Anyone can help me?

1

There are 1 answers

0
Laurens Valk On

An EV3 with ev3dev on a microSD card has several versions of Python installed by default:

With #!/usr/bin/env pybricks-micropython at the top, your script will run using Pybricks.

This is a version of MicroPython, with added support for LEGO hardware like motors and sensors. This is the recommended approach supported by LEGO. Because MicroPython is very resource-efficient and hardware support is integrated, this runs smoothly. But you cannot use all Python libraries. Instead, it's usually easier to copy the relevant code to a module in your project folder and import that module.

Output from print is printed to the console in Visual Studio Code.

With #!/usr/bin/env python3 at the top, your script will run with Python 3.

This does not have built-in LEGO hardware support, but there exist Python libraries like ev3dev-lang-python that interface with the hardware via the ev3dev file system. Since this is just regular Python 3, you can theoretically use any Python library you want. But due to the limited processing power of the EV3, regular Python 3 is quite slow and installing additional libraries is even slower.

To make printed output show up in Visual Studio Code, do this:

import sys

print('Hello, world!', file=sys.stderr)