Skip to content

Add all cf-plot dependencies (i.e. Cartopy) to cf.environment#940

Merged
sadielbartholomew merged 2 commits intoNCAS-CMS:mainfrom
sadielbartholomew:environment-func-cfplot-deps
Mar 26, 2026
Merged

Add all cf-plot dependencies (i.e. Cartopy) to cf.environment#940
sadielbartholomew merged 2 commits intoNCAS-CMS:mainfrom
sadielbartholomew:environment-func-cfplot-deps

Conversation

@sadielbartholomew
Copy link
Member

@sadielbartholomew sadielbartholomew commented Mar 26, 2026

Close #908. For the benefit of user support etc. it would be ideal to have all cf-plot dependencies reported by cf.environment and cartopy is the only one that is not already contained there.(though if any get added as new cf-plot deps I will need to ensure they also get included here, in future.

Notes:

  • We no longer include example outputs of environment calls so it seems only the underlying function code needed updating.
  • Test passes still but I thought that was a good reason to update the test to be more thorough, so that is also included.
  • I don't think it is worth updating the changelog to note that one new llibrary appears in the output of environment (too minor), but let me know if you think otherwise.

@sadielbartholomew sadielbartholomew self-assigned this Mar 26, 2026
@sadielbartholomew sadielbartholomew added documentation Improvements or additions to documentation testing Issues related to units tests and their coverage dependency labels Mar 26, 2026
@sadielbartholomew sadielbartholomew changed the title Add all cf-plot dependencies (just Cartopy left) to cf.environment Add all cf-plot dependencies (just Cartopy) to cf.environment Mar 26, 2026
@sadielbartholomew sadielbartholomew changed the title Add all cf-plot dependencies (just Cartopy) to cf.environment Add all cf-plot dependencies (i.e. Cartopy) to cf.environment Mar 26, 2026
@sadielbartholomew
Copy link
Member Author

sadielbartholomew commented Mar 26, 2026

@davidhassell when I run cf.environment for an environment without cartopy (and various other optional deps) I do see the following (note the AttributeError also emerging before the output returns as expected), but I am quite unsure how we can/should deal with it (as you can see from the code changes, I included Cartopy in the standard way as with the existing libraries that get reported):

$ python -c "import cf; cf.environment()"

A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.3.0 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.

Traceback (most recent call last):  File "<string>", line 1, in <module>
  File "/home/slb93/git-repos/cf-python/cf/functions.py", line 3218, in environment
    "cartopy": _get_module_info("cartopy", try_except=True),
  File "/home/slb93/git-repos/cfdm/cfdm/functions.py", line 389, in _get_module_info
    importlib.import_module(module)
  File "/home/slb93/miniconda3/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/slb93/miniconda3/lib/python3.11/site-packages/cartopy/__init__.py", line 107, in <module>
    import cartopy.crs  # noqa: E402  module-level imports
  File "/home/slb93/miniconda3/lib/python3.11/site-packages/cartopy/crs.py", line 24, in <module>
    import shapely.geometry as sgeom
  File "/home/slb93/miniconda3/lib/python3.11/site-packages/shapely/__init__.py", line 1, in <module>
    from shapely.lib import GEOSException  # NOQA
AttributeError: _ARRAY_API not found

A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.3.0 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.

Traceback (most recent call last):  File "<string>", line 1, in <module>
  File "/home/slb93/git-repos/cf-python/cf/functions.py", line 3219, in environment
    "cfplot": _get_module_info("cfplot", try_except=True),
  File "/home/slb93/git-repos/cfdm/cfdm/functions.py", line 389, in _get_module_info
    importlib.import_module(module)
  File "/home/slb93/miniconda3/lib/python3.11/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/slb93/miniconda3/lib/python3.11/site-packages/cfplot/__init__.py", line 16, in <module>
    from .cfplot import *
  File "/home/slb93/miniconda3/lib/python3.11/site-packages/cfplot/cfplot.py", line 15, in <module>
    import cartopy
  File "/home/slb93/miniconda3/lib/python3.11/site-packages/cartopy/__init__.py", line 107, in <module>
    import cartopy.crs  # noqa: E402  module-level imports
  File "/home/slb93/miniconda3/lib/python3.11/site-packages/cartopy/crs.py", line 24, in <module>
    import shapely.geometry as sgeom
  File "/home/slb93/miniconda3/lib/python3.11/site-packages/shapely/__init__.py", line 1, in <module>
    from shapely.lib import GEOSException  # NOQA
