当生成一些(但不是全部)带有分割错误11的Cartopy地图时,Python 3.4崩溃 [英] Python 3.4 crashes when producing some – but not all – Cartopy maps with segmentation fault 11

查看:171
本文介绍了当生成一些(但不是全部)带有分割错误11的Cartopy地图时,Python 3.4崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在运行带有Python 3.4的El Capitan 10.11.6的Mac上安装了Python映射工具Cartopy.我可以使用Cartopy成功绘制一些地图,但是在某些情况下,Python内核因Segmentation Fault 11而死亡.

I have installed the Python mapping tool Cartopy on a Mac running El Capitan 10.11.6 with Python 3.4. I can use Cartopy to successfully plot some maps but, in some cases, the Python kernel dies with Segmentation Fault 11.

我想要一个可以在需要时从计算机上轻松删除的设置.因此,我使用fink安装了Python 3.4和必需的依赖项:

I wanted a setup that I could remove easily from my computer should the need arise. Therefore, I installed Python 3.4 and the necessary dependencies using fink:

$ fink install python34
$ fink install gdal2
$ fink install gdal2-dev
$ fink install proj
$ fink install libproj9

然后我使用pyvenv创建了一个虚拟环境(但也尝试使用virtualenv和venv)并激活了它.

I then created a virtual environment using pyvenv (but also tried virtualenv and venv) and activated it.

在激活的虚拟环境中,我使用pip进行安装:

In the activated virtual environment, I used pip to install:

$ pip install cython        # Successfully installed cython-0.25.2
$ pip install numpy         # Successfully installed numpy-1.12.1
$ pip install shapely       # Successfully installed shapely-1.5.17.post1
$ pip install pyshp         # Successfully installed pyshp-1.2.10
$ pip install pandas        # Successfully installed pandas-0.19.2 python-dateutil-2.6.0 pytz-2017.2 six-1.10.0
$ pip install matplotlib    # Successfully installed cycler-0.10.0 matplotlib-2.0.0 pyparsing-2.2.0
$ pip install pillow        # Successfully installed olefile-0.44 pillow-4.1.0
$ pip install pyepsg        # Successfully installed pyepsg-0.3.1
$ pip install scipy         # Successfully installed scipy-0.19.0
$ pip install OWSLib        # Successfully installed OWSLib-0.14.0 pyproj-1.9.5.1 requests-2.13.0
$ pip install mock          # Successfully installed mock-2.0.0 pbr-3.0.0
$ pip install nose          # Successfully installed nose-1.3.7
$ pip install pep8          # Successfully installed pep8-1.7.0
$ pip install jupyter       # Successfully installed MarkupSafe-1.0 appnope-0.1.0 backports-abc-0.5 bleach-2.0.0 decorator-4.0.11 entrypoints-0.2.2 html5lib-0.999999999 ipykernel-4.6.1 ipython-6.0.0 ipython-genutils-0.2.0 ipywidgets-6.0.0 jedi-0.10.2 jinja2-2.9.6 jsonschema-2.6.0 jupyter-1.0.0 jupyter-client-5.0.1 jupyter-console-5.1.0 jupyter-core-4.3.0 mistune-0.7.4 nbconvert-5.1.1 nbformat-4.3.0 notebook-5.0.0 pandocfilters-1.4.1 pexpect-4.2.1 pickleshare-0.7.4 prompt-toolkit-1.0.14 ptyprocess-0.5.1 pygments-2.2.0 pyzmq-16.0.2 qtconsole-4.3.0 simplegeneric-0.8.1 terminado-0.6 testpath-0.3 tornado-4.5.1 traitlets-4.3.2 typing-3.6.1 wcwidth-0.1.7 webencodings-0.5.1 widgetsnbextension-2.0.0

以上内容似乎符合以下列出的所有Cartopy依赖要求: http://scitools.org.uk/cartopy/docs/v0.15/installing.html

The above seemed to meet all the Cartopy dependancy requirements listed at: http://scitools.org.uk/cartopy/docs/v0.15/installing.html

然后我安装了Cartopy,并确保使用fink安装的geos库(如果这是正确的术语)构建:

