Using Debuggingbook Code in your own Programs

This notebook has instructions on how to use the debuggingbook code in your own programs.

Can I import the code for my own Python projects?

Yes, you can! (If you like Python, that is.) We provide a debuggingbook Python package that you can install using the pip package manager:

$ pip install debuggingbook

Once this is installed, you can import individual classes, constants, or functions from each notebook using

>>> from debuggingbook.<notebook> import <identifier>

where <identifier> is the name of the class, constant, or function to use, and <notebook> is the name of the respective notebook. (If you read this at debuggingbook.org, then the notebook name is the identifier preceding ".html" in the URL).

Here is an example importing Debugger from the chapter on debuggers, whose notebook name is Debugger:

>>> from debuggingbook.Debugger import Debugger
>>> with Debugger():
    function_to_be_observed()

The "Synopsis" section at the beginning of a chapter gives a short survey on useful code features you can use.

Can I use the code from within a Jupyter notebook?

Yes, you can! Once the book is out of beta, you would first install the debuggingbook package (as above); you can then access all code right from your notebook.

Another way to use the code is to import the notebooks directly. Download the notebooks from the menu. Then, add your own notebooks into the same folder. After importing bookutils, you can then simply import the code from other notebooks, just as our own notebooks do.

Here is again the above example, importing Debugger from the chapter on debuggers – but now from a notebook:

import bookutils
from Debugger import Debugger
with Debugger():
    x = 1 + 1

If you'd like to share your notebook, let us know; we can integrate it in the repository or even in the book.

Can I check out the code from git and get the latest and greatest?

Yes, you can! We have a few continuous integration (CI) workflows running which do exactly that. After cloning the repository from the project page and installing the additional packages (see below), you can cd into notebooks and start jupyter right away!

There also is a Makefile provided with literally hundreds of targets; most important are the ones we also use in continuous integration:

  • make check-imports checks whether your code is free of syntax errors
  • make check-style checks whether your code is free of type errors
  • make check-code runs all derived code, testing it
  • make check-notebooks runs all notebooks, testing them

If you want to contribute to the project, ensure that the above tests run through.

The Makefile has many more, often experimental, targets. make markdown creates a .md variant in markdown/, and there's also make word and make epub, which are set to create Word and EPUB variants (with mixed results). Try make help for commonly used targets.

Can I just run the Python code? I mean, without Notebooks?

Yes, you can! (Although we'd always recommend executing the notebooks instead – they have all the text, and far richer inputs and outputs.) You can download the code as Python programs; simply select "Resources → Download Code" for one chapter or "Resources → All Code" for all chapters. These code files can be executed, yielding (hopefully) the same results as the notebooks.

The code files can also be edited if you wish, but (a) they are very obviously generated from notebooks, (b) therefore not much fun to work with, and (c) if you fix any errors, you'll have to back-propagate them to the notebook before you can make a pull request. Use code files only under severely constrained circumstances.

Which other Packages do I need to use the Python Modules?

We have attempted to limit the dependencies to a minimum (sometimes using ugly hacks). Generally speaking, if you encounter that a module X is not found, just do pip install X. Most notebooks only need modules that are part of the standard Python library.

For a full list of dependencies, there are two sources.

Step 1: Install Required Python Packages

The requirements.txt file within the project root folder; lists all Python packages required.

You can do

$ pip install -r requirements.txt

to install all required packages (but using pipenv is preferred; see below).

Step 2: Install Additional Non-Python Packages

The apt.txt file in the binder/ folder lists all Linux packages required.

In most cases, however, it suffices to install the dot graph drawing program (part of the graphviz package). Here are some instructions:

Installing Graphviz on Linux

$ sudo apt-get install graphviz

to install it.

Installing Graphviz on macOS

On macOS, if you use conda, run

$ conda install graphviz

If you use HomeBrew, run

$ brew install graphviz

Installing the Debuggingbook in an Isolated Environment

If you wish to install the debuggingbook in an environment that is isolated from your system interpreter, we recommend using Pipenv, which can automatically create a so called virtual environment hosting all required packages.

To accomplish this, please follow these steps:

Step 1: Install PyEnv

Optionally install pyenv following the official instructions if you are on a Unix operating system. If you are on Windows, consider using pyenv-win instead. This will allow you to seamlessly install any version of Python.

Install Pipenv following the official installation instructions. If you have pyenv installed, Pipenv can automatically download and install the appropriate version of the Python distribution. Otherwise, Pipenv will use your system interpreter, which may or may not be the right version.

Step 2: Install Python Packages

Run

$ pipenv install -r requirements.txt

in the debuggingbook root directory.

Step 3: Install Additional Non-Python Packages

See above for instructions on how to install additional non-python packages.

Step 4: Enter the Environment

Enter the environment with

$ pipenv shell

where you can now execute

$ make -k check-code

to run the tests.

Creative Commons License The content of this project is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. The source code that is part of the content, as well as the source code used to format and display that content is licensed under the MIT License. Last change: 2021-03-23 10:36:10+01:00CiteImprint