Skip to content

Installing Thonny, etc.

The purpose of this document is to walk you through setting up your own computer for writing and submitting Python code in CS 149.

Video Demonstration

This video walks you through the steps listed below: Install and setup Thonny video

Step 1: Create CS149 Folder

You will save yourself a lot of trouble this semester if you get into the habit of organizing your Python files into folders organized by assignment. We recommend creating a top-level folder named CS149 that you can use to store assignment, labs, notes, etc.

Instructions

Create your CS149 folder in the same place where you store other files for school. A good place might be in your home folder, or Desktop folder, or Documents folder. Do not create CS149 in the Downloads folder; that will likely lead to confusion later on.

Step 2: Download Thonny

Thonny, pronounced THON-ee, is the officially supported Integrated Development Environment (IDE) for CS 149. Thonny is free and open source, originally developed by Aivar Annamaa at the University of Tartu in Estonia. Many other individuals and organizations have contributed to the ongoing development of Thonny.

Note

You can skip this step in the computer lab, because Thonny is already installed.

Instructions

Visit https://thonny.org/ and download the installer based on your operating system. Run the installer, and accept the default options during installation.

Step 3: Virtual Environment

A Python virtual environment ("venv") is a folder for installing Python tools and packages for a specific project. Normally, if you install a library, it gets added to your whole computer, which can cause problems if different projects need different versions of the same library. A venv solves this by creating a folder that acts like its own mini-Python setup, with its own packages. This way, your projects stay organized, independent, and don’t interfere with each other.

Warning

On macOS, you likely need to follow these steps to show hidden files (or it won't let you create a hidden directory):

  1. Open Finder (look in your doc for what is usually the first icon)
  2. Tell finder that you would like to show hidden files by pressing command+shift+.

Instructions

To create a venv in Thonny:

  1. Click the Tools menu (at the top) and click on Options.
  2. Click the Interpreter tab, and click the "New virtual environment" link (bottom right).
  3. Navigate to your CS149 folder (which should be empty), and create a new folder inside of CS149 named .venv (the dot makes the folder hidden).
    • If you're on macOS, you may need to again press the keys in the warning above.
  4. Press OK to select CS149/.venv as your virtual environment.

Step 4: Install Ruff in Thonny

Ruff is a linter and code formatter for Python. A linter is a tool that checks for style defects and common programming mistakes. Ruff will help you learn to write clean and professional code.

Instructions

  1. To install Ruff, type the following command in the Shell (at the bottom of Thonny):

    !pip install ruff
    

  2. Right-click and save ruff.toml in your CS149 folder. When you run Ruff anywhere under CS149, this file will be used to know what rules to enforce.

Using Ruff

You can run Ruff in the Shell by typing the command:

!ruff check

To reformat your code, you can type the command:

!ruff format

Step 5: View Files in Thonny

In Thonny, click the View menu (at the top) and click on Files. Doing this will open a file browser on the left side of Thonny. At this point, you should be in your CS149 folder. The only file you should see is ruff.toml, as shown in the screenshot below. Depending on your operating system, you might also see the hidden .venv folder.

Screenshot of Thonny's files

Note

If you see folders like bin, include, and lib, that means you created your venv in the wrong place. In that case, delete all files except for ruff.toml, and redo Steps 3–4 above. Make sure during Step 3 to create a new folder named .venv under CS149.

Step 6: Force Color in Thonny

Tools like ruff display their output in color, which makes the output easier to read. However, when running in Thonny, tools often can't detect whether color is supported.

Instructions

In Thonny, click the Tools menu (at the top) and click on Options. Click the General tab and enter the environment variable FORCE_COLOR=1 as shown in the screenshot below. Then close and reopen Thonny for the change to take effect.

Screenshot of Thonny's options

Step 7: Show File Extensions

When naming a file, a longstanding convention is to use a file extension to indicate the file's type. For example, a Windows executable might be named installer.exe, while a Word document might be named FinalReport.docx. In this case, .exe and .docx are the extensions indicating the file types. Python programs end with the extension .py.

In the good old days, these file extensions were visible by default, and they were often helpful in figuring out file types. Unfortunately, file extensions could also be confusing and intimidating for non-technical users. For example, .avi, .mp4, .mpg, .divx, .mov (among many others) are all file extensions that represent different kinds of video files. Understanding the differences among these extensions is crucial to someone involved in video production, but irrelevant to someone who just wants to watch the video. In the interest of not confusing people, modern operating systems often hide file extensions by default.

Now that you are studying computer science, you have become the kind of person who needs to pay attention to file extensions. Congratulations! This means that you should modify the settings on your computer to show file extensions by default. Follow the instructions in the following links to change the appropriate settings on your computer.

Note

You can skip this step in the computer lab, because Linux shows file extensions by default.