Python basemap module impossible to import
Asked Answered
R

18

49

I have troubles to import the basemap module of mpl_toolkits in python. Here is what I get when I run the test.py script from the module directory:

/usr/lib/python2.7/dist-packages/mpl_toolkits/basemap$ python test.py
Traceback (most recent call last):
File "test.py", line 1, in <module>
from mpl_toolkits.basemap import Basemap, shiftgrid
ImportError: No module named basemap

I can't get it since sys.path gives a list of paths where I am sure the directory "basemap" is, in the "mpl_toolkits" directory. There is no problem to import mpl_toolkits. Here is a thing I tried, to manually add the path, and the result:

>>> import sys
>>> sys.path.append('/usr/lib/python2.7/dist-packages/mpl_toolkits/basemap')
>>> import basemap
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "basemap/__init__.py", line 30, in <module>
from mpl_toolkits.basemap import pyproj
ImportError: No module named basemap

I tried to uninstall an reinstall basemap from source (carefully following these instructions), from apt-get, from conda, but it does not change anything: I can't import basemap.

Thank you for your help

Ruthenious answered 2/11, 2016 at 7:25 Comment(2)
in which environment did you install the module?Pyroxene
Did you manage to solve this? I am facing the same issueUbana
T
37

I was facing this issue and I was able to solve it using anaconda

After activating my profile

source activate MyProfileName
conda install basemap

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
# setup Lambert Conformal basemap.
# set resolution=None to skip processing of boundary datasets.
m = Basemap(width=12000000,height=9000000,projection='lcc',
            resolution=None,lat_1=45.,lat_2=55,lat_0=50,lon_0=-107.)
m.bluemarble()
plt.show()

BlueMarble format of basemap

Trudey answered 20/3, 2017 at 6:24 Comment(0)
J
48

I was in the same situation until a minute ago, installing it through this made the trick:

sudo apt-get install libgeos-3.5.0
sudo apt-get install libgeos-dev
pip install https://github.com/matplotlib/basemap/archive/master.zip
Jeer answered 6/11, 2016 at 23:44 Comment(7)
I googled and tried everything I could to get 'from mpl_toolkits.basemap import Basemap' to actually work and your answer was the only one that was successful for me. I am using python 3.6 and I only had to run the pip install line. Anyhow, thanks!Chasseur
There are a few other solutions for this problem that are available in Stackoverflow but this is perhaps the only one that works on Google's Colab platform. Hence this solution is very useful.Catercousin
I don't actually see why this should work any differently than running python setup.py build and sudo python setup.py install in a shallow clone of the repository. What's the magic sauce? Is it the specific version of libgeos?Dhruv
This worked without the 'sudo apt-get install libgeos-3.5.0'Jackleg
You sir, are the MVP. Had to change libgeos-3.5.0 to libgeos-3.6.2 though (but it is 3.5 years later).Thaler
DO NOT use sudo pip! Source, source, source.Jardena
This gives me the error: E: Unable to locate package libgeos-3.5.0 E: Couldn't find any package by glob 'libgeos-3.5.0' E: Couldn't find any package by regex 'libgeos-3.5.0'Jardena
T
37

I was facing this issue and I was able to solve it using anaconda

After activating my profile

source activate MyProfileName
conda install basemap

from mpl_toolkits.basemap import Basemap
import matplotlib.pyplot as plt
# setup Lambert Conformal basemap.
# set resolution=None to skip processing of boundary datasets.
m = Basemap(width=12000000,height=9000000,projection='lcc',
            resolution=None,lat_1=45.,lat_2=55,lat_0=50,lon_0=-107.)
m.bluemarble()
plt.show()

BlueMarble format of basemap

Trudey answered 20/3, 2017 at 6:24 Comment(0)
A
19

I do not use Anaconda, using MacOS, and I have found this solution works for me. I guess it is straight forward, and should work with both Linux and MacOs.

https://mcmap.net/q/356144/-installing-basemap-on-mac-python

brew install geos
pip3 install https://github.com/matplotlib/basemap/archive/master.zip