I then installed Cartopy, ensuring to build against (if that's the correct term) the geos library installed with fink:

pip install --global-option=build_ext --global-option="-I/sw/opt/libgeos3.5.0/include" --global-option="-L/sw/opt/libgeos3.5.0/lib"  cartopy
                             # Successfully installed cartopy-0.14.2

我可以在Jupyter笔记本电脑或终端机中运行Python,这使我可以毫无错误地导入Cartopy.我从Cartopy网站下载了一些示例代码来测试安装.

I could run Python either in a Jupyter notebook or in the Terminal and it allowed me to import Cartopy with no errors. I downloaded some example code from the Cartopy website to test the installation.

以下示例可以很好地工作:

The following example worked perfectly:

import matplotlib
matplotlib.use("TkAgg")
cartopy.crs as ccrs
import matplotlib.pyplot as plt
ax = plt.axes(projection=ccrs.Mollweide())
ax.stock_img()
plt.show()

此代码也是如此:

import os
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt

from cartopy import config
import cartopy.crs as ccrs

fig = plt.figure(figsize=(8, 12))

# get the path of the file. It can be found in the repo data directory.
fname = os.path.join(config["repo_data_dir"],
                     'raster', 'sample', 'Miriam.A2012270.2050.2km.jpg'
                     )
img_extent = (-120.67660000000001, -106.32104523100001, 13.2301484511245, 30.766899999999502)
img = plt.imread(fname)

ax = plt.axes(projection=ccrs.PlateCarree())
plt.title('Hurricane Miriam from the Aqua/MODIS satellite\n'
          '2012 09/26/2012 20:50 UTC')

# set a margin around the data
ax.set_xmargin(0.05)
ax.set_ymargin(0.10)

# add the image. Because this image was a tif, the "origin" of the image is in the
# upper left corner
ax.imshow(img, origin='upper', extent=img_extent, transform=ccrs.PlateCarree())
ax.coastlines(resolution='50m', color='black', linewidth=1)

# mark a known place to help us geo-locate ourselves
ax.plot(-117.1625, 32.715, 'bo', markersize=7, transform=ccrs.Geodetic())
ax.text(-117, 33, 'San Diego', transform=ccrs.Geodetic())

plt.show()

但是此代码导致内核崩溃:

But this code caused the kernel to crash:

import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
import cartopy

ax = plt.axes(projection=cartopy.crs.PlateCarree())

ax.add_feature(cartopy.feature.LAND)
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
ax.add_feature(cartopy.feature.LAKES, alpha=0.5)
ax.add_feature(cartopy.feature.RIVERS)

ax.set_extent([-20, 60, -40, 40])

plt.show()

在终端中逐行输入代码后,所有行都可以正常使用,直到最后两行之一输入为止.

When the code was entered line-by-line in the Terminal, all the lines were fine until either of the last two were entered.

在命令行上产生的唯一错误消息是:

The only error message produced at the command line was:

Segmentation fault: 11

有人遇到这个问题的原因和解决方案吗?

Has anyone comes across a reason and a solution for this problem?

推荐答案

最后设法取得了一些进展,所以我将总结我的解决方案.它可能无法解决所有问题,但确实可以解决我最初遇到的问题.

Finally managed to make some progress so I'll summarise my solution. It may not resolve all issues but it did fix the problem I was having initially.

我在Cartopy GitHub页面上发布了一个问题( https://github.com/SciTools /cartopy/issues/879 ),其中QuLogic提出了一种解决方案,可以通过使用以下方法重新安装来停止分段错误:

I posted an issue on the Cartopy GitHub page (https://github.com/SciTools/cartopy/issues/879) where QuLogic suggested a solution to stop the segmentation fault by reinstalling shapely using:

pip uninstall shapely; pip install --no-binary :all: shapely

这确实停止了分段错误11,但是运行问题"代码然后产生一个错误,表明即使存在geos_c,也找不到.确切的错误是:

This indeed stopped the segmentation fault 11 but running the 'problem' code then produced an error suggesting that the geos_c couldn't be found, even though it was present. The exact error was:

OSError: Could not find lib geos_c or load any of its variants ['/Library/Frameworks/GEOS.framework/Versions/Current/GEOS', '/opt/local/lib/libgeos_c.dylib'].

似乎坚持执行的代码在预定的位置查找该库,并且即使我已将位置添加到我的.bash_profile文件中,也拒绝查看芬克安装该库的位置.解决方案是在该预定义位置创建一个指向fink已安装库的符号链接.希望这是有道理的. (请参见 OSError geos_c的Jace Browning安装Shapely时找不到).

It seems that the code insisted in looked for this library in a predefined location and refused to look at the locations where fink had installed the library even though I had added to location to my .bash_profile file. The solution was to create a symbolic link in that predefined location that pointed to the fink installed library. Hope that makes sense. (See Jace Browning at OSError geos_c could not be found when Installing Shapely).

因此,这里是我的整个解决方案的摘要,以防其他人遇到问题.任何改进的建议都将受到欢迎.

So here's a summary of my entire solution, in case it helps someone else. Any suggestions for improvements would be welcomed.

  1. 为便于记录,我的设置是在iMac上运行的Mac OS 10.11.6(El Capitan)上的标准(不是管理员)帐户.但是,如有必要,我也可以访问管理员帐户.

  1. For the record, my setup is a standard (not admin) account on Mac OS 10.11.6 (El Capitan) running on an iMac. I do, however, also have access to an admin account if necessary.

使用python.org提供的安装程序安装了Python 3.6版本

Installed version Python 3.6 using the installer provided at python.org

以管理员身份,使用fink来安装gdal2,gdal2-dev,libproj9,libgeos3.6.1. (还使用fink来安装python3.6,gdal-py36,freetype,freetype219,cairo,gsl,sqlite3和libspatialite7的版本,但不确定这些软件包是否绝对必要.)

As admin, used fink to install gdal2, gdal2-dev, libproj9, libgeos3.6.1. (Also used fink to install a version of python3.6, gdal-py36, freetype, freetype219, cairo, gsl, sqlite3 and libspatialite7 but not sure if these packages are absolutely necessary.)

Python 3.6已安装在/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6中.使用-m venv创建虚拟环境(称为venv36),如下所示:

Python 3.6 was installed in /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6. Created a virtual enviroment (called venv36) using -m venv as follows:

在命令行上:

$ mkdir <name_of_directory_for_virtual_env>
$ cd <name_of_directory_for_virtual_env>
$ /Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 -m venv venv36

  1. 在用户帐户中,使用nano来编辑.bash_profile文件,以包含芬克安装libgeos3.6.1的位置的路径:

在命令行上:

$ cd
$ nano .bash_profile

将以下行添加到.bash_profile文件并保存(ctrl-O):

Added the following lines to the .bash_profile file and saved (ctrl-O):

GEOS_CONFIG="/sw/opt/libgeos3.6.1/bin/geos-config"; export GEOS_CONFIG
GEOS_DIR="/sw/opt/libgeos3.6.1"; export GEOS_DIR

  1. 激活了虚拟环境并pip安装了必需的软件包. pandas和jupyter软件包是可选的,但是为什么不安装它们呢?

在命令行上:

$ cd <path_to_virtual_environment>
$ source venv36/bin/activate

(venv36) $ pip install cython
(venv36) $ pip install numpy
(venv36) $ pip install --no-binary :all: shapely
(venv36) $ pip install pyshp
(venv36) $ pip install pyproj
(venv36) $ pip install six
(venv36) $ pip install matplotlib

(venv36) $ export CPLUS_INCLUDE_PATH=/sw/include/gdal2/
(venv36) $ export C_INCLUDE_PATH=/sw/include/gdal2/
(venv36) $ pip install gdal
(venv36) $ pip install pillow
(venv36) $ pip install pyepsg
(venv36) $ pip install scipy
(venv36) $ pip install OWSLib
(venv36) $ pip install mock nose pep8
(venv36) $ pip install pandas
(venv36) $ pip install jupyter

(venv36) $ pip install --global-option=build_ext --global-option="-I/sw/opt/libgeos3.6.1/include" --global-option="-L/sw/opt/libgeos3.6.1/lib"  cartopy

  1. 最后,在/opt/local/lib/中添加了一个符号链接(这是cartopy或其他软件包坚持要寻找libgeos的地方),该链接指向由fink安装的libgeos库(称为libgeos_c.1.dylib) ).如果/opt/local/lib路径(或其某些位)尚不存在,则可能有必要创建该路径.

然后,在命令行上:

$ cd /opt/local/lib
$ sudo ln -s /sw/opt/libgeos3.6.1/lib/libgeos_c.1.dylib libgeos_c.dylib

就是这样.在激活的虚拟环境中,打开jupyter-notebook.如果要在笔记本中绘制地图,请确保第一行包含以下内容:

That's it. In the activated virtual environment, open jupyter-notebook. Make sure the first line contains the following if you want the maps to be drawn in the notebook:

%matplotlib inline

然后将以下内容添加到下一个单元格:

Then add the following to the next cell:

import cartopy
import matplotlib.pyplot as plt

ax = plt.axes(projection=cartopy.crs.PlateCarree())

ax.add_feature(cartopy.feature.LAND)
ax.add_feature(cartopy.feature.OCEAN)
ax.add_feature(cartopy.feature.COASTLINE)
ax.add_feature(cartopy.feature.BORDERS, linestyle=':')
ax.add_feature(cartopy.feature.LAKES, alpha=0.5)
ax.add_feature(cartopy.feature.RIVERS)

ax.set_extent([-20, 60, -40, 40])

plt.show()

代码运行时,可能会产生警告(Failed CDLL(/Library/Frameworks/GEOS.framework/Versions/Current/GEOS)),但希望它仍会产生以下图像:

When the code is run, it will likely produce a warning (Failed CDLL(/Library/Frameworks/GEOS.framework/Versions/Current/GEOS)) but it should, hopefully, still produce the following image:

就是这样.希望能帮助到你.任何意见或建议的改进将不胜感激.

So, that's it. Hope it helps. Any comments or suggestions for improvements would be appreciated.

这篇关于当生成一些(但不是全部)带有分割错误11的Cartopy地图时,Python 3.4崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