简体   繁体   中英

Python 3.8 cannot import statsmodels.api

I installed NumPy, SciPy, Pandas, and Patsy before installing statsmodels, yet when I tried to import statsmodels.api I got the following error message:

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)

Importing statsmodels by itself works fine. I've tried to resolve this issue by completely uninstalling statsmodels and installing it again as well as installing statsmodels via pip install git+https://github.com/statsmodels/statsmodels , but again no luck.

The versions of everything are as follows:

NumPy: 1.18.1
Pandas: 0.25.3
SciPy: 1.4.1
Patsy: 0.5.1
Statsmodels: 0.11.1

Investigating the error message further, I opened the file __init.py__ within the statespace folder and found the following code:

from statsmodels.tools._testing import PytestTester

test = PytestTester()

Running this code, I get the following error message:

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

I don't know if this means anything since I simply ran the code all by itself, but this problem has been bugging me for a while: please help!

From the file in the error, https://github.com/statsmodels/statsmodels/blob/f3f89ad777b22e6db565897397ece24d41af5700/statsmodels/tools/_testing.py#L27 , it expects a filename. If you're running the file directly it won't work

#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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM