Raspberry Pi System Getting Started Tutorial (3) - Use VSCode on Windows to remotely connect to Raspberry Pi for Python development

Raspberry Pi System Getting Started Tutorial (3) - Use VSCode on Windows to remotely connect to Raspberry Pi for Python development


Many developers prefer to write code on a big screen and a powerful development environment, but at the same time they want to be able to execute and test the code on a tiny computer like the Raspberry Pi. VSCode provides a powerful remote development tool that allows you to write code on the Windows host and execute it on the Raspberry Pi at the same time. The following will give you a detailed introduction on how to achieve it:

1. Install VSCode and SSH extensions

First, make sure you have VSCode installed on Windows .

Then, in VSCode, click on the extension icon in the left sidebar, search for "Remote - SSH" and install it. This is an extension officially provided by VSCode that supports remote development via SSH.

2. SSH connection configuration

  1. Open VSCode, click the green icon in the lower left corner, and select "Remote-SSH: Connect to Host...".
  2. Click "+ Add New SSH Host".
  3. Enter the SSH connection information: pi@树莓派的IP地址, the default user is pi. Then hit enter.
  4. Select an SSH configuration file to save this connection information, it is recommended to save it in the default one ~/.ssh/config.
  5. When prompted to choose an SSH private key file, choose "Skip" if you haven't set one up before.

3. Connect to Raspberry Pi

  1. Click the green icon in the lower left corner of VSCode again.
  2. In the pop-up list, select the SSH connection you just configured, ie pi@树莓派的IP地址.
  3. Enter the password for the Raspberry Pi (which is the default raspberry) and press Enter.

You should now be successfully connected to your Raspberry Pi via SSH. The left sidebar of VSCode will display the file system of the Raspberry Pi, and you can start creating or editing Python files.

4. Run the Python program

  1. Create a new Python file, eg hello.py, and enter the code print("Hello, Raspberry Pi!").
  2. Open a new terminal (Terminal > New Terminal), and you will see that this is already the terminal environment of the Raspberry Pi.
  3. Type in the terminal python3 hello.pyto run your Python program.

5. Recommendations and Precautions

  • In order to better support Python development in VSCode, it is recommended to install Pythonthe extension.

  • Using the remote development function of VSCode, you can run and debug directly on the Raspberry Pi, which is very convenient.

  • Make sure the Raspberry Pi's SSH service is enabled and can connect to the network.

Conclusion :
VSCode's Remote - SSH extension provides developers with a very powerful tool that allows you to write code in your favorite development environment, while using the Raspberry Pi's hardware and software environment for testing and running. This way of working is ideal for developers who want to combine the development capabilities of a Raspberry Pi and a desktop PC.

Guess you like

Origin blog.csdn.net/qq_41866091/article/details/132353310