Thonny Python IDE

repository·master·Indexed 26 days ago

https://github.com/thonny/thonny

A beginner-friendly Python IDE designed to help users learn programming concepts. Thonny supports various microcontrollers through dynamic board support configuration via JSON files and provides a specialized interface for beginners. The project includes documentation on Linux installation, Flatpak builds, macOS bundling, and a collaborative translation process using POEditor and Babel.

Tokens
28.1K
Snippets
39
Records
222
Agent score
82%

What's inside Thonny

  1. Overview of Thonny IDE

    master
    Thonny is an Integrated Development Environment (IDE) specifically designed for learning Python programming. It is intended for beginners to help them understand programming concepts through its specialized interface and features.
  2. Use Assistant for error explanation and code investigation

    master

    The Assistant view in Thonny provides two primary functions for debugging:

    1. Error Explanation: When a program terminates with an error, the Assistant attempts to explain the error in simpler terms and provides advice for locating and fixing the underlying issue.
    2. Code Investigation: For logic errors where no explicit error message is thrown, the Assistant suggests using Pylint and Mypy to identify 'bad smells' or contradictions in the code.
    • Mypy: Detects type contradictions (e.g., passing a string to a function expecting an integer).
    • Pylint: Performs various code quality checks. Thonny includes a curated selection of relevant checks for beginners.
  3. Understand how Thonny updates board support via JSON files

    master

    Thonny uses JSON files located in the data/ directory to manage board variants. Because Thonny consults these files online (e.g., via GitHub), users can benefit from updates to board support and variants without needing to install a newer version of the Thonny software itself.

    • Modern Thonny versions (4.0b4 and later): Use circuitpython-variants-* and micropython-variants-* files.
    • Older Thonny versions: Use other JSON files in the directory.

    Note for contributors: The circuitpython-variants-* and micropython-variants-* files are automatically generated by scripts. Do not edit them directly; if changes are required, please create a GitHub issue first.

  4. Use Birdseye for post-execution value exploration

    master

    Birdseye operates differently from the built-in Thonny debugger. Instead of step-by-step execution with breakpoints, you run the script using Run → Debug current script (Birdseye).

    Note the following behavior:

    • Execution takes longer than a normal run.
    • Breakpoints are ignored.
    • You cannot step through the code manually.
    • Once execution completes, Thonny opens a web page (served by a local Birdseye server) where you can inspect the execution process and how intermediate values were computed.
    • Important: You do NOT need to import birdseye.eye or decorate your functions manually; Thonny handles the recording automatically.
  5. Use Expert mode

    master

    Expert mode provides additional features useful for teaching and advanced workflows:

    • Full Screen: Access via the View menu to enter full-screen mode.
    • Maximize Editor/View: Double-click an editor or view tab to maximize it. Press ESC to unmaximize.
    • Action Repeater: Access via the Tools menu (Tools → Open repeater...) to repeat user action logs.
  6. Perform manual code review for debugging

    master

    When a program fails, you can perform a manual code review by mentally tracing the logic. Focus on the following elements:

    Variables

    • Does the name reveal its purpose?
    • What type of values does it hold (e.g., strings, integers, lists)?
    • What is its function (e.g., updating repeatedly, storing shared information)?

    Loops

    • Is the loop necessary?
    • How many times should it execute and what does it depend on?
    • Which code belongs inside vs. outside the loop?
    • What setup/cleanup is required before and after the loop?

    Complex Expressions

    • What is the evaluation order, and does it match Python's behavior?
    • What type of value should the expression return?

    Missing Logic

    • If different situations require different handling, you may need an if statement.
    • If a task needs to be repeated, you may need a loop.
  7. Update Flatpak Python dependencies

    master

    Thonny's Flatpak dependencies are managed using the Flatpak Pip Generator. This tool generates JSON files for Python packages to be included in the Flatpak manifest's modules section.

    Note: If org.freedesktop.Sdk//20.08 is installed in both user and system installations, the generator may fail. It is recommended to temporarily remove one of them.

  8. Validate the Thonny AppData file

    master

    Before releasing a new version of Thonny on Linux, you must update the org.thonny.Thonny.appdata.xml file with the new version, date, and a description of changes. Use appstreamcli to validate the XML file to ensure it is correctly formatted for app stores.

    # Using system package manager
    sudo apt install appstream
    appstreamcli org.thonny.Thonny.appdata.xml
    
    # Using Flatpak
    flatpak remote-add --user --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo
    flatpak install --user flathub org.freedesktop.appstream-glib
    flatpak run org.freedesktop.appstream-glib validate org.thonny.Thonny.appdata.xml
  9. Use "Faster" debug mode for larger programs

    master

    For larger programs where the "Nicer" mode's overhead (like expression stepping and step-back capabilities) becomes too slow, use "Faster" mode.

    This mode uses a more traditional debugging style. You can use Step over, Step into, Step out, and Resume, but you cannot use Step back.

  10. Debug programs using print statements and Thonny's debugger

    master

    When a program is not working as expected, use these techniques to identify the cause:

    1. Print Debugging: Add print() statements to track program execution and inspect variable states at specific points. This helps verify if Python has reached a certain line and what the current values of variables are.
      • If an expression is complex, break it down into smaller parts or introduce new variables to print them individually.
    2. Step-by-step Execution: For a more powerful approach, use Thonny's core feature: the debugger. This allows you to execute code line-by-line to observe the program flow and state changes in real-time.

    Always aim to diagnose the exact reason for a malfunction before attempting to fix the code.

    print("friends before for-loop", friends)
  11. Anchor user windows for Turtle or Tkinter programs

    master

    When developing programs using Turtle or Tkinter, you can use the Ancrer les fenêtres utilisateur (Anchor user windows) option in the menu Lancer (Run menu) to manage window placement.

    When this option is enabled, Thonny performs the following:

    • Remembers window positions: Thonny saves the locations of your windows and places them in the same position during subsequent runs.
    • Maintains window focus: It ensures your Tkinter window stays on top. After the Tkinter window appears, Thonny automatically returns focus to the Thonny editor window so you can continue editing code immediately without using the mouse.

    To run a new version of your program and replace the existing window, simply press F5 again.