Basemap library using Anaconda Jupyter Notebooks - KeyError: PROJ_LIB - anaconda

I'm trying to install and import the Basemap library into my Jupyter Notebook, but this returns the following error:
KeyError: 'PROJ_LIB'
After some research online, I understand I'm to install Basemap on a separate environment in Anaconda. After creating a new environment and installing Basemap (as well as all other relevant libraries), I have activated the environment. But when importing Basemap I still receive the same KeyError.
Here's what I did in my MacOS terminal:
conda create --name Py3.6 python=3.6 basemap
source activate Py3.6
conda upgrade proj4
env | grep -i proj
conda update --channel conda-forge proj4
Then in Jupyter Notebook I run the following:
from mpl_toolkits.basemap import Basemap
Can anyone tell me why this results in a KeyError?

Need to set the PROJ_LIB environment variable either before starting your notebook or in python with os.environ['PROJ_LIB'] = '<path_to_anaconda>/share/proj'
Ref. Basemap import error in PyCharm —— KeyError: 'PROJ_LIB'

In Windows 10 command line: first find the directory where the epsg file is stored:
where /r "c:\Users\username" epsg.*
...
c:\Users\username\AppData\Local\conda\conda\envs\envname\Library\share\epsg
...
then either in command line:
activate envname
SET PROJ_LIB=c:\Users\username\AppData\Local\conda\conda\envs\envname\Library\share
(make sure there are no leading on trailing spaces in the path!) and then
jupyter notebook
or add this to your jupyter notebook (as suggested by john ed):
import os
os.environ['PROJ_LIB'] = r'c:\Users\username\AppData\Local\conda\conda\envs\envname\Library\share'

The problem occurs as the file location of "epsg" and PROJ_LIB has been changed for recent versions of python, but somehow they forgot to update the init.py for Basemap. If you have installed python using anaconda, this is a possible location for your espg file:
C:\Users\(xxxx)\AppData\Local\Continuum\anaconda3\pkgs\proj4-5.1.0-hfa6e2cd_1\Library\share
So you have to add this path at the start of your code in spyder or whatever field you are using.
import os
os.environ['PROJ_LIB'] = r'C:\Users\(xxxx)\AppData\Local\Continuum\anaconda3\pkgs\proj4-5.1.0-hfa6e2cd_1\Library\share'
from mpl_toolkits.basemap import Basemap

If you can not locate epsg file at all, you can download it here:
https://raw.githubusercontent.com/matplotlib/basemap/master/lib/mpl_toolkits/basemap/data/epsg
And copy this file to your PATH, e.g. to:
os.environ['PROJ_LIB'] = 'C:\Users\username\Anaconda3\pkgs\basemap-1.2.0-py37h4e5d7af_0\Lib\site-packages\mpl_toolkits\basemap\data\'
This is the ONLY solution that worked for me on Windows 10 / Anaconda 3.

Launch Jupyter Notebook from command prompt and it won't throw the same error. It somehow works for me!

Related

ModuleNotFoundError: No module named 'mpl_toolkits.basemap'

