在 conda 环境中安装后无法导入包

时间:2021-01-31 06:04:55

标签: python anaconda conda gdal

我尝试在我的 conda 环境中安装 gdal 包。我激活了 gcpy 环境并使用 conda install -c conda-forge gdal 安装了 gdal 包。软件包安装成功。但是,当我尝试导入包时,出现错误:

In [1]: import gdal
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-1-ee38efafc30b> in <module>
----> 1 import gdal
ModuleNotFoundError: No module named 'gdal'

我可以在 conda list 中看到包裹。

gdal                      3.2.1            py39h409cc32_1    conda-forge
geos                      3.8.1                he1b5a44_0    conda-forge
geoschem-gcpy             1.0.0            py39hf3d152e_0    conda-forge
geotiff                   1.6.0                h5d11630_3    conda-forge
gettext                   0.19.8.1          h0b5b191_1005    conda-forge
giflib                    5.2.1                h36c2ea0_2    conda-forge
glib                      2.66.4               hc4f0c31_2    conda-forge
glib-tools                2.66.4               hc4f0c31_2    conda-forge
gst-plugins-base          1.14.5               h0935bb2_2    conda-forge
gstreamer                 1.18.3               h3560a44_0    conda-forge
h5netcdf                  0.8.1                      py_0    conda-forge
h5py                      3.1.0           nompi_py39h25020de_100    conda-forge
hdf4                      4.2.13            h10796ff_1004    conda-forge
hdf5                      1.10.6          nompi_h6a2412b_1114    conda-forge
heapdict                  1.0.1                      py_0    conda-forge
helpdev                   0.7.1              pyhd8ed1ab_0    conda-forge
icu                       68.1                 h58526e2_0    conda-forge
idna                      2.10               pyh9f0ad1d_0    conda-forge
imagesize                 1.2.0                      py_0    conda-forge
importlib-metadata        3.4.0            py39hf3d152e_0    conda-forge
importlib_metadata        3.4.0                hd8ed1ab_0    conda-forge
intervaltree              3.0.2                      py_0    conda-forge
ipykernel                 5.4.2            py39hef51801_0    conda-forge
ipython                   7.19.0           py39hef51801_2    conda-forge
ipython_genutils          0.2.0                      py_1    conda-forge
isort                     5.7.0              pyhd8ed1ab_0    conda-forge
jedi                      0.17.2           py39hf3d152e_1    conda-forge
jeepney                   0.6.0              pyhd8ed1ab_0    conda-forge
jinja2                    2.11.2             pyh9f0ad1d_0    conda-forge
joblib                    1.0.0              pyhd8ed1ab_0    conda-forge
jpeg                      9d                   h36c2ea0_0    conda-forge
json-c                    0.13.1            hbfbb72e_1002    conda-forge
jsonschema                3.2.0                      py_2    conda-forge
jupyter_client            6.1.11             pyhd8ed1ab_1    conda-forge
jupyter_core              4.7.0            py39hf3d152e_1    conda-forge
jupyterlab_pygments       0.1.2              pyh9f0ad1d_0    conda-forge
kealib                    1.4.14               h0042707_0    conda-forge
keyring                   22.0.1           py39hf3d152e_0    conda-forge
kiwisolver                1.3.1            py39h1a9c180_1    conda-forge
krb5                      1.17.2               h926e7f8_0    conda-forge
lazy-object-proxy         1.4.3            py39h07f9747_2    conda-forge
lcms2                     2.11                 hcbb858e_1    conda-forge
ld_impl_linux-64          2.35.1               hea4e1c9_1    conda-forge
libblas                   3.9.0                7_openblas    conda-forge
libcblas                  3.9.0                7_openblas    conda-forge
libclang                  11.0.1          default_ha53f305_1    conda-forge
libcurl                   7.71.1               hcdd3856_8    conda-forge
libdap4                   3.20.6               h1d1bd15_1    conda-forge
libedit                   3.1.20191231         he28a2e2_2    conda-forge
libev                     4.33                 h516909a_1    conda-forge
libevent                  2.1.10               hcdb4288_3    conda-forge
libffi                    3.3                  h58526e2_2    conda-forge
libgcc-ng                 9.3.0               h2828fa1_18    conda-forge
libgdal                   3.2.1                h52563cd_1    conda-forge
libgfortran-ng            9.3.0               hff62375_18    conda-forge
libgfortran5              9.3.0               hff62375_18    conda-forge
libglib                   2.66.4               h748fe8e_2    conda-forge
libgomp                   9.3.0               h2828fa1_18    conda-forge
libiconv                  1.16                 h516909a_0    conda-forge
libkml                    1.3.0             h74f7ee3_1012    conda-forge
liblapack                 3.9.0                7_openblas    conda-forge
libllvm11                 11.0.1               hf817b99_0    conda-forge
libnetcdf                 4.7.4           nompi_h56d31a8_107    conda-forge
libnghttp2                1.41.0               h8cfc5f6_2    conda-forge
libopenblas               0.3.12          pthreads_h4812303_1    conda-forge
libpng                    1.6.37               h21135ba_2    conda-forge
libpq                     12.3                 h255efa7_3    conda-forge
libsodium                 1.0.18               h36c2ea0_1    conda-forge
libspatialindex           1.9.3                he1b5a44_3    conda-forge
libspatialite             5.0.0                heaf302f_0    conda-forge
libssh2                   1.9.0                hab1572f_5    conda-forge
libstdcxx-ng              9.3.0               h6de172a_18    conda-forge
libtiff                   4.2.0                hdc55705_0    conda-forge
libuuid                   2.32.1            h7f98852_1000    conda-forge
libwebp-base              1.1.0                h36c2ea0_3    conda-forge
libxcb                    1.13              h7f98852_1003    conda-forge
libxkbcommon              1.0.3                he3ba5ed_0    conda-forge
libxml2                   2.9.10               h72842e0_3    conda-forge

我像 Here 所说的那样尝试了 conda update --all 。我也试过所有类似的问题。但是,仍然没有解决方案。

Reference 1 Reference 2

1 个答案:

答案 0 :(得分:-1)

GDAL Python 绑定包含五个主要模块。:

 from osgeo import gdal
 from osgeo import ogr
 from osgeo import osr
 from osgeo import gdal_array
 from osgeo import gdalconst

此外,还包含五个兼容性模块,但会提供通知以声明它们已被弃用并将消失。如果您使用 GDAL 1.7 绑定,您应该更新您的导入以利用上述用法,但以下内容至少在 GDAL 2.1 之前有效。

 import gdal
 import ogr
 import osr
 import gdalnumeric
 import gdalconst

如果您之前有导入全局模块的代码并且仍然需要支持旧的导入,那么简单的 try...except import 可以消除弃用警告并保持名称与以前基本相同:

    try:
        from osgeo import gdal
    except ImportError:
        import gdal