For Ubuntu,

sudo apt-get install geos
sudo pip3 install https://github.com/matplotlib/basemap/archive/master.zip
Antechamber answered 17/3, 2019 at 7:22 Comment(1)
For RHEL/Centos7 this didn't fully work for me. What worked was: sudo yum -y install geos-devel; wget https://github.com/matplotlib/basemap/archive/refs/heads/master.zip; unzip master.zip; cd basemap-master/packages/basemap; python -m pip install .; Test command: python -c "from mpl_toolkits.basemap import Basemap"Hovey
J
15

For me, a simple

pip3 install basemap

did the trick.

Jardena answered 7/2, 2022 at 12:11 Comment(3)
Yes, that worked for me too! (on windows environment) A simple pip install basecamp followed by from mpl_toolkits.basemap import BasemapRegine
Appears to have worked for me on an M1 Mac. Thanks! I did get a warning re boundary dataset file, and instructions to download high and full resolution datasets from the basemap-data-hires package. I grabbed this here: pypi.org/project/basemap-data-hiresSusi
If you've already installed geos and pip install basemap still isn't working, make sure that your GEOS_DIR environment variable is pointing to the right place. In my case, running the latest version of geos installed via Homebrew on macOS, that requires running export GEOS_DIR=/usr/local/Cellar/geos/3.11.0/ (or set -Ux GEOS_DIR /usr/local/Cellar/geos/3.11.0/ if you use Fish)Lamellibranch
U
12

I had the same issue; trying to access basemap using sys would produce that error. But this worked for me:

import mpl_toolkits
mpl_toolkits.__path__.append('/usr/lib/python2.7/dist-packages/mpl_toolkits/')
from mpl_toolkits.basemap import Basemap
Ubana answered 5/4, 2017 at 15:0 Comment(2)
I hate it when you have a problem and people ignore it and just say "just add UNKNOWN REPOSITORY to your trusted sources and install xyz". that's not a true solution, it's a dirty workaround (and even the accepted answer, lol). I'm not sure about the cause of the problem but this one seems to tackle the problem by its root. thanks!Youngs
This seems like it should be the solution. Lesson learned - scroll down to see all answers! Just spent ~30m looking at other workarounds :(Bib
G
3

Download it from here and install it manually. Make sure to download the right version(i.e. if you are Python3.6 then download basemap‑1.2.0‑cp36‑cp36m‑win_amd64.whl)

Reference: https://mcmap.net/q/356145/-how-to-install-matplotlib-39-s-basemap

Gamine answered 4/2, 2019 at 14:28 Comment(0)
S
3

conda install -c anaconda basemap

OR

Go to Anaconda Navigator, search for basemap and click Apply.

Anaconda Navigator Screenshot

enter image description here

Studner answered 20/12, 2019 at 15:17 Comment(0)
W
3

In my Case It works

sudo apt install python3-mpltoolkits.basemap

Weil answered 12/7, 2020 at 18:3 Comment(0)
F
2

This will help you :

!pip install basemap
!pip install basemap-data

you can check this notebook for more information:

for more examples of machine learning algorithms please visit:

Forevermore answered 4/7, 2022 at 17:29 Comment(0)
L
1

I followed this answer:

https://mcmap.net/q/349646/-python-basemap-module-impossible-to-import

Though, I can have wrongly installed mpl_toolkits (or I do not know), in my case I found out, that basemap is located in pymodules like that:

    mpl_toolkits.__path__.append('/usr/lib/pymodules/python2.7/mpl_toolkits/')
    from mpl_toolkits.basemap import Basemap

So this worked for me on ubuntu 14.04 LTS.

Loss answered 19/9, 2018 at 22:58 Comment(0)
C
1

After a day of trying different methods, this worked for me:

conda install -c conda-forge basemap
Champac answered 25/8, 2020 at 22:34 Comment(0)
X
1

The only solution that worked for windows.

Download wheel package from here: https://www.lfd.uci.edu/~gohlke/pythonlibs/#basemap Navigate to download location in terminal. Install using pip install

Xanthochroid answered 4/2, 2021 at 16:59 Comment(0)
D
0

I was able to get basemap working through the following steps. Note that I did a --user install.

  1. Create shallow clone of basemap (git clone --depth 1 [email protected]:matplotlib/basemap.git) or extract the tarball of the current version.
  2. Install the necessary prerequisite libraries (on Ubuntu, libgeos-dev, libproj-dev, libgeos++-dev, proj-data, proj-bin, libgeos-c1v5, libgeos, libproj12, I think).
  3. pip install --user pyproj matplotlib geos (not actually sure if geos is necessary).

Now, here's where I had to improvise a little bit. When I install basemap using python setup.py install, it creates a new egg directory among my Python packages. That directory contains an mpl_toolkits subdirectory which duplicates a separate mpl_toolkits directory installed by matplotlib.

So, instead, I did

python setup.py build_ext --inplace
cp -a lib/mpl_toolkits/basemap /my/python/packages/dir/mpl_toolkits/basemap
cp lib/_geoslib.so /my/python/packages/dir

I am now able to run examples like simpletest.py.

Dhruv answered 5/9, 2018 at 18:16 Comment(0)
P
0

I faced the same problem. Couldn't import the Basemap:

from mpl_toolkits.basemap import Basemap

but surprisingly Pycharm saw the library which was apparently installed in this location:

lib/python3.8/site-packages/basemap-1.2.1-py3.8-linux-x86_64.egg/mpl_toolkits/basemap/

So, to make it work, I just created a symbolic link in lib/python3.8/site-packages/mpl_toolkits/:

ln -s ../basemap-1.2.1-py3.8-linux-x86_64.egg/mpl_toolkits/basemap/ basemap

I know it's not a clean solution, but may be sufficient in some cases.

Parian answered 14/1, 2021 at 14:46 Comment(0)
L
0

On Colab you might have to add the basemap module to mpl_toolkits

!pip install basemap
from mpl_toolkits import basemap

At least, I had the same problem and this worked for me.

Leshalesher answered 4/3, 2022 at 13:39 Comment(0)
O
0

For Windows:

To install basemap you need to download the wheel file from this path:

https://www.lfd.uci.edu/~gohlke/pythonlibs/#basemap

for python 3.10 this will be the file:

basemap‑1.3.2‑cp310‑cp310‑win_amd64.whl

Then you have to go to the folder where the above mentioned .whl file is saved and run this command from there:

pip install basemap‑1.3.2‑cp310‑cp310‑win_amd64.whl


For Mac OS please refer:

https://github.com/matplotlib/basemap/releases:

Make sure you have:

brew install geos
brew install matplotlib
brew install numpy
brew install proj

Add export GEOS_DIR="/user/local/Cellar/geos/3.10.2/" to your .bash profile and reload it via:

source ~/.bash_profile

git clone --depth 1 https://github.com/matplotlib/basemap.git

then in the basemap/packages/basemap directory

pip3 install


For Colab there are slight variation from Akima's answer since its been a while:

!sudo apt-get install libgeos-3.6.2
!sudo apt-get install libgeos-dev
!pip install git+https://github.com/matplotlib/basemap#subdirectory=packages/basemap

enter image description here

enter image description here

Olshausen answered 23/3, 2022 at 5:28 Comment(0)
D
-1

If you're using Anaconda, it has a package for basemap

conda install basemap

(pip doesn't have the basemap package any more)

OR

If you using Ubuntu system you can try

apt install python3-mpltoolkits.basemap

Dundalk answered 1/8, 2019 at 11:36 Comment(0)
P
-1

I had trouble with that. Fortunately, I solved the problem. Firstly, I suggest you to set Anaconda up, if you don't have. Then follow steps below;

  • Create a new environment in anaconda.
  • Install Jupyter Notebook and launch it. If it works, turn it off.
  • Change your anaconda environment in your terminal.
  • Try installation commands below; conda install basemap conda install forge ... pip install basemap
Pictogram answered 13/4, 2022 at 12:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.