Debug circular imports in python

CPython in versions 2 and 3 contains a commandline option which allows you to debug circular imports more easily.

Given are 2 files which are based on a real-world scenario:

The first file is called a.py and represents the main entry point for some web application:

from b import db

app = None

def main():
    global app
    # db.connect()
    app = type('Application', [], {'run': lambda self: 0})
    print("Hello World!")

if __name__ == '__main__':
    main()

The second file is called b.py and represents some submodule handling the database.

from a import app

db = object()

if __name__ == '__main__':
    app.run()

Obviously every file requires the other one (see import statements). If we now run python a.py we get (also in python3):

Traceback (most recent call last):
  File "a.py", line 1, in <module>
    from b import db
  File "/home/meisterluk/b.py", line 1, in <module>
    from a import app
  File "/home/meisterluk/a.py", line 1, in <module>
    from b import db
ImportError: cannot import name db

The command line option -v is specified as:

-v  Print  a  message each time a module is initialized, showing the
    place (filename or built-in module) from  which  it  is  loaded.
    When  given twice, print a message for each file that is checked
    for when searching for a module.  Also provides  information  on
    module cleanup at exit.

If we run the application with this flag, we get additional module loading information at stdout:

…
import 'apport.fileutils' # <_frozen_importlib.SourceFileLoader object at 0x7fb822fd5a10>
import 'apport.report' # <_frozen_importlib.SourceFileLoader object at 0x7fb8211ba890>
# /usr/lib/python3.3/__pycache__/gettext.cpython-33.pyc matches /usr/lib/python3.3/gettext.py
# code object from /usr/lib/python3.3/__pycache__/gettext.cpython-33.pyc
import 'gettext' # <_frozen_importlib.SourceFileLoader object at 0x7fb820afa190>
import 'apport' # <_frozen_importlib.SourceFileLoader object at 0x7fb82122d050>
Traceback (most recent call last):
  File "a.py", line 1, in <module>
    from b import db
  File "<frozen importlib._bootstrap>", line 1564, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1531, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 586, in _check_name_wrapper
  File "<frozen importlib._bootstrap>", line 1023, in load_module
  File "<frozen importlib._bootstrap>", line 1004, in load_module
  File "<frozen importlib._bootstrap>", line 562, in module_for_loader_wrapper
  File "<frozen importlib._bootstrap>", line 869, in _load_module
  File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
  File "/home/meisterluk/b.py", line 1, in <module>
    from a import app
  File "<frozen importlib._bootstrap>", line 1564, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1531, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 586, in _check_name_wrapper
  File "<frozen importlib._bootstrap>", line 1023, in load_module
  File "<frozen importlib._bootstrap>", line 1004, in load_module
  File "<frozen importlib._bootstrap>", line 562, in module_for_loader_wrapper
  File "<frozen importlib._bootstrap>", line 869, in _load_module
  File "<frozen importlib._bootstrap>", line 313, in _call_with_frames_removed
  File "/home/meisterluk/a.py", line 1, in <module>
    from b import db
ImportError: cannot import name db
# clear builtins._
# clear sys.path
# clear sys.argv
# clear sys.ps1
# clear sys.ps2
# clear sys.last_type
…

We can see that module a imports module b and module b imports module a again. This allows easier debugging without additional software tools.

Tested with xubuntu 13.04.

Debug circular imports in python

A visit of DruckZeug

Today I visited “DruckZeug”. This is a local association for the preservation of hot metal printing machines (or in general Movable Type) and printing techniques. The association is organizing open hours one day (about) every 2 months here in Graz, Styria. I got an introduction in the history of this association and its related printing companies and then we tried out some basic printing. Check out our results 🙂

DruckZeug Setzerbüro
Setzerbüro
DruckZeug Druck Letters
Printing Letters
DruckZeug Blindmaterial
Spacing
DruckZeug Satz einer Seite
Typesetting of a page
DruckZeug Nudelpresse typho
typho typeset and pressed

You don’t know what a Zwiebelfisch is? This is a (mostly accidental) different letter from another typeface [DE].

Verein [DE]:
Freunde des Bleisatzes und des Buchdruckes in der Steiermark, kurz “DruckZeug”
Association [EN]:
Friends of Movable Type and Printing in Styria, abbr. “DruckZeug”
Website [DE]:
druckzeug.at
A visit of DruckZeug

Licenses in the TeX community

Package Author / Maintainer License
TeX Don Knuth “Knuth License”: Permissive (don’t name forks TeX) free software licence
pdfTeX Han The Thanh GNU General Public License
PGF/TikZ Till Tantau dual (GNU General Public License, LPPL)
LaTeX Leslie Lamport LaTeX Project Public License (LPPL) (based on Knuth License)
TeX Live Karl Berry LaTeX Project Public License
ConTeXt Hans Hagen GNU General Public License v2 (source), CC BY-NC-SA 3.0 (docu)
XeTeX Jonathan Kew,
Khaled Hosny
Common Public License
eTeX Peter Breitenlohner Knuth License
MacTeX WG copyleft FLOSS (mixture of GNU General Public License, LGPL, BSD license, X license)
MikTeX Christian Schenk copyleft FLOSS
LuaTeX Hans Hagen Gnu Public License
Licenses in the TeX community