Discover bpython: A Python REPL With IDE-Like Features – Real Python
Discover bpython: A Python REPL With IDE-Like Features by Bartosz Zaczyński Updated Jan 04, 2023 Reading time estimate 47m intermediate editors tools Table of Contents Remove ads Recommended Course Using the bpython Enhanced REPL (51m) The standard Python interpreter lets you run scripts from files or interactively execute code on the fly in a so-called read-evaluate-print loop (REPL) . While this is a powerful tool for exploring the language and discovering its libraries through instant feedback on your code inputs, the default REPL shipped with Python has several limitations.
Luckily, alternatives like bpython offer a much more programmer-friendly and convenient experience. You can use bpython to experiment with your code or quickly test an idea without switching contexts between different programs, just like in an integrated development environment (IDE) . In addition, bpython may be a valuable teaching tool in either a virtual or physical classroom. In this tutorial, you’ll learn how to: Install and use bpython as your alternative Python REPL Boost your productivity thanks to bpython’s unique features Tweak bpython’s configuration and its color theme Use common keyboard shortcuts to code more quickly Contribute to bpython’s open-source project on GitHub Before starting this tutorial, make sure you’re already familiar with Python basics and know how to start the standard Python REPL in the command line.
In addition, you should be able to install packages with pip , ideally into a virtual environment . To download the configuration files and sample scripts that you’ll use in this tutorial, click the link below: Source Code: Click here to download the free source code that you’ll use to harness the power of bpython. Get Started With bpython Unlike stand-alone Python distributions, such as CPython , PyPy , or Anaconda , bpython is merely a pure-Python package serving as a lightweight wrapper around a chosen Python interpreter.
Therefore, you can use bpython on top of any particular Python distribution, version, or even a virtual environment, which gives you plenty of flexibility. Note: The letter b in bpython stands for Bob Farrell , who’s the original author and maintainer of the tool. At the same time, bpython remains a familiar Python REPL with only a few essential features, such as syntax highlighting and auto-completion, borrowed from the full-fledged Python IDEs .
This minimalistic approach contrasts with tools like IPython , which is yet another alternative to the standard Python REPL, popular in the data science community. IPython introduces a lot of custom commands and other extras that are unavailable in vanilla Python. There are a few ways to get bpython on your computer. Package managers like Homebrew or APT offer pre-built versions of bpython for your operating system. However, they’re likely obsolete and hardwired to the system-wide Python interpreter.
While you can build the latest bpython version from its source code by hand, it’s better to install it into a virtual environment with pip : Language: Shell (venv) $ python -m pip install bpython It’s common to have bpython installed in several copies across many virtual environments, and that’s fine. This allows you to wrap bpython around the specific Python interpreter that you used to create the virtual environment in the first place.
Note: Unfortunately, bpython isn’t natively supported on Windows because it depends on the curses library , which is only available on Unix-like systems, such as macOS and Linux. The official documentation mentions a work-around , which relies on an unofficial binary for Windows, but it seems not to work anymore. If you’re on Windows, then your best bet is to install the Windows Subsystem for Linux (WSL) and use bpython from there.
Once it’s installed, you can start bpython using either of these two commands: bpython python -m bpython It’s preferable to choose the more explicit second command, which invokes bpython as a runnable Python module . This way, you’ll ensure that you’re running the bpython program installed into the currently active virtual environment. On the other hand, using the bare bpython command could silently fall back to the program installed globally, if there is one.
It could also be aliased to a different executable in your shell, taking precedence over the local bpython module. Here’s an example illustrating the use of bpython against a few different Python interpreters encapsulated within isolated virtual environments: Language: Shell (py2.7) $ python -m bpython bpython version 0.20.1 on top of Python 2.7.18 ⮑ /home/realpython/py2.7/bin/python WARNING: You are using `bpython` on Python 2.
Support for Python 2 ⮑ has been deprecated in version 0.19 and might disappear ⮑ in a future version. >>> import platform >>> platform.python_version() '2.7.18' >>> platform.python_implementation() 'CPython' (py3.11) $ python -m bpython bpython version 0.23 on top of Python 3.11.0 ⮑ /home/realpython/py3.11/bin/python >>> import platform >>> platform.python_version() '3.11.0' >>> platform.python_implementation() 'CPython' (pypy) $ python -m bpython bpython version 0.23 on top of Python 3.9.12 ⮑ /home/realpython/pypy/bin/python >>> import platform >>>> platform.python_version() '3.9.12' >>> platform.python_implementation() 'PyPy' Notice that you use the same command to run bpython from different virtual environments.
Each highlighted line indicates the interpreter version and a path to the Python executable that bpython wraps in the current REPL session. You can confirm the Python version and its implementation through the platform module from the standard library. Note: The Django web framework can detect bpython if it’s installed in your virtual environment. The framework will automatically run bpython when you execute the shell command to bring up the Python interactive interpreter with your project files on the module search path.
Okay, now that you’ve learned how to install and run bpython as an alternative Python REPL , it’s time to explore its key features. Over the next few sections, you’ll discover several ways that bpython can increase your productivity as a Python programmer, regardless of your skill level. Remove ads Spot Typos at a Glance Compared to bpython, Python’s standard REPL is like an old black-and-white TV set. It does the job of accurately conveying information, but sometimes you wish to see things in color for greater clarity.
That’s particularly important during code editing, where every detail matters. Therefore, syntax highlighting and bracket matching are perhaps the most common features found in any decent IDE or code editor. In bpython, you get both features out of the box, even though it’s only a text-based user interface (TUI) to the Python REPL. Colorful syntax highlighting helps you identify the structure of your code at a glance, while bracket matching makes it easier to keep the opening and closing brackets correctly balanced.
Read on to see those features in action. Syntax Highlighting As you type code into bpython, your instructions get tokenized into Python keywords , operators , comments , variables , and literal values like strings , numbers , or Booleans . Each token type has an associated color to let you quickly see what kind of language construct you’re working with: This tokenizing and coloring isn’t done by bpython directly, but by the Pygments library used under the surface.
Later, you’ll learn how to customize the color theme in bpython. Bracket Matching In addition to providing syntax highlighting, bpython also lets you know if the opening and closing brackets in your code are correctly balanced. When you type a closing bracket, bpython will highlight the corresponding opening bracket and the other way around: This works with different kinds of brackets in Python, including round brackets ( () ), square brackets ( [] ), and curly
The rest of this page is on the live site.