在 Linux Ubuntu 18.04 上从 pip 安装 cartopy 退出并出现各种错误

Installing cartopy from pip exits with various errors on Linux Ubuntu 18.04

shell 命令 pip install cartopy 导致了几个错误。

起初,出现了以下错误:

    ERROR: Command errored out with exit status 1:
     command: /home/linuxbrew/.linuxbrew/opt/python@3.8/bin/python3.8 -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-j47_isf2/cartopy/setup.py'"'"'; __file__='"'"'/tmp/pip-install-j47_isf2/cartopy/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-pip-egg-info-a9f6gipd
         cwd: /tmp/pip-install-j47_isf2/cartopy/
    Complete output (3 lines):
    /tmp/pip-install-j47_isf2/cartopy/setup.py:157: UserWarning: Unable to determine Proj version. Ensure you have 4.9.0 or later installed, or installation may fail.
      warnings.warn(
    Proj version 0.0.0 is installed, but cartopy requires at least version 4.9.0.
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

在重新安装 proj4 并尝试在我的 .bashrc 文件中传递环境变量后, 比如

export PROJ_LIB=/home/linuxbrew/.linuxbrew/bin/proj
export PROJ=/home/linuxbrew/.linuxbrew/bin/proj

错误消息更改如下:

andylu@andylu-ThinkPad-Edge-E130:~/$ pip install cartopy
Collecting cartopy
  Using cached Cartopy-0.18.0.tar.gz (14.4 MB)
Requirement already satisfied: numpy>=1.10 in /home/linuxbrew/.linuxbrew/lib/python3.8/site-packages (from cartopy) (1.19.1)
Requirement already satisfied: shapely>=1.5.6 in /home/linuxbrew/.linuxbrew/lib/python3.8/site-packages (from cartopy) (1.7.0)
Requirement already satisfied: pyshp>=1.1.4 in /home/linuxbrew/.linuxbrew/lib/python3.8/site-packages (from cartopy) (2.1.0)
Requirement already satisfied: six>=1.3.0 in /home/linuxbrew/.linuxbrew/lib/python3.8/site-packages (from cartopy) (1.15.0)
Requirement already satisfied: setuptools>=0.7.2 in /home/linuxbrew/.linuxbrew/lib/python3.8/site-packages (from cartopy) (49.2.1)
Building wheels for collected packages: cartopy
  Building wheel for cartopy (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: /home/linuxbrew/.linuxbrew/opt/python@3.8/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-81mh5l39/cartopy/setup.py'"'"'; __file__='"'"'/tmp/pip-install-81mh5l39/cartopy/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-v0pstk7_
       cwd: /tmp/pip-install-81mh5l39/cartopy/
  Complete output (272 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build/lib.linux-x86_64-3.8
  creating build/lib.linux-x86_64-3.8/cartopy

  ...

  I/home/linuxbrew/.linuxbrew/opt/python@3.8/include/python3.8 -c lib/cartopy/trace.cpp -o build/temp.linux-x86_64-3.8/lib/cartopy/trace.o
  lib/cartopy/trace.cpp: In function 'void __pyx_f_7cartopy_5trace__project_segment(GEOSContextHandle_t, const GEOSCoordSequence*, unsigned int, unsigned int, __pyx_obj_7cartopy_5trace_Interpolator*, const GEOSPreparedGeometry*, double, __pyx_obj_7cartopy_5trace_LineAccumulator*)':
  lib/cartopy/trace.cpp:6362:52: warning: '__pyx_v_t_max' may be used uninitialized in this function [-Wmaybe-uninitialized]
         __pyx_t_2 = PyFloat_FromDouble(__pyx_v_t_max); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 539, __pyx_L1_error)
                                                      ^
  lib/cartopy/trace.cpp:6360:52: warning: '__pyx_v_t_min' may be used uninitialized in this function [-Wmaybe-uninitialized]
         __pyx_t_3 = PyFloat_FromDouble(__pyx_v_t_min); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 539, __pyx_L1_error)
                                                      ^
  c++ -pthread -shared -L/home/linuxbrew/.linuxbrew/lib build/temp.linux-x86_64-3.8/lib/cartopy/trace.o -L/home/linuxbrew/.linuxbrew/opt/python@3.8/lib -L/home/linuxbrew/.linuxbrew/Cellar/proj/7.1.0/lib -L/home/linuxbrew/.linuxbrew/Cellar/geos/3.8.1_1/lib -L/home/linuxbrew/.linuxbrew/lib -L/home/linuxbrew/.linuxbrew/opt/openssl@1.1/lib -L/home/linuxbrew/.linuxbrew/opt/sqlite/lib -L/home/linuxbrew/.linuxbrew/opt/python@3.8/lib -R/home/linuxbrew/.linuxbrew/opt/python@3.8/lib -lproj -lgeos_c -o build/lib.linux-x86_64-3.8/cartopy/trace.cpython-38-x86_64-linux-gnu.so
  c++: error: unrecognized command line option '-R'
  error: command 'c++' failed with exit status 1
  ----------------------------------------
  ERROR: Failed building wheel for cartopy
  Running setup.py clean for cartopy
Failed to build cartopy
DEPRECATION: Could not build wheels for cartopy which do not use PEP 517. pip will fall back to legacy 'setup.py install' for these. pip 21.0 will remove support for this functionality. A possible replacement is to fix the wheel build issue reported above. You can find discussion regarding this at https://github.com/pypa/pip/issues/8368.
Installing collected packages: cartopy
    Running setup.py install for cartopy ... error
    ERROR: Command errored out with exit status 1:
     command: /home/linuxbrew/.linuxbrew/opt/python@3.8/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-81mh5l39/cartopy/setup.py'"'"'; __file__='"'"'/tmp/pip-install-81mh5l39/cartopy/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-lmvk94_o/install-record.txt --single-version-externally-managed --compile --install-headers /home/linuxbrew/.linuxbrew/include/python3.8/cartopy
         cwd: /tmp/pip-install-81mh5l39/cartopy/
    Complete output (272 lines):
    running install
    running build
    
...

    building 'cartopy.trace' extension
    creating build/temp.linux-x86_64-3.8
    creating build/temp.linux-x86_64-3.8/lib
    creating build/temp.linux-x86_64-3.8/lib/cartopy
    cc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -O3 -Wall -fPIC -DACCEPT_USE_OF_DEPRECATED_PROJ_API_H=1 -I/home/linuxbrew/.linuxbrew/opt/python@3.8/include -I./lib/cartopy -I/home/linuxbrew/.linuxbrew/lib/python3.8/site-packages/numpy/core/include -I/home/linuxbrew/.linuxbrew/Cellar/proj/7.1.0/include -I/home/linuxbrew/.linuxbrew/Cellar/geos/3.8.1_1/include -I/home/linuxbrew/.linuxbrew/include -I/home/linuxbrew/.linuxbrew/opt/openssl@1.1/include -I/home/linuxbrew/.linuxbrew/opt/sqlite/include -I/home/linuxbrew/.linuxbrew/opt/python@3.8/include/python3.8 -c lib/cartopy/trace.cpp -o build/temp.linux-x86_64-3.8/lib/cartopy/trace.o
    lib/cartopy/trace.cpp: In function 'void __pyx_f_7cartopy_5trace__project_segment(GEOSContextHandle_t, const GEOSCoordSequence*, unsigned int, unsigned int, __pyx_obj_7cartopy_5trace_Interpolator*, const GEOSPreparedGeometry*, double, __pyx_obj_7cartopy_5trace_LineAccumulator*)':
    lib/cartopy/trace.cpp:6362:52: warning: '__pyx_v_t_max' may be used uninitialized in this function [-Wmaybe-uninitialized]
           __pyx_t_2 = PyFloat_FromDouble(__pyx_v_t_max); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 539, __pyx_L1_error)
                                                        ^
    lib/cartopy/trace.cpp:6360:52: warning: '__pyx_v_t_min' may be used uninitialized in this function [-Wmaybe-uninitialized]
           __pyx_t_3 = PyFloat_FromDouble(__pyx_v_t_min); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 539, __pyx_L1_error)
                                                        ^
    c++ -pthread -shared -L/home/linuxbrew/.linuxbrew/lib build/temp.linux-x86_64-3.8/lib/cartopy/trace.o -L/home/linuxbrew/.linuxbrew/opt/python@3.8/lib -L/home/linuxbrew/.linuxbrew/Cellar/proj/7.1.0/lib -L/home/linuxbrew/.linuxbrew/Cellar/geos/3.8.1_1/lib -L/home/linuxbrew/.linuxbrew/lib -L/home/linuxbrew/.linuxbrew/opt/openssl@1.1/lib -L/home/linuxbrew/.linuxbrew/opt/sqlite/lib -L/home/linuxbrew/.linuxbrew/opt/python@3.8/lib -R/home/linuxbrew/.linuxbrew/opt/python@3.8/lib -lproj -lgeos_c -o build/lib.linux-x86_64-3.8/cartopy/trace.cpython-38-x86_64-linux-gnu.so
    c++: error: unrecognized command line option '-R'
    error: command 'c++' failed with exit status 1
    ----------------------------------------