AttributeError: _ARRAY_API not found
Platform: Linux-6.18.4-1-MANJARO-x86_64-with-glibc2.42 
Python: 3.11.8 /home/slb93/miniconda3/bin/python
packaging: 24.1 /home/slb93/miniconda3/lib/python3.11/site-packages/packaging/__init__.py
numpy: 2.3.0 /home/slb93/miniconda3/lib/python3.11/site-packages/numpy/__init__.py
cfdm.core: 1.13.0.0 /home/slb93/git-repos/cfdm/cfdm/core/__init__.py
udunits2 library: libudunits2.so.0 
HDF5 library: 1.14.2 
netcdf library: 4.9.4-development 
netCDF4: 1.7.2 /home/slb93/miniconda3/lib/python3.11/site-packages/netCDF4/__init__.py
h5netcdf: 1.8.1 /home/slb93/miniconda3/lib/python3.11/site-packages/h5netcdf/__init__.py
h5py: 3.14.0 /home/slb93/miniconda3/lib/python3.11/site-packages/h5py/__init__.py
pyfive: 1.1.1 /home/slb93/miniconda3/lib/python3.11/site-packages/pyfive/__init__.py
zarr: 3.0.8 /home/slb93/miniconda3/lib/python3.11/site-packages/zarr/__init__.py
fsspec: 2024.6.1 /home/slb93/miniconda3/lib/python3.11/site-packages/fsspec/__init__.py
scipy: 1.15.3 /home/slb93/miniconda3/lib/python3.11/site-packages/scipy/__init__.py
dask: 2025.7.0 /home/slb93/miniconda3/lib/python3.11/site-packages/dask/__init__.py
distributed: 2025.7.0 /home/slb93/miniconda3/lib/python3.11/site-packages/distributed/__init__.py
cftime: 1.6.4.post1 /home/slb93/miniconda3/lib/python3.11/site-packages/cftime/__init__.py
cfunits: 3.3.7 /home/slb93/miniconda3/lib/python3.11/site-packages/cfunits/__init__.py
cfdm: 1.13.0.0 /home/slb93/git-repos/cfdm/cfdm/__init__.py
esmpy/ESMF: not available 
psutil: 6.1.0 /home/slb93/miniconda3/lib/python3.11/site-packages/psutil/__init__.py
matplotlib: 3.9.2 /home/slb93/miniconda3/lib/python3.11/site-packages/matplotlib/__init__.py
activestorage: not available 
cartopy: not available 
cfplot: not available 
cf: 3.19.0 /home/slb93/git-repos/cf-python/cf/__init__.py

@davidhassell
Copy link
Collaborator

Can you import cartopy on the CLI?

@sadielbartholomew
Copy link
Member Author

Can you import cartopy on the CLI?

