Skip to content

Commit 99f7b8b

Browse files
committed
tutorials/dev/tools/vscode: finish writing
1 parent 807e337 commit 99f7b8b

File tree

1 file changed

+133
-2
lines changed

1 file changed

+133
-2
lines changed

tutorials/dev/tools/vscode/index.md

Lines changed: 133 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,138 @@ description:
99
"Configure Visual Studio Code to use Pybricks."
1010
---
1111

12-
# What/Why/How
12+
## Introduction
1313

14-
TODO
14+
[Visual Studio Code] is a free code editor available from Microsoft. For
15+
new users we recommend using [Pybricks Code] instead. However, if you are an
16+
advanced user that prefers VS Code, read on.
1517

18+
[Visual Studio Code]: https://code.visualstudio.com
19+
[Pybricks Code]: https://code.pybricks.com
20+
21+
## Creating a new project
22+
23+
If you are using Pybricks on LEGO® MINDSTORMS® EV3, we recommend using the
24+
official [LEGO® MINDSTORMS® EV3 MicroPython][ev3-ext] extension for VS Code.
25+
Everything you need to know from how to install the extension to how to create
26+
a new project is detailed in the [official docs][ev3-docs].
27+
28+
For Powered Up hubs, you can create a new project by simply creating a new,
29+
empty folder on your computer and opening that folder in VS Code.
30+
31+
[ev3-ext]: https://marketplace.visualstudio.com/items?itemName=lego-education.ev3-micropython
32+
[ev3-docs]: https://pybricks.com/ev3-micropython
33+
34+
## Code completion
35+
36+
To get code completion/intellisene working in VS Code you will need to install
37+
the [Pylance] extension and the [pybricks] Python package (preferably in a
38+
virtual environment).
39+
40+
[Pylance]: https://marketplace.visualstudio.com/items?itemName=ms-python.vscode-pylance
41+
[pybricks]: https://pypi.org/project/pybricks
42+
43+
### Installing Python and the Pylance extension
44+
45+
To use Pylance, you will need [Python] installed on your computer. You can
46+
install Python from the [official site][py-dl], or using your favorite package
47+
manager. On Linux, Python is most likely already installed.
48+
49+
Then follow the link above or search for "Pylance" in the *Extensions* in VS Code
50+
and click *Install* to install the *Pylance* extension.
51+
52+
[Python]: https://www.python.org
53+
[py-dl]: https://www.python.org/downloads
54+
55+
### Setting up a virtual environment
56+
57+
Once *Python* and *Pylance* are installed, you can use VS Code to set up an
58+
isolated *virtual environment*.
59+
60+
- Open your project folder in VS Code.
61+
- If you created your project using the *LEGO® MINDSTORMS® EV3 MicroPython*
62+
extension, you will need to edit the `.vscode/settings.json` file and change
63+
`"python.languageServer": "None"` to `"python.languageServer": "Pylance"`
64+
and save the file, otherwise skip this step.
65+
- Use <kbd>F1</kbd> or <kbd>CTRL</kbd>+<kbd>SHIFT</kbd>+<kbd>P</kbd>
66+
(<kbd>⌘</kbd>+<kbd>⇧</kbd>+<kbd>P</kbd> on macOS) to open the command palette
67+
in VS Code.
68+
- Type in "py create env" to search for *Python: Create Environment* and select
69+
that option.
70+
- It will ask you to "Select and environment type". Choose *Venv*.
71+
- It will ask you to "Select Interpreter". Choose the one you want. If you
72+
aren't sure, choose the one that says *Global*.
73+
- There will now be a new subfolder in your project named `.venv` that contains
74+
the virtual environment and VS Code should set it as the interpreter to use
75+
for your project.
76+
- To use the virtual environment, open the command pallette again and search
77+
for "py create term" and select *Python: Create Terminal*.
78+
- This should open a new terminal and present a prompt that starts with `(.venv)`,
79+
if all when well.
80+
81+
The equivalent command line invocation of steps above is:
82+
83+
# macOS/Linux
84+
python3 -m venv .venv
85+
. .venv/bin/activate
86+
87+
<!--></!-->
88+
89+
# Windows PowerShell
90+
py -3 -m venv .venv
91+
.venv/scripts/activate
92+
93+
### Installing the pybricks package
94+
95+
Once you have a `(.venv)` prompt as described in the previous section, you can
96+
install the `pybricks` package by typing the following in the terminal with the
97+
`(.venv)` prompt:
98+
99+
pip install pybricks
100+
101+
If you are using Pybricks v2.0 with EV3, type this instead to get the correct
102+
version:
103+
104+
pip install "pybricks<3"
105+
106+
Then you need to restart the Python language server to pick up the new package.
107+
In the command pallette, search for "py restart" and select *Python: Restart
108+
Language Server*.
109+
110+
Now code completion and intellisense should be working. You can try it by
111+
opening an existing file and hovering over text to see the relative documentation
112+
or you can create a new `.py` file and start typing `from pybricks.` and see
113+
suggestions on what comes next.
114+
115+
## Downloading and running programs
116+
117+
If you are using Pybricks on EV3, then refer again to the [official docs][ev3-docs]
118+
to learn how to use the Debug adapter from the *LEGO® MINDSTORMS® EV3 MicroPython*
119+
extension to download and run your programs.
120+
121+
For Powered up hubs, you must use the [pybricksdev] command line tool instead.
122+
123+
Install the `pybricksdev` package in the virtual environment:
124+
125+
pip install pybricksdev
126+
127+
Then run the following command (replacing `my_program` with the actual name
128+
of the program you want to run).
129+
130+
pybricksdev run ble my_program.py
131+
132+
If you have more than one active hub, you can specify a specific hub by name:
133+
134+
pybricksdev run ble --name "my hub" my_program.py
135+
136+
137+
[pybricksdev]: https://pypi.org/project/pybricksdev
138+
139+
### Common mistakes
140+
141+
Clicking any of the "run" buttons in VS Code (other than the one mentioned in
142+
the EV3 docs) will try to run the program on your computer instead of downloading
143+
and running it on the hub. When you do this, it may appear as nothing happened
144+
or if you didn't install the `pybricks` package, you might get an error that
145+
the `pybricks` package could not be found. Be sure you follow the steps above
146+
to download and run a program.

0 commit comments

Comments
 (0)