Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Configure bpython Settings

00:00 Configure bpython. The regular Python REPL doesn’t give you much flexibility when it comes to customizing it, but bpython has a host of options that you can modify by editing a text file.

00:12 bpython relies on defaults that are initially hard-coded in its source code. It’s only when you request to edit the configuration by pressing F3 in bpython that it will create the file from scratch and open it for you in a code editor.

00:34 This file resembles a Windows INI file with key-value pairs grouped by categories. These are processed by bpython using the configparser module. After saving the updated configuration, you’ll need to restart bpython for the changes to take effect.

00:51 As seen previously in the course, the default is to edit in vi, so you’ll need to exit it by pressing Escape than :wq to save the file and return to bpython.

01:02 It’s often easier to edit the configuration file in a different editor, so you’ll need to know the location of the configuration file to do this. The default location of the config file is a hidden subdirectory of the user’s home folder, which can be accessed at the location seen on-screen, on macOS and Linux and also under WSL on Windows.

01:23 You can also specify an alternative configuration file when starting bpython on the command line.

01:34 This can be useful if you want to have different configurations for different projects. The command to edit the file in nano or pico, which is available on macOS, Linux, and WSL, is seen on-screen.

01:52 It’s good to know the default location of the configuration file in case it has an error preventing bpython from starting. When it does, you can open the file in a text editor and fix the problem by hand.

02:05 While there are many options available to you, in the next few sections, you’ll go through the most important bpython settings. One of the first things you might want to change to improve your bpython experience is the external code editor, which defaults to the text-based vi.

02:21 For example, to make bpython open Visual Studio Code instead, find the editor option under the [general] section tag and set it to the value seen on-screen. The

02:48 --wait flag is necessary to make VS Code wait for the files to close before returning. Otherwise, bpython wouldn’t see any changes in the session to apply.

02:58 You may also increase the number of lines to store in the history file by bumping up the hist_length option, which is normally limited to 100.

03:11 Because bpython expects the configuration values to be valid Python literals, make sure that you type an integer literal for this option. You can use the underscore character to visually separate groups of digits.

03:25 For other configurable options available in bpython, including those that bpython may not generate when you first edit the configuration file, check out the configuration page in the official documentation at the link seen on-screen. The next section that you’ll find in bpython’s configuration file is tagged as [keyboard].

03:46 It represents keyboard shortcuts bound to specific actions such as clearing the screen or showing the source code preview. You can define custom key bindings using the syntax seen on-screen, but how do you know the names of the options that correspond to the available actions in bpython? Fortunately, the generated configuration file contains commented-out mappings of actions and their default keyboard shortcuts. You can uncomment and update them.

04:14 For example, to avoid a conflict with a common global hotkey for pausing the terminal, you can remap the shortcut for the save action to F4.

04:28 Don’t forget you’ll need to restart bpython after saving the configuration file to make the changes effective. The configuration file is read only once, and that’s when bpython starts.

04:42 Many code editors allow you to change the color theme. This can help reduce eyestrain by letting you switch between light and dark themes to adapt to the lighting conditions in your environment. Also, if you get bored with a default color theme or require greater contrast, you can always choose a theme that better suits your needs.

05:01 To customize the colors for syntax highlighting in bpython, you must create a .theme file placed next to the configuration file in your user-specific configuration directory.

05:12 To tell bpython which theme file to use when it starts, you should set the color_scheme option in the configuration file.

05:32 Note that you don’t include the .theme suffix here because bpython always appends it to the specified file stem. Now you can edit your custom theme file, which in this case on-screen has contents inspired by the sample light theme available on bpython’s GitHub repository.

05:59 The only difference from the theme file on GitHub is the background line, which uses the letter d for a transparent background instead of w for white, which some terminals render incorrectly.

06:11 Here you can see how the bpython REPL looks when you apply the custom theme.

06:26 Unfortunately, there’s only a small set of fixed color markers that you can choose from, and they’re seen on-screen. On the upside, you can make the font bold by using uppercase letters, so there’s a tiny bit of room for variety and creativity. For example, the uppercase letter Y will make your text yellow and bold.

06:47 Now that bpython is customized to your liking, you can start using it as a debugging tool, and that’s what you’ll see in the next section of the course.

Become a Member to join the conversation.