Ah, no - but the question has helped me see the issue at hand, namely that my base environment did actually have Cartopy installed somehow (should have definitely checked that instead of assuming my conda base was nice and clean as it should be, so didn't!). And when I get rid of that so Cartopy isn't truly installed, the behaviour is nice and as expected:

$ python -c "import cartopy"

A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.3.0 as it may crash. To support both 1.x and 2.x
versions of NumPy, modules must be compiled with NumPy 2.0.
Some module may need to rebuild instead e.g. with 'pybind11>=2.12'.

If you are a user of the module, the easiest solution will be to
downgrade to 'numpy<2' or try to upgrade the affected module.
We expect that some modules will need time to support NumPy 2.

Traceback (most recent call last):  File "<string>", line 1, in <module>
  File "/home/slb93/miniconda3/lib/python3.11/site-packages/cartopy/__init__.py", line 107, in <module>
    import cartopy.crs  # noqa: E402  module-level imports
  File "/home/slb93/miniconda3/lib/python3.11/site-packages/cartopy/crs.py", line 24, in <module>
    import shapely.geometry as sgeom
  File "/home/slb93/miniconda3/lib/python3.11/site-packages/shapely/__init__.py", line 1, in <module>
    from shapely.lib import GEOSException  # NOQA
AttributeError: _ARRAY_API not found
Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/home/slb93/miniconda3/lib/python3.11/site-packages/cartopy/__init__.py", line 107, in <module>
    import cartopy.crs  # noqa: E402  module-level imports
    ^^^^^^^^^^^^^^^^^^
  File "/home/slb93/miniconda3/lib/python3.11/site-packages/cartopy/crs.py", line 24, in <module>
    import shapely.geometry as sgeom
  File "/home/slb93/miniconda3/lib/python3.11/site-packages/shapely/__init__.py", line 1, in <module>
    from shapely.lib import GEOSException  # NOQA
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
ImportError: numpy.core.multiarray failed to import
$ conda list | grep cartopy
cartopy                   0.22.0                   pypi_0    pypi
$ pip uninstall cartopy
Found existing installation: Cartopy 0.22.0
Uninstalling Cartopy-0.22.0:
  Would remove:
    /home/slb93/miniconda3/bin/feature_download
    /home/slb93/miniconda3/lib/python3.11/site-packages/Cartopy-0.22.0.dist-info/*
    /home/slb93/miniconda3/lib/python3.11/site-packages/cartopy/*
Proceed (Y/n)? Y
  Successfully uninstalled Cartopy-0.22.0
$ python -c "import cartopy"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'cartopy'
$ python -c "import cf; cf.environment()"
Platform: Linux-6.18.4-1-MANJARO-x86_64-with-glibc2.42 
Python: 3.11.8 /home/slb93/miniconda3/bin/python
packaging: 24.1 /home/slb93/miniconda3/lib/python3.11/site-packages/packaging/__init__.py
numpy: 2.3.0 /home/slb93/miniconda3/lib/python3.11/site-packages/numpy/__init__.py
cfdm.core: 1.13.0.0 /home/slb93/git-repos/cfdm/cfdm/core/__init__.py
udunits2 library: libudunits2.so.0 
HDF5 library: 1.14.2 
netcdf library: 4.9.4-development 
netCDF4: 1.7.2 /home/slb93/miniconda3/lib/python3.11/site-packages/netCDF4/__init__.py
h5netcdf: 1.8.1 /home/slb93/miniconda3/lib/python3.11/site-packages/h5netcdf/__init__.py
h5py: 3.14.0 /home/slb93/miniconda3/lib/python3.11/site-packages/h5py/__init__.py
pyfive: 1.1.1 /home/slb93/miniconda3/lib/python3.11/site-packages/pyfive/__init__.py
zarr: 3.0.8 /home/slb93/miniconda3/lib/python3.11/site-packages/zarr/__init__.py
fsspec: 2024.6.1 /home/slb93/miniconda3/lib/python3.11/site-packages/fsspec/__init__.py
scipy: 1.15.3 /home/slb93/miniconda3/lib/python3.11/site-packages/scipy/__init__.py
dask: 2025.7.0 /home/slb93/miniconda3/lib/python3.11/site-packages/dask/__init__.py
distributed: 2025.7.0 /home/slb93/miniconda3/lib/python3.11/site-packages/distributed/__init__.py
cftime: 1.6.4.post1 /home/slb93/miniconda3/lib/python3.11/site-packages/cftime/__init__.py
cfunits: 3.3.7 /home/slb93/miniconda3/lib/python3.11/site-packages/cfunits/__init__.py
cfdm: 1.13.0.0 /home/slb93/git-repos/cfdm/cfdm/__init__.py
esmpy/ESMF: not available 
psutil: 6.1.0 /home/slb93/miniconda3/lib/python3.11/site-packages/psutil/__init__.py
matplotlib: 3.9.2 /home/slb93/miniconda3/lib/python3.11/site-packages/matplotlib/__init__.py
activestorage: not available 
cfplot: not available 
cf: 3.19.0 /home/slb93/git-repos/cf-python/cf/__init__.py

So sorry, that's a non issue relating to a bad conda base environment of mine (now remedied). Please go ahead with what should otherwise be an easy quick review 🙂

Copy link
Collaborator

@davidhassell davidhassell left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect - thanks! Merge at will.

@sadielbartholomew sadielbartholomew merged commit 02e348c into NCAS-CMS:main Mar 26, 2026
4 of 14 checks passed
@sadielbartholomew sadielbartholomew deleted the environment-func-cfplot-deps branch March 26, 2026 14:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependency documentation Improvements or additions to documentation testing Issues related to units tests and their coverage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cf-plot (& dependencies) inclusion in cf.environment()

2 participants