You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Python中Theano的循环导入问题?

Theano 1.0.0循环导入错误排查与解决方案

问题场景

在Jupyter Notebook中运行2019年的旧代码时,导入theano.tensor触发循环导入错误,报错信息如下:

----> 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)

已按项目README要求安装Theano 1.0.0和numpy 1.13.3,也修复了collections.MutableMap转collections.abc的兼容问题,但仍存在上述循环导入错误,希望找到无需重写代码的解决方案。

解决方案

1. 降级Python版本到3.7或更低

Theano 1.0.0发布于2017年,官方仅支持Python 2.7、3.5、3.6,部分兼容3.7,Python 3.8+的导入机制变化会触发这类循环导入问题。

  • 创建指定Python版本的虚拟环境:
    conda create -n theano-legacy python=3.7
    conda activate theano-legacy
    
  • 安装指定依赖:
    pip install theano==1.0.0 numpy==1.13.3
    

2. 修复Theano源码中的循环导入问题

如果无法降级Python,可手动修改Theano的源码文件:

  • 打开theano/scalar/basic.py,找到第657行的theano.compile.register_view_op_c_code调用
  • 将其修改为显式导入后再调用:
    # 替换原代码
    import theano.compile
    theano.compile.register_view_op_c_code(
        Scalar,
        """
        %(oname)s = %(iname)s;
        """,
        1)
    
    或者将这段代码移到文件末尾,确保theano.compile模块已完全加载。

3. 使用Docker封装旧环境

用Docker隔离兼容的运行环境,避免本地环境冲突:

  • 创建Dockerfile:
    FROM python:3.7-slim
    WORKDIR /app
    RUN pip install --no-cache-dir theano==1.0.0 numpy==1.13.3 jupyter
    CMD ["jupyter", "notebook", "--ip=0.0.0.0", "--allow-root"]
    
  • 构建并运行容器:
    docker build -t theano-legacy .
    docker run -p 8888:8888 -v $(pwd):/app theano-legacy
    

4. 使用社区维护的Theano分支

Theano虽已停止官方维护,但社区分支theano-pymc针对新版本Python做了兼容修复:

  • 安装维护版本:
    pip install git+https://github.com/pymc-devs/Theano.git
    
    该分支已修复Python 3.8+的导入问题,无需手动修改源码。

内容的提问来源于stack exchange,提问作者Juan Hortez

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 02:34:59