ERROR: Command errored out with exit status 1: /home/linuxbrew/.linuxbrew/opt/python@3.8/bin/python3.8 -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-81mh5l39/cartopy/setup.py'"'"'; __file__='"'"'/tmp/pip-install-81mh5l39/cartopy/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' install --record /tmp/pip-record-lmvk94_o/install-record.txt --single-version-externally-managed --compile --install-headers /home/linuxbrew/.linuxbrew/include/python3.8/cartopy Check the logs for full command output.

完整环境定义

操作系统

Lubuntu 18.04 64 位

Cartopy 版本

Collecting cartopy
  Using cached Cartopy-0.18.0.tar.gz (14.4 MB)

点列表

Package                       Version
----------------------------- ------------
affine                        2.3.0
aiohttp                       3.6.2
alabaster                     0.7.12
argon2-cffi                   20.1.0
arrow                         0.15.8
asgiref                       3.2.10
astroid                       2.4.2
async-timeout                 3.0.1
attrs                         19.3.0
Babel                         2.8.0
backcall                      0.2.0
beautifulsoup4                4.9.1
bleach                        3.1.5
bokeh                         2.1.1
Bottleneck                    1.3.2
branca                        0.4.1
bs4                           0.0.1
cachetools                    4.1.1
cdsapi                        0.3.0
certifi                       2020.6.20
cffi                          1.14.1
cftime                        1.2.1
chardet                       3.0.4
click                         7.1.2
click-plugins                 1.1.1
cligj                         0.5.0
cloudpickle                   1.5.0
colorama                      0.4.3
coverage                      5.2.1
cycler                        0.10.0
Cython                        0.29.21
dask                          2.22.0
decorator                     4.4.2
defusedxml                    0.6.0
descartes                     1.1.0
distributed                   2.22.0
Django                        3.0.9
docutils                      0.16
entrypoints                   0.3
et-xmlfile                    1.0.1
filelock                      3.0.12
Fiona                         1.8.13.post1
flake8                        3.8.3
Flask                         1.1.2
folium                        0.11.0
fsspec                        0.8.0
geojson                       2.5.0
geojsoncontour                0.4.0
geopandas                     0.8.1
geos                          0.2.2
gevent                        20.6.2
google                        3.0.0
google-api-core               1.22.0
google-auth                   1.20.0
google-auth-oauthlib          0.4.1
google-cloud-bigquery         1.26.1
google-cloud-core             1.3.0
google-crc32c                 0.1.0
google-resumable-media        0.7.0
googleapis-common-protos      1.52.0
greenlet                      0.4.16
grpcio                        1.30.0
h5py                          2.10.0
HeapDict                      1.0.1
html5lib                      1.1
hypothesis                    5.23.9
idna                          2.10
imagesize                     1.2.0
imgkit                        1.0.2
importlib-metadata            1.7.0
iniconfig                     1.0.1
ipykernel                     5.3.4
ipython                       7.17.0
ipython-genutils              0.2.0
ipywidgets                    7.5.1
isort                         5.2.2
itsdangerous                  1.1.0
jdcal                         1.4.1
jedi                          0.17.2
Jinja2                        2.11.2
joblib                        0.16.0
jsonschema                    3.2.0
jupyter                       1.0.0
jupyter-client                6.1.6
jupyter-console               6.1.0
jupyter-core                  4.6.3
kiwisolver                    1.2.0
lazy-object-proxy             1.5.1
locket                        0.2.0
lxml                          4.5.2
MarkupSafe                    1.1.1
matplotlib                    3.3.0
mccabe                        0.6.1
mistune                       0.8.4
modin                         0.8.0
more-itertools                8.4.0
mpmath                        1.1.0
msgpack                       1.0.0
multidict                     4.7.6
munch                         2.5.0
nbconvert                     5.6.1
nbformat                      5.0.7
netCDF4                       1.5.4
networkx                      2.4
notebook                      6.1.0
numexpr                       2.7.1
numpy                         1.19.1
oauthlib                      3.1.0
openpyxl                      3.0.4
osmnx                         0.15.1
OWSLib                        0.20.0
packaging                     20.4
pandas                        1.1.0
pandas-datareader             0.9.0
pandas-gbq                    0.13.2
pandocfilters                 1.4.2
parso                         0.7.1
partd                         1.1.0
patsy                         0.5.1
pdfkit                        0.6.1
pep8                          1.7.1
pexpect                       4.8.0
pickle5                       0.0.11
pickleshare                   0.7.5
Pillow                        7.2.0
pip                           20.2
planar                        0.4
plotly                        4.9.0
pluggy                        0.13.1
proj                          0.2.0
prometheus-client             0.8.0
prompt-toolkit                3.0.5
protobuf                      3.12.4
psutil                        5.7.2
ptyprocess                    0.6.0
py                            1.9.0
py-spy                        0.3.3
pyarrow                       0.16.0
pyasn1                        0.4.8
pyasn1-modules                0.2.8
pycodestyle                   2.6.0
pycparser                     2.20
pydata-google-auth            1.1.0
pyepsg                        0.4.0
pyflakes                      2.2.0
Pygments                      2.6.1
pyhdf                         0.10.2
pykdtree                      1.3.1
pylint                        2.5.3
pylint-django                 2.2.0
pylint-plugin-utils           0.6
pyparsing                     2.4.7
pyproj                        2.6.1.post1
PyQt5                         5.15.0
PyQt5-sip                     12.8.0
pyrsistent                    0.16.0
pyshp                         2.1.0
PySide2                       5.15.0
pytest                        6.0.1
pytest-cov                    2.10.0
pytest-filter-subpackage      0.1.1
python-dateutil               2.8.1
pytz                          2020.1
PyYAML                        5.3.1
pyzmq                         19.0.2
qtconsole                     4.7.5
QtPy                          1.9.0
rasterio                      1.1.5
ray                           0.8.6
redis                         3.4.1
regex                         2020.7.14
requests                      2.24.0
requests-oauthlib             1.3.0
retrying                      1.3.3
rsa                           4.6
Rtree                         0.9.4
salem                         0.2.4
scikit-learn                  0.23.1
scipy                         1.5.2
seaborn                       0.10.1
Send2Trash                    1.5.0
setuptools                    49.2.1
Shapely                       1.7.0
shiboken2                     5.15.0
six                           1.15.0
sklearn                       0.0
snowballstemmer               2.0.0
snuggs                        1.4.7
sortedcollections             1.2.1
sortedcontainers              2.2.2
soupsieve                     2.0.1
Sphinx                        3.1.2
sphinxcontrib-applehelp       1.0.2
sphinxcontrib-devhelp         1.0.2
sphinxcontrib-htmlhelp        1.0.3
sphinxcontrib-jsmath          1.0.1
sphinxcontrib-qthelp          1.0.3
sphinxcontrib-serializinghtml 1.1.4
SQLAlchemy                    1.3.18
sqlparse                      0.3.1
statsmodels                   0.11.1
swifter                       1.0.2
sympy                         1.6.1
tables                        3.6.1
tblib                         1.7.0
terminado                     0.8.3
testpath                      0.4.4
threadpoolctl                 2.1.0
toml                          0.10.1
toolz                         0.10.0
tornado                       6.0.4
tqdm                          4.48.1
traitlets                     4.3.3
typed-ast                     1.4.1
typing-extensions             3.7.4.2
urllib3                       1.25.10
wcwidth                       0.2.5
webcolors                     1.11.1
webencodings                  0.5.1
Werkzeug                      1.0.1
wheel                         0.34.2
widgetsnbextension            3.5.1
windrose                      1.6.7
wrapt                         1.12.1
xarray                        0.16.0
xlrd                          1.2.0
XlsxWriter                    1.3.1
yapf                          0.30.0
yarl                          1.5.1
zict                          2.0.0
zipp                          3.1.0
zope.event                    4.4
zope.interface                5.1.0

