We present TB2J, a Python package for the automatic computation of magnetic interactions, including exchange and Dzyaloshinskii-Moriya interactions, between atoms of magnetic crystals from the results of density functional calculations. The program is based on the Greens function method with the local rigid spin rotation treated as a perturbation. As input,the package uses the output of either Wannier90, which is interfaced with many density functional theory packages,or of codes based on localized orbitals. A minimal user input is needed, which allows for easy integration into high-throughput workflows. The package is open source under BSD 2-Clause license, available at https://github.com/mailhexu/TB2J.