Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG/ISSUE] "Cannot load backend 'TkAgg'" error when trying to regrid restart from lat-lon to cube sphere #269

Closed
msulprizio opened this issue Oct 16, 2023 · 3 comments · Fixed by #275
Assignees
Labels
category: Bug Something isn't working topic: Colors and Display Issues pertaining to color scales or other display issues
Milestone

Comments

@msulprizio
Copy link
Contributor

Describe the bug

When attempting to regrid restart files using the latest dev branch of GCPy (at commit 30a13f6) and the regridding fixes from PR #266, I'm getting the following error:

Traceback (most recent call last):
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/runpy.py", line 188, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "/n/home05/msulprizio/python/gcpy/gcpy/__init__.py", line 6, in <module>
    from .examples import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/__init__.py", line 8, in <module>
    from .plotting import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/plotting/__init__.py", line 4, in <module>
    from .plot_single_panel import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/plotting/plot_single_panel.py", line 24, in <module>
    mpl_use("tkagg")
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/site-packages/matplotlib/__init__.py", line 1249, in use
    plt.switch_backend(name)
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/site-packages/matplotlib/pyplot.py", line 350, in switch_backend
    raise ImportError(
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running
Traceback (most recent call last):
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/runpy.py", line 188, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "/n/home05/msulprizio/python/gcpy/gcpy/__init__.py", line 6, in <module>
    from .examples import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/__init__.py", line 8, in <module>
    from .plotting import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/plotting/__init__.py", line 4, in <module>
    from .plot_single_panel import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/plotting/plot_single_panel.py", line 24, in <module>
    mpl_use("tkagg")
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/site-packages/matplotlib/__init__.py", line 1249, in use
    plt.switch_backend(name)
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/site-packages/matplotlib/pyplot.py", line 350, in switch_backend
    raise ImportError(
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running
Traceback (most recent call last):
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/runpy.py", line 188, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "/n/home05/msulprizio/python/gcpy/gcpy/__init__.py", line 6, in <module>
    from .examples import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/__init__.py", line 8, in <module>
    from .plotting import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/plotting/__init__.py", line 4, in <module>
    from .plot_single_panel import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/plotting/plot_single_panel.py", line 24, in <module>
    mpl_use("tkagg")
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/site-packages/matplotlib/__init__.py", line 1249, in use
    plt.switch_backend(name)
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/site-packages/matplotlib/pyplot.py", line 350, in switch_backend
    raise ImportError(
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running
Traceback (most recent call last):
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/runpy.py", line 188, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "/n/home05/msulprizio/python/gcpy/gcpy/__init__.py", line 6, in <module>
    from .examples import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/__init__.py", line 8, in <module>
    from .plotting import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/plotting/__init__.py", line 4, in <module>
    from .plot_single_panel import *
  File "/n/home05/msulprizio/python/gcpy/gcpy/examples/plotting/plot_single_panel.py", line 24, in <module>
    mpl_use("tkagg")
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/site-packages/matplotlib/__init__.py", line 1249, in use
    plt.switch_backend(name)
  File "/n/home05/msulprizio/python/mamba/envs/gcpy_bmy/lib/python3.9/site-packages/matplotlib/pyplot.py", line 350, in switch_backend
    raise ImportError(
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running

I updated to @yantosca's gcpy environment which had tk installed, but still get the same error.

To Reproduce

I call the file_regrid routine from a bash script:

$!/bin/bash

prefix="GEOSChem.Restart.fullchem.20190101_0000z"

cs_resolutions=( '24' '48' '90' '180') # '360' )

# Loop over output resolutions
for cs_res in "${cs_resolutions[@]}"; do

    python -m gcpy.file_regrid --filein ${prefix}.nc4 \
       --dim_format_in classic \
       --fileout ${prefix}.c${cs_res}.nc4 \
       --cs_res_out ${cs_res} \
       --dim_format_out checkpoint

    # Remove regridding files
    rm -rf conservative_*.nc

done
@msulprizio msulprizio added the category: Bug Something isn't working label Oct 16, 2023
yantosca added a commit that referenced this issue Oct 19, 2023
gcpy/examples/plotting/plot_comparisons.py
gcpy/examples/plotting/plot_single_panel.py
- Load the TkAgg X11 backend before importing matplotlib.pyplot,
  as this might be the cause of the error in issue #269

Signed-off-by: Bob Yantosca <[email protected]>
@yantosca
Copy link
Contributor

Thanks @msulprizio. According to StackOverflow, this should be the proper import order:

from matplotlib import use as mpl_use
mpl_use("TkAgg")                          # X11 backend for plt.show()
import matplotlib.pyplot as plt

Apparently you need to apply the X11 backend immediately after importing matplotlib and before importing matplotlib.pyplot.

@yantosca yantosca self-assigned this Oct 19, 2023
@yantosca yantosca added the topic: Colors and Display Issues pertaining to color scales or other display issues label Oct 19, 2023
@yantosca yantosca added this to the 1.4.0 milestone Oct 19, 2023
@yantosca
Copy link
Contributor

A hotfix has been pushed in commit 8f75284 to the dev branch.

@yantosca
Copy link
Contributor

We can close this issue as #275 has since been merged into version 1.4.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category: Bug Something isn't working topic: Colors and Display Issues pertaining to color scales or other display issues
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants