Basemap; Jupyter; Instalation

Hello :slight_smile:,

Hope everyone is safe and in a good health condition!

I’m trying to install the Basemap package and already tried two different ways, but with no success. :frowning:

First I tried through the Anaconda Navigator. Environments–Not installed–basemap. Proceeded with its instalation but it stays there processing ad eternum and nothing really happens.

Then I tried downloading the package trough this site: https://www.lfd.uci.edu/~gohlke/pythonlibs/#basemap

I downloaded two versions: ’ basemap‑1.2.1‑cp36‑cp36m‑win_amd64.whl’ and ‘basemap‑1.2.1‑cp36‑cp36m‑win32.whl’. I did so because I was not sure on which one to install, since my Python version is the 3.6, on that I was clear regarding which one to download (the cp36) but then I had a Windows 10 64, but a Python 3.6 32 version. Anyway I installed one, it didn’t worked. I installed another one, it didn’t work either! I did install both files using the cmd and the pip install.

The installation part went well no error occurred. The problem was when I went to use the Jupyter Notebook and this error emerged:

pip install basemap
....

Collecting basemap
Note: you may need to restart the kernel to use updated packages.

Could not find a version that satisfies the requirement basemap (from versions: )
No matching distribution found for basemap

And when I try to use for example the mpl_toolkits.basemap module in Jupyter I get this error:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-13-486e44db2745> in <module>
----> 1 from mpl_toolkits.basemap import Basemap
      2 
      3 safety_pivot = combined.pivot_table(values=['saf_s_11', 'lon', 'lat'],
      4                             index='school_dist', aggfunc='mean',
      5                                    )

ModuleNotFoundError: No module named 'mpl_toolkits.basemap'

I donnu what to do more to fix this issue, and I really needed to put this to work in my Jupyter Notebook in order to finish a Project using the Basemap!! :frowning:

Could anyone help me with this issue??

Kind Regards,

Abel

I see already that the problem probably lies on the Jupyter/Anaconda side, because I can use Basemap with the Python IDLE:

But still nothing on the Jupyter Side:

:neutral_face:

Hi @a.xitas

You have to use Anaconda Prompt (Anaconda CLI) to install the packages in Anaconda. For basemap use command

conda install basemap

Let me know if you need more help.

Hi Prem :slight_smile:,

I actually managed to install the package following your advice. But now I got a different error in Jupyter:

from mpl_toolkits.basemap import Basemap

safety_pivot = combined.pivot_table(values=['saf_s_11', 'lon', 'lat'],
                            index='school_dist', aggfunc='mean', 
                                   )

# safety_avg = combined.groupby('school_dist')['saf_s_11'].mean()

# safety_avg.head() 


# Passing into a list both the Series lat as lon, from our Dataset combined,
# in order to use them and pass them as arguments in our scatter plot of our
# NYC map. Both these Series contain all of the geografic points related to
# the the location of our schools in the NYC map:
lat = safety_pivot['lat'].tolist()
lon = safety_pivot['lon'].tolist()

# Constructing our NYC map, resorting to the Basemap module:
m = Basemap(resolution= 'i',
            projection='merc',
            llcrnrlat=40.496044,
            urcrnrlat=40.915256,
            llcrnrlon=-74.255735,
            urcrnrlon=-73.700272,
            )
m.drawrivers(linewidth=.5, color='k')
m.drawmapboundary(fill_color='#85A6D9')
m.drawcoastlines(linewidth=.4, color='k')
m.fillcontinents(color='g')

# Drawing a scatter plot of the map of the NYC with the safety_avg levels 
# per district:
m.scatter(lon,
          lat,
          s=40,
          c=safety_pivot['saf_s_11'],
          cmap= 'summer',
          zorder=4,
          latlon=True)

plt.show()

Returns:


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-12-486e44db2745> in <module>
----> 1 from mpl_toolkits.basemap import Basemap
      2 
      3 safety_pivot = combined.pivot_table(values=['saf_s_11', 'lon', 'lat'],
      4                             index='school_dist', aggfunc='mean',
      5                                    )

