Python 3.8 cannot import statsmodels.api - python

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"

Related

ImportError: cannot import name 'safe_str_cmp' from 'werkzeug.security'

Any ideas on why I get this error?
My project was working fine. I copied it to an external drive and onto my laptop to work on the road; it worked fine. I copied it back to my desktop and had a load of issues with invalid interpreters etc, so I made a new project and copied just the scripts in, made a new requirements.txt and installed all the packages, but when I run it, I get this error:
Traceback (most recent call last):
File "E:\Dev\spot_new\flask_blog\run.py", line 1, in <module>
from flaskblog import app
File "E:\Dev\spot_new\flask_blog\flaskblog\__init__.py", line 3, in <module>
from flask_bcrypt import Bcrypt
File "E:\Dev\spot_new\venv\lib\site-packages\flask_bcrypt.py", line 21, in <module>
from werkzeug.security import safe_str_cmp
ImportError: cannot import name 'safe_str_cmp' from 'werkzeug.security' (E:\Dev\spot_new\venv\lib\site-packages\werkzeug\security.py)
I've tried uninstalling Python, Anaconda, PyCharm, deleting every reg key and environment variable I can find that looks pythonic, reinstalling all from scratch but still no dice.
Werkzeug released v2.1.0 today, removing werkzeug.security.safe_str_cmp.
You can probably resolve this issue by pinning Werkzeug~=2.0.0 in your requirements.txt file (or similar).
pip install Werkzeug~=2.0.0
After that it is likely that you will also have an AttributeError related to the jinja package, so if you have it, also run:
pip install jinja2~=3.0.3
This issue can also be fixed by upgrading flask_login.
pip install --upgrade flask_login
Werkzeug 2.1.0 release notes recommend using the hmap equivalent. For reference, here is the implementation of safe_str_cmp from wekzeug 2.0.x, and here is a stripped-down version:
import hmac
def safe_str_cmp(a: str, b: str) -> bool:
"""This function compares strings in somewhat constant time. This
requires that the length of at least one string is known in advance.
Returns `True` if the two strings are equal, or `False` if they are not.
"""
if isinstance(a, str):
a = a.encode("utf-8") # type: ignore
if isinstance(b, str):
b = b.encode("utf-8") # type: ignore
return hmac.compare_digest(a, b)
or even more stripped-down one:
import hmac
str_to_bytes = lambda s: s.encode("utf-8") if isinstance(s, str) else s
safe_str_cmp = lambda a, b: hmac.compare_digest(str_to_bytes(a), str_to_bytes(b))
ImportError: cannot import name 'safe_str_cmp' from 'werkzeug.security
To Solve ImportError: cannot import name 'safe_str_cmp' from 'werkzeug.security' Error You can also
Downgrade Werkzeug to 2.0.0
is working fine So you can Just downgrade Werkzeug to 2.0.0 just run this command:
pip install Werkzeug==2.0.0
OR
pip install Werkzeug==2.1.0
now your error must be solved.
The import statement here is due to an outdated version of flask-bcrypt.
You can fix this issue by importing the most recent version of flask-bcrypt, which at time of writing is version 1.0.1:
pip install --upgrade flask-bcrypt
The new version imports and uses hmac, rather than werkzeug's security for this purpose.
Note: it is very likely that the other answers here were correct at time of writing, I'm not sure on the timeframe for when flask-bcrypt was updated

Python 3.6 AttributeError: module 'statsmodels' has no attribute 'compat'

