Skip to main content
Chemistry LibreTexts

3.9: Thonny

  • Page ID
    469543
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    The RPi (Raspberry Pi) comes with the Thonny IDE (Interactive Development Environment) installed on it and so it is simplest to install the Thonny IDE on your laptop. As of December 2022 the latest version of Thonny is Thonny 4.0.1, which will also instal Python 3.10.6, and so you do not need to install python.  If you are planning on doing machine learning or advanced data science applications you may want to install the Anaconda Package, which helps you update multiple packages without causing conflicts. But unless you are an advanced user, Thonny should be fine. 

    Download Thonny at https://thonny.org/

    Thonny is now the default IDE for *.py and you can open it from either the start menu or by clicking on a *.py file.  If you want, you can also pin it to the taskbar.

    UI Modes 

    Thonny has three UI (User Interface) modes, Simple, Regular and Expert.  We will typically use the Simple or Regular mode.  On your PC you will probably want Regular as it has access to the file menu (fig. \(\PageIndex{2}\)), but if you have limited screen space, like are on a Pi with a low resolution monitor, you may wish to use simple mode to maximize the screen's realestate.

    clipboard_e06829b6defa094862fc1c1ca9dfa080c.pngFigure \(\PageIndex{1}\): Thonny UI in SIMPLE MODE, note there is no menu bar (see image below) and this installation is running python 3.7.9.  (Copyright; Belford, CC 0.0)

    To switch to REGULAR mode you can click the text in the circle of the upper right in figure \(\PageIndex{1}\). To switch from regular mode to simplemode go to tools/options/general/UI mode and pick the option you want (figure \(\PageIndex{2}\)).  Also, note that the blue oblong circle on the shell of figure \(\PageIndex{1}\) shows that Thonny is running python 3.7.9, which was co-installed with Thonny, but in \(\PageIndex{2}\) we can see that Python 3.9.7 is being run. That is, these images were taken when we were using different versions of Python, and in the next section you will install Python 

    clipboard_eb30c58169d628ce475e9f741473f4d57.pngFigure \(\PageIndex{2}\): Thonny UI in regular mode.(Copyright; Belford, CC 0.0)

    To switch from regular mode to simple mode  choose Tools/General/UI mode/regular (Figure \(\PageIndex{2}\)). We will typically use the Regular mode.

     

    When in Regular mode you have access to several menu items and it is prudent to go over these before starting.  Many of these like open and save will be familiar to you while others will be unfamiliar.  It is always a good idea to check out menu items on any software package before you start using it.

    clipboard_e9b0beaf8f109e94bfe6358290dee9d75.pngFigure \(\PageIndex{3}\): Different menu options, go ahead and exlore (Copyright; Belford cc0.0)

     

    We will take a quick dive on several of the menu item features that your are probably not familiar with.

    Edit

    In some IDEs you need to save a file before you run it.  In Thonny you do not, and the moment you hit "RUN" it saves the file and then runs it.  This means it overwrites the code of the original file. So, a common error is to change some code and run it to see if it works, but you then lose the original code.  A good practice is to comment out code with the # symbol.  This means that text will not be operated on.  In the Edit menu you have the ability to comment or uncomment large blocks.

    clipboard_e59164b869338589bdf60d2bb7589fdf0.pngFigure \(\PageIndex{4}\): By clicking comment out in the Edit menu we were able to prevent lines 4-8 from being run. We could then try some other script, and if it did not work, we could simple uncomment these in the Edit menu, and not lose the code. It is very important to realize that in Thonny saves code the moment you run it, so if you edit something and run it, you lose the original code. (Copyright; Belford CC 0.0)

    Note, the color coding above is a result of the option chosen in the tools/options/theme-fonts tab.

    View

    The View option allows you to select the interfaces you wish to work with. The code editor is always present and you probably want to run the Shell, which emulates a command line interface. In figure (\(\PageIndex{3}\)) the Shell, Variables and Assistant options have been opened. We will go over the utility of these features as the course proceeds, but these are probably the most common ones you will wish to work with.

    clipboard_e86c06adfb755656990424bd87ad31509.pngFigure \(\PageIndex{3}\): View menu options for Thonny UI. (Copyright; Belford, CC 0.0)

    You will also note that the View menu provides an option to increase or decrease font size with keyboard shortcuts of Ctrl + + and Ctrl + - (click the crtl followed by either plus or minus).  This is often very important when you are running a Pi in headless mode using the VNC viewer.

    Run

    The run tab allows you to execute python code in the editor and interact with it through the shell. You will often want to use the <F5> shortcut key.  You can also run the program in "debugger mode" that allows you to walk through the code one step at a time.  There are a lot of YouTube videos on the debugger and the following one goes over it (we are starting where the debugger part of the script is, but you may want to watch the entire video.

     

     

    Tools

    Tools is an important menu item, in fact we have already looked at in in figure \(\PageIndex{2}\) when we switched the UI (User Interface) from simple to regular mode. Let's quickly look at some of the other features of the Tools, starting with the manage packages and manage plugins.  A Python package is a way to add new features to your program, like say math or time functions, and is essentially a directory of modules that you typically download from a service like the PyPI Python Package Index, that as of 1/22/2022 contains 351,947 projects. A package has a init_.py file sets how the packages are imported, while a directory could be a collection of python modules, but without the init_.py file, they will not be integrated into the interpreter.

    clipboard_eb47455ae51ba9dd19c11d418f0ebf8da.pngFigure \(\PageIndex{6}\): If you are running the default python interpeter that comes with Thonny you can manage packages from this menu.  If you are using a different interpreter, say the one from Anaconda (see below), you would not use this option.  The manage packages allows you to install new packages while the manage plug-ins is used for managing existing plug-ins. . (Copyright; Bob Belford, CC 0.0)

    We have already looked at the options submenu when we changed the UI (Figure \(\PageIndex{2}\).

    clipboard_eb1057b9a04eaaf80f846ddc5ebc37ff7.pngFigure \(\PageIndex{7}\): Editor and "Theme & Font" submenu items of the "options" item of tools overlaid on the IDE. (Copyright; Belford CC 0.0)

    PEP 8, the Index of Python Enhancement Proposal style guide suggests a maximum line length of 79 characters and you can set a verticle line in your editor to warn you when your string of code is getting to long (figure \(\PageIndex{7}\)).

     

    Editor Options

    Under the regular UI you can go to Tools/Options/Editor and do things like insert/remove line numbers, set the reccomended line length (standard python libraries limit line length to 79 characters with comments being recommended to be limited to 72)

    clipboard_e9c20e5bf301143f94e3d0c7fa5f514cc.png
    Figure \(\PageIndex{3}\): Editor options under Tools/Options/Editor of normal UI. (Belford CC 0.0)

     

    Installing Modules

     

     

    clipboard_edc669ba58fa10ba2be84dbb8ae565331.pngFigure \(\PageIndex{1}\): Installing modules on Thonny. (Belford CC 0.0)

    3.9: Thonny is shared under a not declared license and was authored, remixed, and/or curated by LibreTexts.

    • Was this article helpful?