在搜索主要错误信息 c++: error: unrecognized command line option '-R' 的解决方案后,我终于找到了 in this discussion

我所做的是使用强大的 find 方法搜索相关文件:

andylu@andylu-ThinkPad-Edge-E130:~/Desktop/Python/Scripts$ sudo find / -type f -iname "unixccompiler.py"
find: ‘/run/user/1000/gvfs’: Permission denied
find: ‘/tmp/.mount_pcloudgeFVGR’: Permission denied
/snap/core/9436/usr/lib/python3.5/distutils/unixccompiler.py
/snap/core/9665/usr/lib/python3.5/distutils/unixccompiler.py
/snap/gimp/281/usr/lib/python2.7/distutils/unixccompiler.py
/snap/gimp/273/usr/lib/python2.7/distutils/unixccompiler.py
/snap/core18/1880/usr/lib/python3.6/distutils/unixccompiler.py
/snap/core18/1880/usr/lib/python3.7/distutils/unixccompiler.py
/snap/core18/1880/usr/lib/python3.8/distutils/unixccompiler.py
/snap/core18/1754/usr/lib/python3.6/distutils/unixccompiler.py
/snap/core18/1754/usr/lib/python3.7/distutils/unixccompiler.py
/snap/core18/1754/usr/lib/python3.8/distutils/unixccompiler.py
/usr/lib/python3.8/distutils/unixccompiler.py
/usr/lib/python3.6/distutils/unixccompiler.py
/usr/lib/python3/dist-packages/numpy/distutils/unixccompiler.py
/usr/lib/python3.7/distutils/unixccompiler.py
/usr/lib/python2.7/distutils/unixccompiler.py
/usr/lib/python2.7/dist-packages/numpy/distutils/unixccompiler.py
find: ‘/home/andylu/pCloudDrive’: Permission denied
/home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.5/libexec/setuptools/build/lib/setuptools/_distutils/unixccompiler.py
/home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.5/libexec/setuptools/setuptools/_distutils/unixccompiler.py
/home/linuxbrew/.linuxbrew/Cellar/python@3.8/3.8.5/lib/python3.8/distutils/unixccompiler.py
/home/linuxbrew/.linuxbrew/Cellar/numpy/1.19.1/lib/python3.8/site-packages/numpy/distutils/unixccompiler.py
/home/linuxbrew/.linuxbrew/lib/python3.8/site-packages/numpy/distutils/unixccompiler.py
/home/linuxbrew/.linuxbrew/lib/python3.8/site-packages/setuptools/_distutils/unixccompiler.py