Totally new to Python. Saw no hits whatsoever on this error. Here's what I did, almost to the keystroke:
Clean install of Python 3.6.
pip install -U statsmodels
pip install scipy
pip install numpy
pip install statsmodels --upgrade
(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'
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.
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
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.

ImportError: cannot import name '_ccallback_c'

Initially I was getting this error (No Module name was found scipy) So I installed a Scipy wheel file. Now I don't get the same error any more but I get cannot import name "_ccallback_c".
The error seems to be triggered at the fourth line of code. I have done my research and saw that other people suggested to try an environment such as Anaconda. I have seen it work on idle, and that solution isn't ideal for me.
Traceback:
Traceback (most recent call last):
File "C:\Users\joesh\Desktop\Python\Machine Learning\1st tutorial.py", line 4, in <module>
from sklearn import preprocessing, cross_validation, svm
File "C:\Users\joesh\Desktop\Python\lib\site-packages\sklearn\__init__.py", line 134, in <module>
from .base import clone
File "C:\Users\joesh\Desktop\Python\lib\site-packages\sklearn\base.py", line 10, in <module>
from scipy import sparse
File "C:\Users\joesh\Desktop\Python\lib\site-packages\scipy\__init__.py", line 118, in <module>
from scipy._lib._ccallback import LowLevelCallable
File "C:\Users\joesh\Desktop\Python\lib\site-packages\scipy\_lib\_ccallback.py", line 1, in <module>
from . import _ccallback_c
ImportError: cannot import name '_ccallback_c'
And the code:
import pandas as pd
import quandl, math
import numpy as np
from sklearn import preprocessing, cross_validation, svm
from sklearn.linear_model import LinearRegression
I had the same error on USING Anaconda, so I am not sure if using it would make any difference.
I solved it by just uninstalling scipy and re-installing it using pip:
pip uninstall scipy
you'll get this message:
Uninstalling scipy-1.1.0: Would remove:
c:\users\thesh\appdata\local\programs\python\python36-32\lib\site-packages\scipy-1.1.0.dist-info*
c:\users\thesh\appdata\local\programs\python\python36-32\lib\site-packages\scipy*
Proceed (y/n)?
press y, and after pip is done, type:
pip install scipy
Having just moved to MSVS 2017 for Python (ML) development, I encountered this and other errors related to missing modules. The problem (and all related problems like it) has a frustratingly simple solution: when I originally started coding in Python, I installed everything from the command line - apparently, MSVS 2017 doesn't "see" those installations (and, in fact, they sometimes conflict, since the underlying python may be tapping older libs); so, the solution is to:
Use the command line version of 'pip uninstall ...' where '...' is the library having missing dependencies (scipy, in this case). Then, in the MSVS 2017 command line on the Python environment window (usually, top right in the default display configuration), reload the library (in this case, typing 'scipy' will format a command line for execution [in the list control below the command textbox]) that will read something like 'pip install scipy' (or whatever library needs to be reinstalled for MSVS).
You may have to do this for many (or all) of your previous Python package installations where these missing module errors persist.
Can be resolved, by uninstalling and reinstalling using pip on Anaconda Prompt:
pip uninstall scipy
After the uninstall, you can reinstall with:
pip install scipy
When you installed scipy with pip in a Python version 3.6 and later try to run your code with Python 3.7 you will encounter this problem. So one solution is to uninstall scipy
pip3 uninstall scipy
and reinstall it (using an environment with Python 3.7):
pip3 install scipy
This will make sure that the installed version of scipy is compatible with your version of Python.
PS: When you updated Python from Python 3.6 to Python 3.7 it might be necessary to also reinstall pip, so that pip will use the correct version of Python internally.
I ran into this when I was following these instructions on how to use a virtual environment to use the pre-built version of SciPy. The simplest solution for me was to simply comment out from . import _ccallback_c under scipy\_lib\_ccallback.py.
I first had the error with scipy. So I ran the command python -m pip install -user numpy scipy matplotlib ipython jupyter pandas sympy noseand it worked perfectly. I was installing everything with pip, so I decided to use Anaconda. I installed and checked to add to the PATH. From there, the same code that was executed before normally stopped working and displays the error similar to that of the question. I uninstalled Anaconda and it is now working again.
Erro:
$ winpty python ia.py
Traceback (most recent call last):
File "ia.py", line 11, in <module>
from sklearn import tree #importando a biblioteca e a árvore p/ o classifica
dor
File "C:\Users\ferna\Anaconda3\lib\site-packages\sklearn\__init__.py", line 13
4, in <module>
from .base import clone
File "C:\Users\ferna\Anaconda3\lib\site-packages\sklearn\base.py", line 11, in
<module>
from scipy import sparse
File "C:\Users\ferna\AppData\Roaming\Python\Python36\site-packages\scipy\__ini
t__.py", line 118, in <module>
from scipy._lib._ccallback import LowLevelCallable
File "C:\Users\ferna\AppData\Roaming\Python\Python36\site-packages\scipy\_lib\
_ccallback.py", line 1, in <module>
from . import _ccallback_c
ImportError: cannot import name '_ccallback_c'
Código:
from sklearn import tree #importando a biblioteca e a árvore p/ o classificador
#COLLLECT TRAINING DATA
features = [[140,1],[130,1],[150,0],[170,0]]
labels = [0,0,1,1]
# TRAIN CLASSIFIER
clf = tree.DecisionTreeClassifier() #Classificador
clf = clf.fit(features, labels) #algoritmo de decisão p/ encontrar padrões
#MAKE PREDICTIONS
print(clf.predict([[160, 0]])) #entrada de dados para o tratamento
Try this:
python -m pip install --upgrade scipy
After digging in, to give the full background on this, first of all SciPy relies on having NumPy already installed. The SciPy wheel's setup.py file uses NumPy functionality to configure and install the wheel.
SciPy setup.py:
...
if __name__ == '__main__':
from numpy.distutils.core import setup
setup(**configuration(top_path='').todict())
Secondly, when just trying to use the wheel, if you run into this error, you can see after inspecting the wheel's files that the reason is the binary wheels have a naming convention where the shared object file, here it's called _ccallback_c.so, is instead named based on the architecture that the binary wheel supports. When trying to import the shared object by file name in /_lib/_ccallback.py it can't find it, hence this error (line 1 in /_lib/_ccallback.py) because, instead of being named _ccallback_c.so it's called _ccallback_c.cpython-36m-x86_64-linux-gnu.so or another architecture variation:
from . import _ccallback_c
These file names seem to be an artifact of libraries that are using Cython and Cython's adherence to PEP 3149 (ABI version tagged .so files). But the easiest fix is to change the .whl extension to .zip and rename all those relevant .so files to not contain the architecture snippet. Then change .zip -> .whl and it should be good to go unless it's the wrong architecture for the platform you're using, in which case you need to download the appropriate platform wheel for your platform.

ImportError: cannot import name 'ellipkm1'

I've seen some answers to my issue, but none seem sufficiently applicable. Here's my issue: I'm by no means a Python expert, but am trying to experiment with a fairly complex Python package. (Don't ask...) When I try to import something I need to move forward, I get the following error (IDLE output):
import scipy.special as special
Traceback (most recent call last):
File "<pyshell#16>", line 1, in <module>
import scipy.special as special
File "C:\DevTools\Python35\lib\site-packages\scipy\special\__init__.py", line 629, in <module>
from .basic import *
File "C:\DevTools\Python35\lib\site-packages\scipy\special\basic.py", line 14, in <module>
from ._ufuncs import (ellipkm1, mathieu_a, mathieu_b, iv, jv, gamma, psi, zeta,
ImportError: cannot import name 'ellipkm1'
My environment:
Windows 10, 64-bit
Python 3.5.1 (64-bit)
Installed packages:
Bottleneck (1.0.0)
numpy (1.10.4)
pandas (0.18.0)
Pint (0.7.2)
pip (8.1.0)
python-dateutil (2.5.0)
pytz (2016.1)
PyYAML (3.11)
requests (2.9.1)
scipy (0.17.0)
setuptools (18.2)
six (1.10.0)
Some were installed via straight 'pip install [package]'; others, because I couldn't get all the necessary C compilers set up on my machine, installed from wheel files from http://www.lfd.uci.edu/~gohlke/pythonlibs/ , as follows:
PyYAML-3.11-cp35-none-win_amd64.whl
pandas-0.18.0-cp35-cp35m-win_amd64.whl
Bottleneck-1.0.0-cp35-cp35m-win_amd64.whl
scipy-0.17.0-cp35-none-win_amd64.whl
Along the way I've had to reverse-engineer failed DLL loads, missing runtime DLLs, etc, but seemingly all of these have now been resolved--yet I still get the darn error above.
Any help would be greatly appreciated. Sorry if I've missed any information that may be helpful in troubleshooting this.
Thanks in advance.
As indicated at the top of Dr. Gohlke's excellent page, he writes
Many binaries depend on NumPy-1.10+MKL ...
and the scipy section states:
Requires numpy+mkl and optionally pillow.
I suspect there's a linking issue hidden somewhere where somebody is trying to call an MKL function and it can't be found, hence the module fails to load. It certainly wouldn't hurt to install the numpy wheel to see if that's the case.
Also, your import can simply be:
from scipy import special

'from gensim import test' is not importing successfully

I installed gensim, Python library.
I executed the command
Import gensim
It executed without any error.
Then I tried to import test from gensim using the command
from gensim import test
and it showed the following error
Traceback (most recent call last):
File "", line 1, in
from gensim import test
ImportError: cannot import name 'test'
Python site-packages had gensim folder in that.
Any help would be highly appreciated.
As it says: cannot import name 'test'
That means that test is not in gensim package.
You can see package's modules with:
import gensim
print(gensim.__all__) # when package provide a __all__
or
import gensim
import pkgutil
modules = pkgutil.iter_modules(gensim.__path__)
for module in modules:
print(module[1])
Edit:
How can I verify gensim installation ?
try:
import gensim
except NameError:
print('gensim is not installed')
Side note: if you have a file or package named gensim, this will ne imported instead of the real package.
I had similar experience but with scipy. After uninstalling scipy import scipy did not give error. but none of its modules will work. I noticed that scipy folder was still in \site-packages but modules inside it were uninstalled.
When I installed it again it worked fine.
You should also see inside the directory, and try to reinstall or upgrade it.

Categories

Resources