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

Jupyter中导入Theano/PyMC3报错:未初始化模块无compile属性(循环导入)

问题描述

我在Jupyter Notebook中使用Python 3.11.5版本,已安装Theano 1.0.5和PyMC3 3.11.4。执行以下导入代码:

import numpy as np
import arviz as az
import theano
import pymc3 as pm

出现如下错误:

AttributeError      Traceback (most recent call last)
   Input In [76], in <module>
      1 import numpy as np
      2 import arviz as az
----> 3 import theano
      4 import pymc3 as pm
      5 #print("NumPy version:", np.__version__)
      6 #print("ArviZ version:", az.__version__)

File ~/opt/anaconda3/lib/python3.9/site-packages/theano/__init__.py:124, in <module>
    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 ~/opt/anaconda3/lib/python3.9/site-packages/theano/scan_module/__init__.py:41, in <module>
     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 ~/opt/anaconda3/lib/python3.9/site-packages/theano/scan_module/scan_opt.py:60, in <module>
     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 ~/opt/anaconda3/lib/python3.9/site-packages/theano/tensor/__init__.py:38, in <module>
     34 from theano.tensor.sharedvar import tensor_constructor as _shared
     36 from theano.tensor.io import *
--> 38 from theano.tensor import nnet  # used for softmax, sigmoid, etc.
     40 from theano.gradient import Rop, Lop, grad, numeric_grad, verify_grad, \
     41     jacobian, hessian, consider_constant
     43 from theano.tensor.sort import sort, argsort, topk, argtopk, topk_and_argtopk

File ~/opt/anaconda3/lib/python3.9/site-packages/theano/tensor/nnet/__init__.py:2, in <module>
      1 from __future__ import absolute_import, print_function, division
--> 2 from .nnet import (
      3     CrossentropyCategorical1Hot, CrossentropyCategorical1HotGrad,
      4     CrossentropySoftmax1HotWithBiasDx, CrossentropySoftmaxArgmax1HotWithBias,
      5     LogSoftmax, Prepend_scalar_constant_to_each_row,
      6     Prepend_scalar_to_each_row, Softmax,
      7     SoftmaxGrad, SoftmaxWithBias,
      8     binary_crossentropy, sigmoid_binary_crossentropy,
      9     categorical_crossentropy, crossentropy_categorical_1hot,
     10     crossentropy_categorical_1hot_grad, crossentropy_softmax_1hot,
     11     crossentropy_softmax_1hot_with_bias,
     12     crossentropy_softmax_1hot_with_bias_dx,
     13     crossentropy_softmax_argmax_1hot_with_bias,
     14     crossentropy_softmax_max_and_argmax_1hot,
     15     crossentropy_softmax_max_and_argmax_1hot_with_bias,
     16     crossentropy_to_crossentropy_with_softmax,
     17     crossentropy_to_crossentropy_with_softmax_with_bias,
     18     graph_merge_softmax_with_crossentropy_softmax, h_softmax,
     19     logsoftmax, logsoftmax_op, prepend_0_to_each_row, prepend_1_to_each_row,
     20     prepend_scalar_to_each_row, relu, softmax, softmax_grad, softmax_graph,
     21     softmax_op, softmax_simplifier, softmax_with_bias, elu, selu,
     22     confusion_matrix, softsign)
     23 from . import opt
     24 from .conv import ConvOp

File ~/opt/anaconda3/lib/python3.9/site-packages/theano/tensor/nnet/nnet.py:32, in <module>
     29 from theano.compile import optdb
     30 from theano.gof import Apply
--> 32 from theano.tensor.nnet.sigm import sigmoid, softplus
     33 from theano.gradient import DisconnectedType
     34 from theano.gradient import grad_not_implemented`

File ~/opt/anaconda3/lib/python3.9/site-packages/theano/tensor/nnet/sigm.py:275, in <module>
    273         out.tag.values_eq_approx = values_eq_approx_remove_low_prec
    274         return [out]
--> 275 theano.compile.optdb['uncanonicalize'].register("local_ultra_fast_sigmoid",
    276                                                 local_ultra_fast_sigmoid)
    279 def hard_sigmoid(x):
    280     """
    281     An approximation of sigmoid.
    282 
   (...)
    288 
    289    
AttributeError: partially initialized module 'theano' has no attribute 'compile' (most likely due to a circular import)

我尝试过更换Python版本,安装pygpu并使用numpy monkey patch解决了初始错误,但仍无法解决该属性错误。

解决方案

这个循环导入错误的核心原因是版本不兼容:Theano 1.0.5未适配Python 3.11,而PyMC3 3.11.4的官方支持上限是Python 3.9,二者在高版本Python下会触发循环导入问题。按以下步骤解决:

  • 创建Python 3.9的虚拟环境
    用conda隔离环境,避免影响其他项目:

    conda create -n pymc3_env python=3.9
    conda activate pymc3_env
    
  • 安装兼容版本的依赖
    在激活的环境中,安装指定版本的包,确保依赖链匹配:

    pip install theano==1.0.5 pymc3==3.11.4 arviz==0.11.4 numpy==1.21.6
    

    指定ArviZ和NumPy版本是为了避免后续的兼容性冲突。

  • 验证安装
    在Jupyter Notebook中切换到pymc3_env环境,执行导入代码:

    import numpy as np
    import arviz as az
    import theano
    import pymc3 as pm
    print("Theano版本:", theano.__version__)
    print("PyMC3版本:", pm.__version__)
    

    无报错则说明安装成功。

若不想用虚拟环境,也可以直接降级Python到3.9,然后重新安装所有兼容版本的依赖,但虚拟环境的方式更安全。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 07:27:33