【问题标题】:Python 3.8 cannot import statsmodels.apiPython 3.8 无法导入 statsmodels.api
【发布时间】:2020-07-12 13:45:40
【问题描述】:

我在安装 statsmodels 之前安装了 NumPy、SciPy、Pandas 和 Patsy,但是当我尝试导入 statsmodels.api 时收到以下错误消息:

ImportError: cannot import name '_initialization' from
'statsmodels.tsa.statespace'
(/Library/Frameworks/Python.framework/Versions/3.8/lib/
python3.8/site-packages/statsmodels/tsa/statespace/__init__.py)

单独导入 statsmodels 可以正常工作。我试图通过完全卸载 statsmodels 并再次安装它以及通过pip install git+https://github.com/statsmodels/statsmodels 安装 statsmodels 来解决此问题,但还是没有运气。

一切的版本如下:

NumPy:1.18.1
熊猫:0.25.3
SciPy:1.4.1
帕西:0.5.1
统计模型:0.11.1

进一步调查错误信息,我打开状态空间文件夹中的文件__init.py__,发现以下代码:

from statsmodels.tools._testing import PytestTester

test = PytestTester()

运行此代码,我收到以下错误消息:

File "/Library/Frameworks/Python.framework/Versions/
3.8/lib/python3.8/site-packages/statsmodels/tools/_testing.py", line 29, in __init__
raise ValueError('Unable to determine path')
ValueError: Unable to determine path

我不知道这是否意味着什么,因为我只是自己运行了代码,但是这个问题一直困扰着我一段时间:请帮助!

【问题讨论】:

标签: python python-3.x statsmodels


【解决方案1】:

从错误中的文件https://github.com/statsmodels/statsmodels/blob/f3f89ad777b22e6db565897397ece24d41af5700/statsmodels/tools/_testing.py#L27,它需要一个文件名。如果您直接运行该文件,它将无法正常工作

#temp.py

def foo():
  import sys
  f = sys._getframe(1)
  print("Name:", f.f_locals.get('__file__', None))

$ python3 ./temp.py
>>> Name: None

# temp2.py
import temp
temp.foo()

$ python3 ./temp2.py
>>> Name: "temp2.py"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-02
    • 2019-10-26
    • 1970-01-01
    • 2021-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    相关资源
    最近更新 更多