接下来,我在文本编辑器VS Code中打开了包含当前使用的version 3.8的相关python-files。 然后,我在这些文件中搜索“-R”,找到下面的code-piece:

        else:
            if self._is_gcc(compiler):
                # gcc on non-GNU systems does not need -Wl, but can
                # use it anyway.  Since distutils has always passed in
                # -Wl whenever gcc was used in the past it is probably
                # safest to keep doing so.
                if sysconfig.get_config_var("GNULD") == "yes":
                    # GNU ld needs an extra option to get a RUNPATH
                    # instead of just an RPATH.
                    return "-Wl,--enable-new-dtags,-R" + dir
                else:
                    return "-Wl,-R" + dir
            else:
                # No idea how --enable-new-dtags would be passed on to
                # ld if this system was using GNU ld.  Don't know if a
                # system like this even exists.
                return "-R" + dir

在这个 code-piece 中,我用 "-rpath=" 替换了 "-R" 和最后的 stand-alone "-R" (上面代码的最后一行)甚至'-Wl,-rpath='。 完成后,code-piece 看起来像这样:

    else:
        if self._is_gcc(compiler):
            # gcc on non-GNU systems does not need -Wl, but can
            # use it anyway.  Since distutils has always passed in
            # -Wl whenever gcc was used in the past it is probably
            # safest to keep doing so.
            if sysconfig.get_config_var("GNULD") == "yes":
                # GNU ld needs an extra option to get a RUNPATH
                # instead of just an RPATH.
                return "-Wl,--enable-new-dtags,-rpath=" + dir
            else:
                return "-Wl,-rpath=" + dir
        else:
            # No idea how --enable-new-dtags would be passed on to
            # ld if this system was using GNU ld.  Don't know if a
            # system like this even exists.
            return "-Wl,-rpath=" + dir