I have trouble importing the basemap module of mpl_toolkits in python.
I've got following error message when I try to run "from mpl_toolkits.basemap import Basemap":
ModuleNotFoundError: No module named 'mpl_toolkits.basemap'
I'm using python 3.6.5 in windows.
I've found relevant Q&A in
"Python basemap module impossible to import" and I already followed what's instructed there (i.e. source activate MyProfileName, conda install basemap) but it didn't work.
The clue might already be given in the Q&A above but as I'm quite new to python, I couldn't figure out the solution.
I would recommend installing Anaconda environment from scratch. Let Anaconda handle dependencies for you. Then you need to install mpl_toolkits separately in your conda environment with:
conda install -c conda-forge basemap-data-hires
See also here.
After doing this, executing
from mpl_toolkits.basemap import Basemap
from python console should work normally.
I recently faced this problem on Windows 10. I had created a conda environment with python 3.7 and anaconda 5.2.0 and tried all retrieved solutions. But nothing worked for me and all my efforts worth many hours were in vain.
What I observed that currently Basemap is not compatible with python 3.0, so I remove the environment and creates a fresh with python 2.7 and anaconda 5.2.0. So, to help others who are juggling with same problem, here is the complete set of solution:
Getting the Basemap Toolkit (support the functionality of mapping data)
create conda environment as:
conda create -n Basemap python=2.7 anaconda=5.2.0
Activate the created conda environment:
activate Basemap
Install the following packages:
conda install -c conda-forge basemap
(do not use basemap=1.1.0, channel error comes "package basemap =1.1.0 is not
available from current channels")
"Only the 'crude' and 'low' resolution datasets are installed by default". You may
need to install the following for high resolution:
conda install -c conda-forge basemap-data-hires
PROJ is a generic coordinate transformation software that transforms geospatial
coordinates from one coordinate reference system (CRS) to another. This includes
cartographic projections as well as geodetic transformations.
conda install -c conda-forge proj4 </b>
(Better to avoid pro4=5.2.0 as specific packages generates error)
PROJ is a generic coordinate transformation software that transforms geospatial
coordinates from one coordinate reference system (CRS) to another. This includes
cartographic projections as well as geodetic transformations.
After installation of the Basemap toolkit, open the jupyter notebook a fresh, it should show the current environment in upper right corner.
To check the current environment in jupyter, type:
import sys
print(sys.executable)
If still the activated environment is not shown then:
run the following command in anaconda prompt in activated environment Basemap:
python -m ipykernel install --user --name Basemap --display-name "Python (Basemap)"
Now change the environment from Kernel-> change kernel -> Python (Basemap)
Finally Python(Basemap) should be shown in upper right corner.
Following needs to be imported in your program then to use it:
from mpl_toolkits.basemap import Basemap
In case of error: "No module named 'mpl_toolkits.basemap" type the following in jupyter:
import os
os.environ['PROJ_LIB'] = r'C:\ProgramData\Anaconda3\pkgs\proj4-5.2.0-h6538335_1006\Library\share
After this you need to restart apps for them to pick up the change, including explorer.exe. Restarting the machine is reccomended (but not required).
All the best. I hope this will work for you as well.

Graphviz not running in jupyter notebook python = 3.6?

I am trying to run graphviz to view dot file on jupyter notebook in have imported path to dot.exe path.
G:\anaconda3\envs\tensorflowgpu\Library\bin\graphviz
G:\anaconda3\envs\tensorflowgpu\Library\bin\graphviz\dot.exe
I am trying to run this code:
with open("tree1.dot") as f:
dot_graph = f.read()
graphviz.Source(dot_graph)
OR
graphviz.Source("tree1.dot") .view()
It is giving:
failed to execute ['dot', '-Tsvg'], make sure the Graphviz executables are on your systems' PATH
Please tell me any way to fix this or any other way to View dot file on jupyter notebook i am running python 3.6 on jupter notebook
I have been having a similar issue (different execution - but both have the same errors in Jupyter) and was able to resolve it this way:
First I uninstalled the graphviz package I had originally installed via the Anaconda prompt.
conda remove graphviz
I found in this issue thread to use the below command to install the graphviz package. Appending graphviz with python- downloads the package in lib/site-packages
conda install python-graphviz
I restarted Jupyter Notebook in order to find dot.exe since I couldn't get it to find it in my running notebook after installing the package.
I hope this helps!
if you code doesn´t work, you can use this.
import graphviz
from IPython.display import display
with open("tree1.dot") as f:
dot_graph = f.read()
display(graphviz.Source(dot_graph))
Reinstall graphviz
conda remove graphviz
conda install python-graphviz
graphviz.Source(dot_graph).view()
after you will have a small window where you have to choose where you want see your decision tree
the following worked for me (removal in Jupyter via 'conda remove graphviz' didn't work):
go to Anaconda Navigator Console:
-->Environments--> Search for graphviz --> deinstall
-->Environments --> install python-graphviz

Not able to get Python 2 environment in Jupyter Notebook after creating and activating in Anaconda Prompt

Working on my Windows machine, I installed Anaconda 3 and created a Python 2 environment in Anaconda Prompt using commands below:
conda create -n py2 python=2
activate py2
conda install numpy matplotlib pandas scikit-learn
However, when I run jupyter notebook in Anaconda Prompt, in the opened notebook, I checked Python version using this:
import sys
print('Python version is:', sys.version_info)
I found out it is Python 3 instead of Python 2...I wonder what's going on?
It turns out I need to install jupyter notebook like below in Anaconda Prompt as well, otherwise, the default Python 3 version notebook will be called.
conda install jupyter notebook
Now, if we launch jupyter notebook again, we'll get Python 2.
You may also find that you need to install the Notebook extensions which provides Conda environment and package access extension from within Jupyter Notebook. You can do that by running:
conda install nb_conda

FAIL: pandas in python3 jupyter notebook

My apologies in advance - I have read through multiple sites and tried multiple things, but am still having trouble.
On OSX. Am trying to execute "import pandas as pd" in a jupyter notebook with Python 3, but receive the following message:
ModuleNotFoundError: No module named 'pandas'
I created a new conda environment
conda create -n py3x python=3
activated it
source activate py3x
then opened jupyter
jupyter notebook
Did not work.
Tried this, but that seemed to default to Python 2: ImportError: No module named 'pandas'
I've also tried (perhaps?) every iteration from the following: Using both Python 2.x and Python 3.x in IPython Notebook
I have python 3 in jupyter notebook now, but none of the packages from anaconda. I also tried updating Anaconda (had already done Anaconda3 graphical install)
conda update anaconda
And that just said "All requested packages already installed"
Any further thoughts on terminal command lines to help would be much appreciated.

Error no module named tensorflow in Anaconda for mac

I installed tensorflow as described for Anaconda installation in the tensorflow.org. I could run the tensorflow in the Mac terminal just typing,
$ source activate tensorflow
$ python
import tensorflow as tf
But, when I run iPython notebook, it does not work by showing an error of no module named tensorflow. I felt like tensorflow was correctly installed but iPython notebook could not recognize it. Is there any way to fix it?
Please have a look at your path. When you do the command 'source activate tensorflow', your path may have changed. The example is following.
sherrie#sherrie-PC:~/tensorflow/tensorflow/examples/udacity$ source activate tensorflow
discarding /home/sherrie/anaconda2/bin from PATH
prepending /home/sherrie/anaconda2/envs/tensorflow/bin to PATH
Tensorflow is installed in the first path, instead of the second one.
The most important step comes. Open your directory of 'anaconda' --> find the 'envs' --> open 'tensorflow' --> copy all files in 'sitepack0ages' to 'anaconda/lib/python2.7/sitepack-ages'. Done.
My answer is based on this other answer: Trouble with TensorFlow in Jupyter Notebook. I had the exact same problem, and I solved it by doing this:
Once your environment is activated, run "which jupyter" and verify if the path returned points to the /bin folder under your environment, something like /my_environments_path/my_tensorflow_environment_name/bin/jupyter. If this is already the case, your scenario is different from the one I had;
Run "which pip" and/or "which pip3". Odds are, you are executing pip or pip3 from a different location, so Jupyter is not being installed inside your environment. In my case, Python version was 3.6, but pip3 was being called from a different location. I ran "pip install jupyter", since pip was inside the /bin folder in my environment. Once I called the correct pip, I checked again the "which jupyter" command, and this time Jupyter was under my environment and I could import Tensorflow inside my notebooks. If no pip's path points to your environment, install pip inside it by running "conda install pip".
I hope this works, along with the additional information on the link above.

Resources