C:\Abel\Python\Anaconda\lib\site-packages\mpl_toolkits\basemap\__init__.py in <module>
    153 
    154 # create dictionary that maps epsg codes to Basemap kwargs.
--> 155 pyproj_datadir = os.environ['PROJ_LIB']
    156 epsgf = open(os.path.join(pyproj_datadir,'epsg'))
    157 epsg_dict={}

C:\Abel\Python\Anaconda\lib\os.py in __getitem__(self, key)
    676         except KeyError:
    677             # raise KeyError with the original key value
--> 678             raise KeyError(key) from None
    679         return self.decodevalue(value)
    680 

KeyError: 'PROJ_LIB'

I was googling it and I found that it is quite a frequent thing to happen…

Thank you very much for you kindful help!

I ran this command in the Anaconda Prompt:

conda install -c conda-forge proj4

It installed, but the error persists… :frowning:

Hi Abel,

This error comes because of not getting an epsg file from pyproj_datadir . PROJ_LIB is an environment variable and contains the position of the directory where the epsg file is present and it’s value assigned to pyproj_datadir variable.

So, this error comes due to not getting the value of the PROJ_LIB variable.

What we have to do is assigning the correct directory location of epsg file to variable pyproj_datadir directly or via setting the environment variable PROJ_LIB.

First, we have to find the epsg file in the Anaconda folder. For that, you can run the below command in cmd (Windows 10).

where /r "C:\Abel\Python\Anaconda" epsg.*

In return, you get the location of epsg files in Anaconda directory. Copy that location. You can remove this error in two ways.

Location something like this "C:\Abel\Python\Anaconda\pkgs\proj4-5.2.0-ha925a31_1\Library\share\epsg"
It may differ.

Copy the location up to the share basically, excluding the epsg file name.

  1. You can set the value of the environment variable PROJ_LIB equal to the location you copied.

    In Windows 10:

    Search “Edit the system environment variables” in the start menu. Then click the “Environment Variables…” button. Add a new environment variable using the New button where in this case, Variable name is PROJ_LIB, and Variable value is the location you copied.

    For other OS, just google.

  2. You can assign the value (as a string) of pyproj_datadir directly equal to the location you copied in this file C:\Abel\Python\Anaconda\lib\site-packages\mpl_toolkits\basemap_init_.py .

I would suggest trying the first way setting the environment variable initially, if not work, then go for the second one.

Let me know the status, which one works for you :slightly_smiling_face: .

3 Likes

UUUUUUUuuuuuuuuuuhhhhhhhhhhhhhhh IT’S FIXED!!! :smiley: :smiley: :smiley:

Thank you very much for your help, Prem :wink: , really :slight_smile:.

I must add that after trying your first approach I was still running into this error, now a different one:

OSError: Unable to open boundary dataset file. Only the 'crude' and 'low', resolution datasets are installed by default. If you are requesting an, 'intermediate', 'high' or 'full' resolution dataset, you may need to download and install those files separately with `conda install basemap-data-hires`.

So I googled it, and I thought to myself, hhhmmm, maybe the ‘machine’ is right. So I immediately opened the Anaconda Prompt, and typed the command:

conda install basemap-data-hires

Than typed yes to the first option, it started to install 3 different ‘packages’. After it finished I ran the same file in Jupyter, refreshed it, ET VOILÀ, MY MAP APPEARED!!! ;D

So I guess that after your solution all I needed to do more was this final thing :wink:

Once again, Prem, OBRIGADO :slight_smile:.

Abraço

3 Likes

I already suffered from this situation that’s why I know how to resolve it. No Problem :smile: .

Stay Safe and Happy Learning :slightly_smiling_face: .

@Prem you are a Godsend, sir! This fix worked great for me, after going around in circles and becoming extremely confused on stackoverflow :grimacing:

I would also add that you need to restart Anaconda and not just shutdown/restart the notebook before these changes take effect.

Once again, thank you so much Prem!

1 Like

