Why won't Jupyter Notebook load a module? - python

I'm using Windows 10, anaconda navigator, and jupyter notebook 5.7.4 throws an error when I try to import 'basemap' using "from mpl_toolkits.basemap import basemap"
I'm very new to python. Following a tutorial to learn GIS mapping with python, I've installed all the required packages: pandas, numpy, geopandas, basemap, matplotlib, pillow and a few others.
The tutorial code says to write "from mpl_toolkits.basemap import basemap"
However, mpl_toolkits does not appear to be an available package when I search for it in the anaconda navigator or in the anaconda prompt.
Having searched the anaconda user guide and the matplotlib web page, I am inferring that mpl_toolkits is included within matploblib?
The error that results is this: ImportError: cannot import name 'basemap' from 'mpl_toolkits.basemap' (C:\Users\Kevin\AppData\Local\conda\conda\envs\mapping\lib\site-packages\mpl_toolkits\basemap__init__.py)
# command to display matplotlib plots inline within the ipython notebook
%matplotlib inline
# import necessary modules
import numpy as np, matplotlib.pyplot as plt, pandas as pd, geopandas as
gpd
from mpl_toolkits.basemap import basemap
from shapely.geometry import Point
from geopy.distance import great_circle
I expect the jupyter notebook to load all of the modules but instead I get the above error.
Any help is much appreciated, this is my first go at something like this so I am probably making a simple neophyte mistake.

Seems this is very peculiar to Anaconda. If you correctly installed all the dependencies you should check this Python basemap module impossible to import

Related

How do I run a Python Script in PowerBI?

I am new to Python. I am trying to create a Bar Chart in Power BI using Python but the script does not seem to work. The code is below:
pip install pandas
pip install matplotlib
import matplotlib.pyplot as plt
plt.plot(dataset.Letter,dataset.Number)
plt.show()
Please find attached the file:
https://www.mediafire.com/file/f0bwt9cp4ha1sfb/Example.pbix/file
enter image description here
After installing only one (1) initial time pip install pandas and pip install matplotlib, you only need to write in your code:
import matplotlib.pyplot as plt
dataset.plot('bar', dataset.Letter, dataset.Number)
plt.show()
import is for setting up the library and you must always use it in your code, while pip install pandas and pip install matplotlib only need to be installed once at the beginning.
First, you should not be required to run pip install every time you run your script. You should only have to use pip install commands when installing a library for the first time. This might require you ensuring that you have the correct libraries located on your computer. However, I do not know what your error is since I do not have your error code.
Also, be sure to specify what type of plot you wish to produce from matplotlob.
Try (EDIT):
import matplotlib.pyplot as plt
dataset.plot(kind='bar', x='Letter', y='Number')
plt.show()
You do not need an indent in your last two lines of code.
Also after reviewing you update I think it might be best if yo review the matplotlib library documentation and familiarize yourself with Python basics. You can learn Python almost anywhere, I have found the interactive lessons on w3schools.com to be useful.
matplotlib: https://matplotlib.org/stable/users/index.html

How do I resolve this AttributeError when using eccodes/grib?

I'm working with cfgrib on macOS and I'm getting the following error when I try to use covert a grib file into xarray:
AttributeError: module 'pyeccodes.compat' has no attribute 'codes_grib_multi_support_off'
Here's the code I have so far:
import xarray as xr
import matplotlib.pyplot as plt
import matplotlib.style as sty
sty.use('classic')
import numpy as np
import cartopy.crs as ccrs
d7 = xr.open_dataset('2007_BLH.grib', engine='cfgrib')
I've downloaded everything that seems to be needed, including eccodes, pyeccodes, cfgrib. How can I resolve this issue?
Install the ecCodes binary with the package manager, see: https://github.com/ecmwf/cfgrib#binary-dependencies.
I had the same error on a Windows 10 machine using Anaconda3, but hopefully my resolution will be helpful.
I think the issue was that I was installing metpy before cfgrib. It appears metpy set up some packages that conflicted with cfgrib because, after much trial and error, I created a fresh virtual python environment and resolved the issue doing the following:
conda install -n new_env cfgrib //install the package into my new virtual environment
Set up eccodes definitions path in environment variable...
Variable name: ECCODES_DEFINITION_PATH
Variable Value: C:\Users\path\to\eccodes\definitions
I imagine this would look something like ECCODES_DEFINITION_PATH=./path/to/eccodes/definitions for your MacOS.

Can import python package in r while the package exists for other python interpreters

