无法导入的 Python 底图模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40374441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python basemap module impossible to import
提问by Arnaud BUBBLE
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:
我在python中导入mpl_toolkits的底图模块时遇到了麻烦。这是我从模块目录运行 test.py 脚本时得到的结果:
/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:
我无法得到它,因为sys.path
在“mpl_toolkits”目录中给出了我确定目录“basemap”所在的路径列表。没有问题import mpl_toolkits
。这是我尝试过的一件事,手动添加路径和结果:
>>> 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 theseinstructions), from apt-get, from conda, but it does not change anything: I can't import basemap.
我试图从源(仔细按照这些说明)、apt-get、conda卸载重新安装的底图,但它没有改变任何东西:我无法导入底图。
Thank you for your help
感谢您的帮助
回答by Akima
I was in the same situation until a minute ago, installing it trough this made the trick:
直到一分钟前,我还处于相同的情况,通过它安装它就成功了:
sudo apt-get install libgeos-3.5.0
sudo apt-get install libgeos-dev
sudo pip install https://github.com/matplotlib/basemap/archive/master.zip
回答by Anant Gupta
I was facing this issue and I was able to solve it using anaconda
我遇到了这个问题,我能够使用 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()
回答by beahacker
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.
我不使用 Anaconda,使用 MacOS,我发现这个解决方案对我有用。我想它很简单,应该适用于 Linux 和 MacO。
https://stackoverflow.com/a/53171723/2570842
https://stackoverflow.com/a/53171723/2570842
brew install geos
pip3 install https://github.com/matplotlib/basemap/archive/master.zip
For Ubuntu,
对于 Ubuntu,
sudo apt-get install geos
sudo pip3 install https://github.com/matplotlib/basemap/archive/master.zip
回答by GStav
I had the same issue; trying to access basemap using sys would produce that error. But this worked for me:
我遇到过同样的问题; 尝试使用 sys 访问底图会产生该错误。但这对我有用:
import mpl_toolkits
mpl_toolkits.__path__.append('/usr/lib/python2.7/dist-packages/mpl_toolkits/')
from mpl_toolkits.basemap import Basemap
回答by Sam
Download it from hereand 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)
从这里下载并手动安装。确保下载正确的版本(即如果您是 Python3.6 则下载basemap?1.2.0?cp36?cp36m?win_amd64.whl)
Reference: https://stackoverflow.com/a/33020524/8730201
回答by weatherman
I followed this answer:
我遵循了这个答案:
https://stackoverflow.com/a/43234894/3818277
https://stackoverflow.com/a/43234894/3818277
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(或者我不知道),就我而言,我发现该底图位于 pymodules 中,如下所示:
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.
所以这在 ubuntu 14.04 LTS 上对我有用。
回答by Santosh
If you're using Anaconda, it has a package for basemap
如果您使用的是 Anaconda,它有一个用于底图的包
conda install basemap
conda 安装底图
(pip doesn't have the basemap package any more)
(pip不再有底图包)
OR
或者
If you using Ubuntu system you can try
如果您使用 Ubuntu 系统,您可以尝试
apt install python3-mpltoolkits.basemap
apt 安装 python3-mpltoolkits.basemap
回答by Jade Cacho
回答by Autumnsault
I was able to get basemap working through the following steps. Note that I did a --user
install.
我能够通过以下步骤获得底图。请注意,我进行了--user
安装。
- Create shallow clone of basemap (
git clone --depth 1 [email protected]:matplotlib/basemap.git
) or extract the tarball of the current version. - Install the necessary prerequisite libraries (on Ubuntu,
libgeos-dev
,libproj-dev
,libgeos++-dev
,proj-data
,proj-bin
,libgeos-c1v5
,libgeos
,libproj12
, I think). pip install --user pyproj matplotlib geos
(not actually sure ifgeos
is necessary).
- 创建底图 (
git clone --depth 1 [email protected]:matplotlib/basemap.git
) 的浅层克隆或提取当前版本的 tarball。 - 安装必要的先决条件库(在Ubuntu上
libgeos-dev
,libproj-dev
,libgeos++-dev
,proj-data
,proj-bin
,libgeos-c1v5
,libgeos
,libproj12
,我认为)。 pip install --user pyproj matplotlib geos
(实际上不确定是否geos
有必要)。
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.
现在,这是我必须即兴发挥的地方。当我使用 安装底图时python setup.py install
,它会在我的 Python 包中创建一个新的 egg 目录。该目录包含一个mpl_toolkits
子目录,该子目录复制mpl_toolkits
由 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
.
我现在可以运行像simpletest.py
.