"ImportError: cannot import name" with fresh Anaconda install - python

I am receiving the following error in an iPython notebook:
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-06ac68ebf148> in <module>()
5 import pandas as pd
6 import numpy as np
----> 7 from sklearn import neighbours
8
9 start_time = time.time()
ImportError: cannot import name neighbours
Whilst trying to run the following:
# Import required packages
import time
import pandas as pd
import numpy as np
from sklearn import neighbours
I get the same issue on the command line. My suspicions:
I believe my problem has something to do with my system path or environment variables. I've given this various attempts and have followed other answers (e.g. Python ImportError- what is wrong here?) but in vain thus far. My current system Path variable includes the location of my python script; the environment PATH variable includes the location of the Anaconda libraries: "...\Anaconda2\Library\bin;". Does this appear correct?
Many solutions refer to circular dependencies, which I do not believe I have in the one module.
Other solutions are to do with the name of the script. Mine is named 'my_solution', so I do not believe this is an issue.
My sklearn folder can be found in \AppData\Local\Continuum\Anaconda2\Lib\site-packages\sklearn
I have recently uninstalled a standalone Python application. I have also reinstalled Anadonda and the scikit-learn, numpy and scipy packages multiple times.
I am using Python 2.7.11 through Anaconda 2.5.0 on Windows 7.

You want the American spelling of neighbours, i.e.
from sklearn import neighbors

Related

Facing ImportError: cannot import name 'ModelParameters' (Python)

I am using the module ydata_synthetic (github link) for generating synthetic timeseries datasets. After installing the package, I ran the following commands:
from os import path
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from ydata_synthetic.synthesizers import ModelParameters
from ydata_synthetic.preprocessing.timeseries import processed_stock
from ydata_synthetic.synthesizers.timeseries import TimeGAN
I am receiving the following error:
ImportError Traceback (most recent call last)
<ipython-input-11-9f2f25e511c0> in <module>
4 import matplotlib.pyplot as plt
5
----> 6 from ydata_synthetic.synthesizers import ModelParameters
7 from ydata_synthetic.preprocessing.timeseries import processed_stock
8 from ydata_synthetic.synthesizers.timeseries import TimeGAN
ImportError: cannot import name 'ModelParameters'
How can I solve this error?
Please make sure, that the virtual environment, where you installed the package is the same one where you try to execute your code. In case of venv or anaconda, you need to explicitly activate it in the console/terminal, then use pip to install your package and then also use that activated environment to execute your python script. If you are using an IDE, you need to set your IDE python interpreter to that specific new environment you created and installed the packe in.
Unexpected solution I found:
Use Python 3.7 (not 3.6, or 3.8+).
It seems like much of the code requires features that were new to 3.7 (eg namedtuple with default values), but the codebase has not yet been adjusted to run with python 3.8 or later.

Easy install python modules for Jupyter notebooks?

Lately I've been learning a bit about Python. Jupyter notebooks seem like a good idea in theory, in practice I'm having some difficulty setting up the python modules within a Jupyter environment.
For example, I found this neat geographical data article and I want to follow along. There is a very nice github project that I downloaded to follow along.
Start up Jupyter and everything looks in order until I run the first code block:
import pandas as pd
import re
from datetime import datetime
from dateutil.parser import parse
import missingno as msno
import numpy as np
import seaborn as sb
import matplotlib.pyplot as plt
import json
This immediately generates an error:
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-1-422e89229f53> in <module>
----> 1 import pandas as pd
2 import re
3 from datetime import datetime
4 from dateutil.parser import parse
5 import missingno as msno
ModuleNotFoundError: No module named 'pandas'
There are quite a few modules and I guess I can go into the command line to add them each individually. That doesn't seem efficient. Is there some other 'Jupyter' or 'Conda' way to do this?
Within Jupyter notebooks you can execute command line methods using ! at the start of the line e.g.
! conda install pandas --yes
More detail on the inner working is discussed in this question:
Running interactive command line code from Jupyter notebook
Thanks to #PaulH for pointing out the need to add the --yes flag.
just run this in Jupyter:
!pip install <the name of the module>, <the name of the module>, <the name of the module>...
this will install the module you choose.
For example :
!pip install pandas, re, ...

is fastai.structured still a part of fast ai library

Why i am getting the error fastai.structured is not a module?.
i have tried installing previous versions of fastai. but nothing helped.
from fastai.imports import *
from fastai.structured import *
#from pandas_summary import DataFrameSummary
from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
from IPython.display import display
from sklearn import metrics
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-17-35432a48f631> in <module>()
1 from fastai.imports import *
----> 2 from fastai.structured import *
3
4 #from pandas_summary import DataFrameSummary
5 from sklearn.ensemble import RandomForestRegressor, RandomForestClassifier
ModuleNotFoundError: No module named 'fastai.structured'
---------------------------------------------------------------------------
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
That module is no longer included in fastai's default python packages. Your default setup commands for using fastai packages will no longer includes that module. You may need to download it from the GitHub master, save it to your working directory, and import from your working directory to your jupyter notebook.
Here's a note from the fastai forum:
The structured.py has been moved to folder “old” (in anticipation to fastai_v1).
https://github.com/fastai/fastai/blob/master/old/fastai/structured.py
--- Andrei Oct '18
When importing from your working directory:
from structured import *
This will replace:
from fastai.structured import *
The module’s name has been changed to “tabular”. So use “from fastai.tabular import *” instead.
see this >> https://forums.fast.ai/t/modulenotfounderror-no-module-named-fastai-structured/36904

