BUG: recursion errors

Bug

Error description

Error arises when importing mlair in an interactive python console. It does not appear when running a python script. UPDATE: It does only appear in a PyCharm python console.

Error message

import mlair
Backend TkAgg is interactive backend. Turning interactive mode on.
Using TensorFlow backend.
/home/l.leufen/demystify-temporal-components/venv/lib64/python3.6/site-packages/tensorflow/python/framework/dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint8 = np.dtype([("qint8", np.int8, 1)])
/home/l.leufen/demystify-temporal-components/venv/lib64/python3.6/site-packages/tensorflow/python/framework/dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
/home/l.leufen/demystify-temporal-components/venv/lib64/python3.6/site-packages/tensorflow/python/framework/dtypes.py:528: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint16 = np.dtype([("qint16", np.int16, 1)])
/home/l.leufen/demystify-temporal-components/venv/lib64/python3.6/site-packages/tensorflow/python/framework/dtypes.py:529: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
/home/l.leufen/demystify-temporal-components/venv/lib64/python3.6/site-packages/tensorflow/python/framework/dtypes.py:530: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  _np_qint32 = np.dtype([("qint32", np.int32, 1)])
/home/l.leufen/demystify-temporal-components/venv/lib64/python3.6/site-packages/tensorflow/python/framework/dtypes.py:535: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.
  np_resource = np.dtype([("resource", np.ubyte, 1)])
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/opt/pycharm-community-2020.2.1/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/home/l.leufen/demystify-temporal-components/venv/lib64/python3.6/site-packages/mlair/__init__.py", line 7, in <module>
    from mlair.run_modules import RunEnvironment, ExperimentSetup, PreProcessing, ModelSetup, Training, PostProcessing
  File "/opt/pycharm-community-2020.2.1/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/home/l.leufen/demystify-temporal-components/venv/lib64/python3.6/site-packages/mlair/run_modules/__init__.py", line 7, in <module>
    from mlair.run_modules.training import Training
  File "/opt/pycharm-community-2020.2.1/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/home/l.leufen/demystify-temporal-components/venv/lib64/python3.6/site-packages/mlair/run_modules/training.py", line 16, in <module>
    from mlair.plotting.training_monitoring import PlotModelHistory, PlotModelLearningRate
  File "/opt/pycharm-community-2020.2.1/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
  File "/home/l.leufen/demystify-temporal-components/venv/lib64/python3.6/site-packages/mlair/plotting/training_monitoring.py", line 15, in <module>
    matplotlib.use('Agg')
  File "/opt/pycharm-community-2020.2.1/plugins/python-ce/helpers/pydev/pydev_ipython/matplotlibtools.py", line 70, in patched_use
    matplotlib.real_use(*args, **kwargs)
  File "/opt/pycharm-community-2020.2.1/plugins/python-ce/helpers/pydev/pydev_ipython/matplotlibtools.py", line 70, in patched_use
    matplotlib.real_use(*args, **kwargs)
  File "/opt/pycharm-community-2020.2.1/plugins/python-ce/helpers/pydev/pydev_ipython/matplotlibtools.py", line 70, in patched_use
    matplotlib.real_use(*args, **kwargs)
  [Previous line repeated 473 more times]
RecursionError: maximum recursion depth exceeded while calling a Python object

First guess on error origin

run_environment is imported in the module run_modules.__init__.py but also in run_modules.training.py. This somehow is not working in the interactive python command prompt.

Problem is not the import of the training run module but the import of the monitoring plots inside the training module!

Error origin

There is a stackoverflow question which describes the error origin: https://stackoverflow.com/questions/744373/circular-or-cyclic-imports-in-python

This is the link to the real problem: https://github.com/fabioz/PyDev.Debugger/issues/148 There is no solution provided there, but indicates the problem.

Solution

Try to refac import strategy, moving from total imports starting at root to more relative imports. In this particular case, try to replace all run_environment statements inside of the run_modules package by relative imports using from . import run_environment. This could fix this import problem. But it could be that more errors will arise in other places in the code.

Remove the line with matplotlib.use("TkAgg") to solve the issue.

Edited by Ghost User