保存并关闭above-mentioned修改后python-scripts,cartopy的安装终于成功了:

andylu@andylu-ThinkPad-Edge-E130:~/Desktop/Python/Scripts$ pip install cartopy
Collecting cartopy
  Using cached Cartopy-0.18.0.tar.gz (14.4 MB)
Requirement already satisfied: numpy>=1.10 in /home/linuxbrew/.linuxbrew/lib/python3.8/site-packages (from cartopy) (1.19.1)
Requirement already satisfied: shapely>=1.5.6 in /home/linuxbrew/.linuxbrew/lib/python3.8/site-packages (from cartopy) (1.7.0)
Requirement already satisfied: pyshp>=1.1.4 in /home/linuxbrew/.linuxbrew/lib/python3.8/site-packages (from cartopy) (2.1.0)
Requirement already satisfied: six>=1.3.0 in /home/linuxbrew/.linuxbrew/lib/python3.8/site-packages (from cartopy) (1.15.0)
Requirement already satisfied: setuptools>=0.7.2 in /home/linuxbrew/.linuxbrew/lib/python3.8/site-packages (from cartopy) (49.2.1)
Building wheels for collected packages: cartopy
  Building wheel for cartopy (setup.py) ... done
  Created wheel for cartopy: filename=Cartopy-0.18.0-cp38-cp38-linux_x86_64.whl size=15743039 sha256=b95932012d877432db8e629bdc77e730227f93f94515a54e59e4c6fb6a85bdad
  Stored in directory: /home/andylu/.cache/pip/wheels/7c/3c/68/ed800c08e3e6579b632fdd26becee97c5c5474625f6c97eca6
Successfully built cartopy
Installing collected packages: cartopy
Successfully installed cartopy-0.18.0