I just shared my solution with the community. Glad that it helps. :smile:

Hello,

I am having the following errors when trying to install Basemap. I am using a Mac and Jupyter Notebook through Anaconda.

Typed in Terminal: conda install basemap
Typed in Terminal: pip install basemap
Result: does nothing

Typed in Jupyter: from mpl_toolkits.basemap import basemap
Got error: ModuleNotFoundError: No module named ‘mpl_toolkits.basemap’

In Anaconda: went to Environments, searched for basemap, clicked basemap, clicked apply
Got error: No packages will be modified

UnsatisfiableError: The following specifications were found

to be incompatible with the existing python installation in your environment:

Specifications:

  • basemap -> python[version=’>=3.5,<3.6.0a0|>=3.9,<3.10.0a0’]
  • mpl_sample_data -> python[version=’>=3.9,<3.10.0a0’]

Your python: python=3.8

If python is on the left-most side of the chain, that’s the version you’ve asked for.

When python appears to the right, that indicates that the thing on the left is somehow

not available for the python version you are constrained to. Note that conda will not

change your python version to a different minor version unless you explicitly specify

that.

The following specifications were found to be incompatible with each other:

Output in format: Requested package -> Available versions

Package ncurses conflicts for:

python=3.8 -> readline[version=’>=7.0,<8.0a0’] -> ncurses[version=‘6.0.*|>=6.0,<7.0a0’]

python=3.8 -> ncurses[version=’>=6.1,<7.0a0|>=6.2,<7.0a0’]

Package setuptools conflicts for:

basemap -> matplotlib-base[version=’>=1.0.0,!=3.0.1’] -> setuptools

mpl_sample_data -> matplotlib-base[version=’>=3.3.2,<3.4.0a0’] -> setuptools

python=3.8 -> pip -> setuptools

Package matplotlib-base conflicts for:

basemap-data-hires -> basemap[version=’>1.0.7’] -> matplotlib-base[version=’>=1.0.0|>=1.0.0,!=3.0.1’]

basemap -> matplotlib[version=’>=1.0.0’] -> matplotlib-base[version=‘3.1.2|3.1.2|3.1.2|3.1.3|3.1.3|3.1.3|>=3.2.1,<3.2.2.0a0|>=3.2.2,<3.2.3.0a0|>=3.3.1,<3.3.2.0a0|>=3.3.2,<3.3.3.0a0’,build=‘py37h9aa3819_1|py36h9aa3819_1|py36h9aa3819_0|py37h9aa3819_0|py38h9aa3819_0|py38h9aa3819_1’]

mpl_sample_data -> matplotlib-base[version=‘3.1.2|3.1.2|3.1.2|3.1.3|3.1.3|3.1.3|>=3.2.1,<3.3.0a0|>=3.2.2,<3.3.0a0|>=3.3.1,<3.4.0a0|>=3.3.2,<3.4.0a0’,build=‘py37h9aa3819_1|py36h9aa3819_1|py36h9aa3819_0|py37h9aa3819_0|py38h9aa3819_0|py38h9aa3819_1’]

basemap -> matplotlib-base[version=’>=1.0.0|>=1.0.0,!=3.0.1’]

Package libcxxabi conflicts for:

basemap -> libcxx[version=’>=4.0.1’] -> libcxxabi==4.0.1[build=‘hebd6815_0|hcfea43d_1’]

python=3.8 -> libcxx[version=’>=4.0.1’] -> libcxxabi==4.0.1[build=‘hebd6815_0|hcfea43d_1’]

Package kiwisolver conflicts for:

mpl_sample_data -> matplotlib-base[version=’>=3.3.2,<3.4.0a0’] -> kiwisolver[version=’>=1.0.1’]

basemap -> matplotlib-base[version=’>=1.0.0,!=3.0.1’] -> kiwisolver[version=’>=1.0.1’]

I’ve searched online and the solutions are over my head. I’ve read Prem’s solution but it looks like its for Windows but I have a Mac.

Any help would be appreciated.

Thank you!