With the r package reticulate, I attempted to import the python package scipy to do my data analysis. However, it says
ModuleNotFoundError: No module named 'scipy'.
But when I use jupyter notebook, clearly I can import scipy so my computer has the package. Somehow RStudio is not importing it.
This is what I did:
library(reticulate) #import reticulate to enable python usage in r
use_python("/usr/local/bin/python3", required = TRUE) #set the path for where my python is.
from scipy.optimize import minimize #try to import the package in a python block
Then I get the error package not found. I tried so many things this afternoon but failed
First, I tried to install again using r studio with py_install("scipy"). It installs, but when I try to import again, it still reports not module found.
Second, I tried to create virtual environments by conda_create('r-reticulate', packages = "python=3") and then py_install("scipy"). Doesn't help.
Third, I upgraded the python to python 3.7. Now not only the scipy package, but all python packages like numpy, pandas also could not be imported. Of course they can still be imported in Jupiter notebook so it's not the case that my computer does not have them.
I tried the previous two things again but no help.
library(reticulate)
use_python("/usr/local/bin/python3.7", required = TRUE)
from scipy.optimize import minimize
import matplotlib.pyplot as plt # for plotting
import numpy as np # for numeric calculations
import pandas as pd # for python data frame
import time
I expect to successfully import all the python packages to RStudio. And figure out a way to install new python packages and import them to r.

Import package works in jupyter notebook but not in iPython and Anaconda cmd

When searching about this issue, I came across some questions asking the opposite, i.e., package opens in iPython but not in Jupyter Notebook. But in my case, its the opposite. That's why I posted this question.
I added path\to\anaconda3 and path\to\anaconda3\Lib\site-packages in the environment variable, but it doesn't solve the issue.
I can see the packages in the site-packages folder:
But I just can't import some of the packages in iPython:
or with python in the anaconda cmd:
But it works fine in Jupyter Notebook:
What do/can I do to fix this?
Here's some more info if it helps:
(base) C:\Users\h473>where python
C:\Users\h473\AppData\Local\Continuum\anaconda3\python.exe
(base) C:\Users\h473>where conda
C:\Users\h473\AppData\Local\Continuum\anaconda3\Library\bin\conda.bat
C:\Users\h473\AppData\Local\Continuum\anaconda3\Scripts\conda.exe
(base) C:\Users\h473>where pip
C:\Users\h473\AppData\Local\Continuum\anaconda3\Scripts\pip.exe
P.S.: It doesn't happen for all packages, only some packages, as shown for pandas, numpy and matplotlib in the screenshot below.
When you are using matplotlib (and seaborn is built on top of it) it needs to use a so called backend that is used to display the actual GUI with the plot in it once you execute for example matplotlib.pyplot.show().
When you are running a Jupyter Notebook with matplotlib in inline mode (default I think, but not sure), then a Jupyter specific backend is used (module://ipykernel.pylab.backend_inline). That makes sense, since the plots should not appear in separate windows, but be displayed inside the notebook itself.
When you are in an interactive python or iPython session however, Qt5 was used as
import matplotlib
print(matplotlib.rcParams["backend"]) # this prints the backend that would be loaded when trying anything with pyplot
has revealed. Since you get the error youa re getting, it looks like your QT5 installation is broken. You can try to reinstall them using the conda commands, but for now you could also fall back to using a different backend, that you need to specify before trying to load seaborn:
import matplotlib
matplotlib.use("TkAgg") #use backend TkAgg
import seaborn
You can also change the default backend being loaded to TkAgg by creating a matplotlibrc file in C:\Users\<your name>\.matplotlib\ with
backend : TkAgg
in it.

how to solve error module sklearn.cluster?

i want to make recommendation location but i have problem with my sklearn. i have been update my library but it is not work. i use python 2.7 with anaconda
please help me :D
it is my library code.
from sklearn.cluster import KMeans
import numpy as np
import pandas as pd
from sklearn.cross_validation import train_test_split
from sklearn.metrics import accuracy_score, recall_score, precision_score
from sklearn import svm
from numpy import algorithms, environment
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
Windows
Open command prompt(as admin)
enter 'pip install -U scikit-learn'
Unix
Open terminal
Enter 'sudo pip install -U scikit-learn'
The default Anaconda distribution should have all of these packages, so it's likely your interpreter is looking for packages in a different spot. This is controlled by the PYTHONPATH system variable, which tell it where to look for package imports.
Anaconda can set this correctly for you during (re)installation if you choose to update the variable. You can also edit it yourself--how you do so depends on your OS.
To view the variable in python for troubleshooting:
How do I find out my python path using python?
This should point to a directory on you computer containing the package files.

Categories