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

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.

Related

Python 3.6.8 - Jedi - ImportError: cannot import name 'traverse_parents'

<TL;DR>
When running import jedi in python 3.6.8 I get the following import error (see end of post for full traceback):
...
from jedi.common import traverse_parents
ImportError: cannot import name 'traverse_parents'
</TL;DR>
I checked in ~/.local/lib/python3.6/site-packages/jedi/common.py : There's a function called traverse_parents alright so there's definitely some witchery at work. Not the first time I've run into one of those "undefined name" problems, but I'm running out of ideas on this one.
Already tried :
uninstalling, reinstalling, upgrading the "jedi" package for pip3
pretty much the same jazz for a couple other packages with "jedi" in their names
upgrading pip3 to 20.2.1 and redoing the same reinstalling routine
installing jedi for python2 because why not (hey, it works fine in python2 but who cares)
also tried installing python-jedi from apt-get (I removed it afterwards)
incantations to ancient Babylonian demons (to no avail, they don't work Sundays)
Vague clues on how I might fix it :
getting the latest version of jedi from github and somehow installing it without making a mess (fat chance)
uninstalling jedi and reinstalling it from within Neovim (hey that might achieve the previous point)
I've scoured the web for the answer to this problem but I'm getting nowhere. Has anyone experienced something similar and managed to solve it ?
Any suggestion welcome.
The full traceback :
Error detected while processing function provider#python3#Call:
line 18:
Error invoking 'python_execute' on channel 5 (python3-script-host):
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "/home/vee/.local/lib/python3.6/site-packages/jedi/__init__.py", line 32, in <module>
from jedi.api import Script, Interpreter, set_debug_funcion, \
File "/home/vee/.local/lib/python3.6/site-packages/jedi/api/__init__.py", line 24, in <module>
from jedi.api import classes
File "/home/vee/.local/lib/python3.6/site-packages/jedi/api/classes.py", line 26, in <module>
from jedi.inference import imports
File "/home/vee/.local/lib/python3.6/site-packages/jedi/inference/__init__.py", line 70, in <module>
from jedi.inference import imports
File "/home/vee/.local/lib/python3.6/site-packages/jedi/inference/imports.py", line 24, in <module>
from jedi.inference import sys_path
File "/home/vee/.local/lib/python3.6/site-packages/jedi/inference/sys_path.py", line 8, in <module>
from jedi.common import traverse_parents
ImportError: cannot import name 'traverse_parents'
Fixed it :
I reinstalled the latest version of jedi via pip with this command :
pip3 install -e git://github.com/davidhalter/jedi.git#egg=jedi
Now, the command import jedi doesn't generate any exception and it seems to work alright in Neovim.
The reason why the default version installed with pip3 install jedi generated that ImportError is beyond what I can guess. Maybe this version was faulty itself, maybe some quirk of my python environment...
Edit: According to David Halter in the comment below, it seems to be a pip bug
Anyway. If you have the same problem, try this fix.

How to properly install csdtoolbox-remote?

I am having trouble installing the cdstoolbox-remote on my Linux machine (tried both Ubuntu and Debian). I installed it using pip:
pip install cdstoolbox-remote
Which successfully installs the module, but I can't import the module in Python. Importing the module causes the following error:
>>> import cdstoolbox
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/an/miniconda2/envs/tools/lib/python3.7/site-packages/cdstoolbox/__init__.py",
line 8, in <module>
with open(__main__.__file__) as f:
AttributeError: module '__main__' has no attribute '__file__'
What can be done to get it working?
Do you really need cdstoolbox-remote? The standard API for downloading data from CDS would be cdsapi, which can be installed using
pip install cdsapi
or using conda (https://anaconda.org/conda-forge/cdsapi)
I also just tried installing cdstoolbox-remote and got the same problem. But as it is flagged as experimental I even consider it to be possible that the current version is not a stable version.
And cdsapi seems to work.
Actually, the CDSAPI can also be used to execute workflows as follows:
import cdsapi
c = cdsapi.Client()
with open("workflow.py") as f:
code = f.read()
r = c.workflow(code)
print(c.download(r))
For more, read this thread on the Copernicus services documentation.

ImportError: cannot import name get_ast_names error on lmfit

I have recently updated to Ubuntu 18.04.1 LTS. After that I tried to run some of my python codes and I got an error related to the lmfit package, here's the code:
import scipy
from asteval import get_ast_names
import lmfit
(I have explicitly added the scipy and asteval modules to check python was finding them porperly)
I obtained this error:
Traceback (most recent call last):
File "/home/maurizio/Documents/Python/Programmini/PhD/TEAS/Test.py", line 3, in <module>
import lmfit
File "/home/maurizio/.local/lib/python2.7/site-packages/lmfit/__init__.py", line 42, in <module>
from .confidence import conf_interval, conf_interval2d
File "/home/maurizio/.local/lib/python2.7/site-packages/lmfit/confidence.py", line 12, in <module>
from .minimizer import MinimizerException
File "/home/maurizio/.local/lib/python2.7/site-packages/lmfit/minimizer.py", line 35, in <module>
from .parameter import Parameter, Parameters
File "/home/maurizio/.local/lib/python2.7/site-packages/lmfit/parameter.py", line 8, in <module>
from asteval import Interpreter, get_ast_names, valid_symbol_name
ImportError: cannot import name get_ast_names
which basically states that lmfit cannot find the function get_ast_names from asteval, although the python interpreter doesn't complain when I try to import it so it should be properly installed.
I have found this question with a similar problem and the guy in the comments claims he solved it by reinstalling it with pip3, so I tried to compile it with python3 and there's no problem so the issue concerns only python 2
Any help?
I believe you must have conflicting versions of lmfit and asteval. This should have been automatically noticed (and ideally resolved) when installing lmfit, but something there appears to have gone wrong. For the latest releases, you should get
>>> import asteval
>>> asteval.__version__
0.9.12
>>> import lmfit
>>> lmfit.__version__
0.9.11
Depending on how you installed these, you may need to check for and remove older versions of asteval in your $HOME/.local/lib/python2.7/site-packages/ folder and re-do pip install asteval.

matplotlib.pyplot Error "ImportError: cannot import name '_path'"

I came across an ImportError in the importing of matplotlib.pyplot on Windows 10 Pro
Here is the full error:
Traceback (most recent call last):
File "C:\Users\****\Dropbox\Code\Python\lib_test.py", line 1, in <module>
import matplotlib.pyplot as plt
File "C:\Users\****\AppData\Roaming\Python\Python36\site-packages\matplotlib\pyplot.py", line 31, in <module>
import matplotlib.colorbar
File "C:\Users\****\AppData\Roaming\Python\Python36\site-packages\matplotlib\colorbar.py", line 32, in <module>
import matplotlib.artist as martist
File "C:\Users\****\AppData\Roaming\Python\Python36\site-packages\matplotlib\artist.py", line 16, in <module>
from .path import Path
File "C:\Users\****\AppData\Roaming\Python\Python36\site-packages\matplotlib\path.py", line 21, in <module>
from . import _path, rcParams
ImportError: cannot import name '_path'
Here is the script:
import matplotlib.pyplot as plt
squares = [1, 4, 9, 16, 25]
plt.plot(squares)
plt.show()
If anyone could help me, much appreciated.
It can be a problem related to matplotlib version. I had the same problem and solved it uninstalling the existing version of matplotlib (in my case with conda but the command is similar substituing pip to conda) so:
firstly uninstalling with:
conda uninstall matplotlib (or pip uninstall matplotlib)
and after the successful removal of the existing version (mine was 2.2.2), the following command installed a successive version, that can be specified with '==' after the module name:
conda install matplotlib==3.0.2 (or pip install matplotlib==3.0.2)
and the error disappears.
The uninstall command removed also seaborn installation so it was necessary to install it after matplotlib but it was easy...
Yeah!:)
Since you're probably like me and doing a lot of installing/importing, it can be just a mishap in the original matplotlib install... so try doing the ol' get out, walk around to the back side and give it a kick...
conda uninstall matplotlib
#let it do its thing
conda install matplotlib
#let it do its thing
Then test it on the code you're trying to do and see if it works. In my case, it came down to a "_Path" issue in my original install that messed up things. After the uninstall/install, I was fine.
Dunno what mpip is,, or why,, try plain old pip (or in your case perhaps pip3 ? This may have more to do with how the base Python was installed though,, why I suggest using Anaconda to install all (which automatically installs MPL as well..)

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.

Categories

Resources