4
votes

Totally new to Python. Saw no hits whatsoever on this error. Here's what I did, almost to the keystroke:

  1. Clean install of Python 3.6.
  2. pip install -U statsmodels
  3. pip install scipy
  4. pip install numpy
  5. pip install statsmodels --upgrade
  6. (In Python): import statsmodels.api as sm -> "AttributeError: module 'statsmodels' has no attribute 'compat'"

Any suggestions? I'm just trying to walk through a multi regression tutorial on https://towardsdatascience.com/simple-and-multiple-linear-regression-in-python-c928425168f9. Full traceback follows.

Thanks in advance.

>>> import statsmodels.api as sm
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\dataylor\AppData\Local\Programs\Python\Python36-32\lib\site-packages\statsmodels\api.py", line 35, in <module>
    from .stats import api as stats
  File "C:\Users\dataylor\AppData\Local\Programs\Python\Python36-32\lib\site-packages\statsmodels\stats\api.py", line 65, in <module>
    from .mediation import Mediation
  File "C:\Users\dataylor\AppData\Local\Programs\Python\Python36-32\lib\site-packages\statsmodels\stats\mediation.py", line 22, in <module>
    import statsmodels.compat.pandas as pdc  # pragma: no cover
AttributeError: module 'statsmodels' has no attribute 'compat'
3
It doesn't look as though statsmodels is properly installed. It may be worth uninstalling it with pip uninstall statsmodels and then installing again with pip install statsmodelsWill Keeling
I think that was it! Thanks!D. Taylor
If the answer below solved your problem, please consider clicking the tick next to the post. This let's the community know your question has been answered.Philipp HB

3 Answers

9
votes

In my case, also using Jupyter Notebook, the solution was to use:

import statsmodels.api as sm

instead of

import statsmodels as sm

This is the recommended approach (as per documentation), as statsmodels.api is the public access, and statsmodels (without api) might stop working. In my case, I used the GLM function.

3
votes

Formulating @Will Kneeling's comment into an answer for users with a similar problem.

The Statsmodels package seems to have not installed properly. Try uninstalling and reinstalling the package, like so:

pip uninstall statsmodels 
pip install statsmodels
2
votes

In case you're working with jupyter, try to restart the notebook server.

Usually pip install <package> makes the package available without issues, but for statsmodels I kept getting the above mentioned error until I restarted the notebook server.