pathlib library error in pathlib.Path.home() : type object 'Path' has no attribute 'home'

So I've just updated matplotlib to version 3.0.2 . I can import matplotlib just fine, but when I try to import matplotlib.pyplot, I get :
---------------------------------------------------------------------------
AttributeError
Traceback (most recent call last) <ipython-input-3-864e826dab68> in <module>()
----> 1 import matplotlib.pyplot
/usr/local/lib/python3.6/dist-packages/matplotlib/pyplot.py in <module>()
30 from cycler import cycler
31 import matplotlib
---> 32 import matplotlib.colorbar
33 import matplotlib.image
34 from matplotlib import rcsetup, style
/usr/local/lib/python3.6/dist-packages/matplotlib/colorbar.py in <module>()
30 import matplotlib.collections as collections
31 import matplotlib.colors as colors
---> 32 import matplotlib.contour as contour
33 import matplotlib.cm as cm
34 import matplotlib.gridspec as gridspec
/usr/local/lib/python3.6/dist-packages/matplotlib/contour.py in <module>()
16 import matplotlib.colors as mcolors
17 import matplotlib.collections as mcoll
---> 18 import matplotlib.font_manager as font_manager
19 import matplotlib.text as text
20 import matplotlib.cbook as cbook
/usr/local/lib/python3.6/dist-packages/matplotlib/font_manager.py in <module>()
133
134 if not USE_FONTCONFIG and sys.platform != 'win32':
--> 135 OSXFontDirectories.append(str(Path.home() / "Library/Fonts"))
136 X11FontDirectories.append(str(Path.home() / ".fonts"))
137
AttributeError: type object 'Path' has no attribute 'home'
I'm on Ubuntu 18.04 LTS, on Jupyter Lab 0.35.4 with python3.6.7.
Side information/question : Before installing jupyter lab this morning, I was using jupyter notebook with python3.6.0. Now the kernels says it is using python3.6.7, although I cannot seem to find it anywhere on my system.
That being told, when I import anything else that doesn't rely on matplotlib.pyplot, everything works perfectly. If I try seaborn, for example, it returns me to the same attribute error.
EDIT In fact, the error happens with the pathlib library. It also happens whether or not I'm in jupyter. To replicate it :
from pathlib import Path
Path.home()
and the error is the same as before :
AttributeError: type object 'Path' has no attribute 'home'
I hit this recently where matplotlib was trying to import and use pathlib and failing because my virtualenv was 3.6 but it was somehow getting a 3.4 pathlib and hitting either the exception you hit or another exception where the mkdir() it was calling was missing the exists_ok optional parameter.
It turned out that one of the modules I was using, openapi-spec-validator version 0.2.6, downloads a copy of pathlib (of the 3.4 type) and sticks it in the base site-packages folder where it can easily end up being used by matplotlib instead of the standard library version. In my case upgrading to 0.2.7 fixed it as that version doesn't download and place the file there.
However, if you're not using openapi-spec-validator and you hit this problem check to see if there are extra copies of pathlib.py floating around your environment.
If your environment include multiple versions of python, check which version of pathlib your environment is currently using by pip show.
pip show pathlib
If it is pointing the pathlib of python <3.5, modify the PYTHONPATH to point pathlib of expected version of python.
You can further check the list of directory of python package by sys.path. If it shows directories of an unexpected version of python earlier than that of an expected version, that's the reason why your environment import older pathlib.
import sys
sys.path
I was able to fix this problem by performing pip uninstall pathlib while in the environment from which I using python and getting this error.
This should work for anyone using python >=3.5
The cause of the problem is that from python 3.5 and later, pathlib comes bundled with all the standard libraries of python. This will be the newer version that has Path.home(). However, if for some reason, like me, you have pathlib also installed as an independent package via pip, it will be the older version that doesn't have pathlib.Path.home(), and therefore breaks when home() is called.
Before uninstalling pathlib with pip uninstall pathlib make sure that:
You actually have python >= 3.5 by running python --version.
That you actually have a redundant installation of the "old" pathlib by running pip show pathlib
what about the classic trick of uninstalling matplotlib and installing it again?
pip uninstall matplotlib==3.0.2
pip install matplotlib==3.0.2
Also, if you are not doing it already, I strongly suggest you should check using virtual environments (e.g. this guide) as you will have absolute control on which packages you install/modify/uninstall in a particular virtualenv (not affecting your development environment for all your projects).

python newbie - how to import library

I installed the 'pandas' module and now I need to run it at my command line, but it is not available. How to make it available?
In [1]: import pandas as pd
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-af55e7023913> in <module>()
----> 1 import pandas as pd
ImportError: No module named 'pandas'
In [2]:
Your module can't be found in the current directory, in pythons site-packages folder or in any path defined in the $PYTHON_PATH environment variable.
If you installed pandas via pip install pandas and encounter an ImportError, you most likely have a problem due to multiple python distributions installed on your system.
Look carefully at the output of pip install pandas and examine the folders in its output. Find out which executable you are running when calling python via which python and look at the version numbers.
This is a very common problem btw. so prepare for a close of this question.

Categories

Resources