Closed Sunrise98 closed 2 years ago
With the disclaimer that we haven't focused on this use case yet so there isn't an easy way to do it, here is one way to do it right now...
Make sure you have Python 3 installed on your computer.
Install the Microsoft Python and LEGO EV3 extensions in VS Code.
Create a new project in VS Code using the LEGO EV3 extension.
Open the main.py
file to activate the Python extension in VS Code.
Open a terminal in VS Code and create a virtual environment in your project. (On Windows use py -3
instead of python3
.)
python3 -m venv .venv
VS Code should ask you if you want to use the virtual environment that you just created. Answer yes. If it doesn't, select the virtual environment as the Python interpreter in the status bar (see Microsoft Python link above for screenshots).
Use the command palette in VS Code to open a new Python terminal (search for Python: Create Terminal
command).
In the Python terminal run the following
python -m pip install --upgrade pip pip install git+https://github.com/pybricks/pybricks-api@master
EDIT: This step was needed in the LEGO EV3 MicroPython v1.0.0 but is no longer needed in v2.0.0 (released May 2020)
In the main.py
file, change the line
from pybricks import ev3brick as brick
to
from pybricks.hubs import EV3Brick
Code completion should be working already when you make this change.
Thank you for your quick response! I did above way and checked the autocomplete function.
I would be grateful if you could add this function as a default.
Looking forward to your support, thanks.
+1 on incorporating Python and MS Python extension into the standard installation (or at least the installation instructions). Using the approach worked fine on my windows machine, but it is of course rather fiddly for FLL. I wonder if this can be scripted as part of the "create a new project" Activity in the Lego VSCode Extension?
Hello David,
what is your opinion on this approach: https://micropy-cli.readthedocs.io/en/latest/index.html#
The micropy stub for pybricks is also available: https://github.com/BradenM/micropy-stubs/tree/master/packages/pybricks/v1.0.0
I have not yet tested it ... but looks very promising to me ...
Look interesting. Let us know how the testing goes.
Does this still work? I have not been able to carry out this process i have this error
Running command git clone -q https://github.com/pybricks/pybricks-api 'C:\Users\ANGELA PATIÑO\AppData\Local\Temp\pip-req-build-ibu3fuen'
ERROR: Error [WinError 2] El sistema no puede encontrar el archivo especificado while executing command git clone -q https://github.com/pybricks/pybricks-api 'C:\Users\ANGELA PATIÑO\AppData\Local\Temp\pip-req-build-ibu3fuen'
ERROR: Cannot find command 'git' - do you have 'git' installed and in your PATH?
Cannot find command 'git' - do you have 'git' installed and in your PATH?
It requires that git
is installed on your computer and the path to git
is in the PATH
environment variable.
You can get git
at https://git-scm.com if you don't have it already.
I already downloaded the git, and added the path to the PATH, however the error persists, I don't know what to do.
It seems that I've done all the steps correctly per dlech above, with no errors, but still autocomplete does not work. I've verified pybricks 2.0.0 is installed with "pip list". Any ideas?
It seems that I've done all the steps correctly per dlech above, with no errors, but still autocomplete does not work.
In your project, go to .vscode/settings.json
.
Delete or comment out the lines about linting and the language server:
{
"files.eol": "\n",
"debug.openDebug": "neverOpen",
// "python.linting.enabled": false,
// "python.languageServer": "None"
}
Does that help?
Commenting out those lines in the settings.json worked! (After a restart and it complaining about not having a linter installed, but I’m ignoring those now.) Thank you!! The kids are much happier to have the auto complete working, it seems like we are spoiling them :-)
Thanks for getting back.
Those settings are there by default because for most users, this avoids the complexity of having to set it up. (Otherwise, users would always see red squiggly lines unless they install extras.)
But it looks like we should update the step by step guide above. (Or find a way to make it easier.)
I think I'm missing something simple, or perhaps I want too much...
My FLLers are doing great and cranking Pybricks code left and right, so its time to help them stay organized. Instead of a single flat file structure, I'd like them to organize something like this:
PROJECT ---|---lib ---|---|--- file.py ---|---|--- file2.py ---|--- missions ---|---|--- mission1.py ---|---|--- mission2.py ---|--- test ---|---|--- test1.py ---|---|--- test2.py ---|---main.py
from main.py, imports and autocomplete are intact. for example:
from lib.file import SomeClass
But when we try to import modules/symbols from lib into a parallel directory (like missions or test), issues appear. Attempts to use absolute paths like the one above generate "unable to open" complaints from pylint and attempts to use relative paths
from ..lib.robot import SomeClass
generate "Attempted relative import beyond top-level package" complaints from pylint.
Weirdly, autocomplete then works on instances of SomeClass in that file.
However, when a class in mission1.py inherits from a class in lib.file.py, autocomplete is dead again.
StackOverflow failed me on this one...thanks in advance for any pointers here
Does it help if you put empty __init__.py
files in all of the directories so that Python sees them as modules?
Like magic...thank you!
Also, another tip for those reading this thread. A couple of Python "type hints" strategically applied can make a huge difference for the kids. A team I coach created a "Robot" class that "knows" about their particular robot design and gave it lots and lots of useful methods for doing common actions. When they passed that into the __init__
method on their "mission" class, it lost it's typing information, and with it went autocomplete on all those methods... By adding the type hint:
def __init__(self, the_robot: Robot):
they are now back in their happy place.
@devinbreise Started coding for Mindstorms Inventor Hub very recently. I come from the Java world and the idea of having the whole code in a single file is terrifying. When organising your project in Python modules, how do you upload it to the Hub?
This topic was originally opened when there was only the EV3, which supports multi-file projects.
We don't support this yet on Pybricks for the MINDSTORMS Inventor Hub.
We do have a library and command line utility called pybricksdev that lets you run a script on the hub (without our online editor). If you really wanted to, it should be possible to write your own tool that merges multiple files into one and then uploads the result to the hub.
@laurensvalk That might be a decent workaround. Thanks!
@devinbreise Started coding for Mindstorms Inventor Hub very recently. I come from the Java world and the idea of having the whole code in a single file is terrifying. When organising your project in Python modules, how do you upload it to the Hub?
I had the same concerns. My solution was to extend pybricksdev with a pre-processor that can combine multiple files into one, by following the import statements. It worked pretty well and I used it a lot, but not for a few months. I did create a pull request to integrate it with the master branch (see https://github.com/pybricks/pybricksdev/pull/20) but I never managed to get it over the line to be merged. It looks as though it now needs some updates to be compatible with the latest pybricksdev. Feel free to adapt it if it would help you.
@thesynman Thanks for sharing this! From what I've seen the requested changes are simple cosmetics easy to fix. Or do I miss something that is more difficult to do to get it over the line (except for the compatibility issues)?
I've managed to set up my environment using IntelliJ Idea (PyCharm would work too). A run configuration allows me to upload and run a script with a single click. Autocomplete works. The only missing thing is the support for multiple-files projects.
If I'm not mistaken, micropython supports modules and imports between multiple files. From that perspective, it would be best if the pybricksdev
tool could upload a set of files (or a whole project directory) and execute a main.py
For example.
I want to use autocomplete functions for coding of EV3Micropython, but can't use. Is there are way to do it on VSCode?