How to solve theano - circular import problem in python?

375 Views Asked by At

I try to run some code from 2019 in my jupyternotebook. I have error that I don't know how to solve:



----> 8 import theano.tensor as T

...


File ~/my-jupyter-env/lib/python3.11/site-packages/theano/__init__.py:124
    120 from theano.misc.safe_asarray import _asarray
    122 from theano.printing import pprint, pp
--> 124 from theano.scan_module import (scan, map, reduce, foldl, foldr, clone,
    125                                 scan_checkpoints)
    127 from theano.updates import OrderedUpdates
    129 # scan_module import above initializes tensor and scalar making these imports
    130 # redundant
    131 
   (...)
    136 
    137 # import sparse

File ~/my-jupyter-env/lib/python3.11/site-packages/theano/scan_module/__init__.py:41
     38 __copyright__ = "(c) 2010, Universite de Montreal"
     39 __contact__ = "Razvan Pascanu <r.pascanu@gmail>"
---> 41 from theano.scan_module import scan_opt
     42 from theano.scan_module.scan import scan
     43 from theano.scan_module.scan_checkpoints import scan_checkpoints

File ~/my-jupyter-env/lib/python3.11/site-packages/theano/scan_module/scan_opt.py:60
     57 import numpy as np
     59 import theano
---> 60 from theano import tensor, scalar
     61 from theano.tensor import opt, get_scalar_constant_value, Alloc, AllocEmpty
     62 from theano import gof

File ~/my-jupyter-env/lib/python3.11/site-packages/theano/tensor/__init__.py:8
      4 __docformat__ = "restructuredtext en"
      6 import warnings
----> 8 from theano.tensor.basic import *
      9 from theano.tensor.subtensor import *
     10 from theano.tensor.type_other import *

File ~/my-jupyter-env/lib/python3.11/site-packages/theano/tensor/basic.py:20
     17 from theano.gof import Apply, Constant, Op, Variable, ParamsType
     18 from theano.gof.type import Generic
---> 20 from theano.scalar import int32 as int32_t
     21 from theano.tensor import elemwise
     22 from theano.tensor.var import (AsTensorError, TensorVariable,
     23                                TensorConstant, TensorConstantSignature,
     24                                _tensor_py_operators)

File ~/my-jupyter-env/lib/python3.11/site-packages/theano/scalar/__init__.py:3
      1 from __future__ import absolute_import, print_function, division
----> 3 from .basic import *
      5 from .basic_scipy import *

File ~/my-jupyter-env/lib/python3.11/site-packages/theano/scalar/basic.py:657
    654         return shape_info
    656 # Register C code for ViewOp on Scalars.
--> 657 theano.compile.register_view_op_c_code(
    658     Scalar,
    659     """
    660     %(oname)s = %(iname)s;
    661     """,
    662     1)
    665 bool = get_scalar_type('bool')
    666 int8 = get_scalar_type('int8')

AttributeError: partially initialized module 'theano' has no attribute 'compile' (most likely due to a circular import)

In readme.md it is said taht it requires Theano (1.0.0) and numpy (1.13.3). Should I downgrade my versions to these to run it? I tried but it doesn't help. I heard that theano is dead now and I see a lot of errors like theano uses for example 'MulatbleMaps' from 'collections' library, but now beacuse of some updates it should be 'collections.abc'. So I tried to change for example this collection error in my theano source code and I don't get this error now. But I have error that I don't know how to solve which i showed here. So what should I change in code or in installation to get rid of this error? Or maybe I should do something different to just run theano safely now, in 2023? I know I can use tensorflow for examle, but this code is very long and I don't want to rewrite it.

1

There are 1 best solutions below

0
0x00 On

Looking at PyPi it looks like they updated the library to support Python 3.9 but you are running Python 3.11 which is likely causing problems.

Also, if you check the README file from their repository is does say:

MILA will stop developing Theano: https://groups.google.com/d/msg/theano-users/7Poq8BZutbY/rNCIfvAEAwAJ [Sep 28, 2017]

You have two options:

  • Recommended: Theano was forked into a new project called Aesara which is actively maintained and does support Python 3.11. You'd have to update your Jupyternotebook code to use Aesara.
  • Not recommended: Install Python 3.9, install the dependencies and try running the jupyternotebook. It's not recommended because you'd have to install an older version of Python and you'll be using an old project (Theano) which stopped getting updates.