Skip to content

[Bug]: mplsetup.cfg parsing issue #21660

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

Closed
adamjstewart opened this issue Nov 18, 2021 · 6 comments · Fixed by #21662
Closed

[Bug]: mplsetup.cfg parsing issue #21660

adamjstewart opened this issue Nov 18, 2021 · 6 comments · Fixed by #21662
Milestone

Comments

@adamjstewart
Copy link
Contributor

adamjstewart commented Nov 18, 2021

Bug summary

mplsetup.cfg isn't being parsed correctly, leading to an invalid matplotlibrc being installed.

Code for reproduction

$ wget https://files.pythonhosted.org/packages/source/m/matplotlib/matplotlib-3.5.0.tar.gz
$ tar xvzf matplotlib-3.5.0.tar.gz
$ cd matplotlib-3.5.0
$ cat > mplsetup.cfg
[rc_options]
backend=macosx
$ python setup.py install

Actual outcome

The matplotlibrc that gets installed contains:

#backend: macosx#webagg.port: 8988

Expected outcome

The matplotlibrc should instead contain:

backend: macosx
#webagg.port 8988

Additional information

When I try to import this installation, I see:

    import matplotlib.pyplot as plt
../.spack/.spack-env/view/lib/python3.8/site-packages/matplotlib/__init__.py:895: in <module>
    defaultParams = rcsetup.defaultParams = {
../.spack/.spack-env/view/lib/python3.8/site-packages/matplotlib/__init__.py:898: in <dictcomp>
    rcParamsDefault[key]),
../.spack/.spack-env/view/lib/python3.8/site-packages/matplotlib/__init__.py:674: in __getitem__
    return dict.__getitem__(self, key)
KeyError: 'webagg.port'

Operating system

macOS

Matplotlib Version

3.5.0

Matplotlib Backend

macosx

Python version

3.8.12

Jupyter version

N/A

Installation

from source (.tar.gz)

@tacaswell
Copy link
Member

due to the way we now use the matplotlibrc shipped as part of the package the expected outcome is

#backend: macosx
#webagg.port 8988

however, as of the last few minor releases of Matplotlib we will auto-detect which backend to use based on what is available so if you are on OSX, it will automatically default to using 'macosx' without needing to configure it as build time.

I'm working on the (2 character) PR to fix this...

@adamjstewart
Copy link
Contributor Author

Thanks! I'm working on Spack's matplotlib package and we would like to force an installation to use a particular backend. In prior releases, we could simply set this in setup.cfg (now mplsetup.cfg) and the matplotlibrc installed with the package would be correct. Is this still supported? Would we have to locate this file and remove the comment symbol ourselves?

P.S. If any matplotlib developers would like to help maintain Spack's build recipe for matplotlib just let me know

@tacaswell
Copy link
Member

tacaswell commented Nov 18, 2021

The matplotlibrc that is shipped as part of matplotlib is where we now store the defaults so it needs to have exactly 1 comment in front of it. I would point out that this does not actually force a particular backend, it sets the default if the user fails to set a different one.

I assume that spack has a way of including patches? The change in #21662 should be sufficient to un-stick you.

@adamjstewart
Copy link
Contributor Author

Okay, so if I understand correctly:

For older versions of matplotlib (3.4 and older), the matplotlibrc that gets installed has all settings commented out, and if you want to set something as the default, it needs to be uncommented.

For newer versions of matplotlib (3.5 and newer), the matplotlibrc that gets installed has all settings commented out, but the comment is removed during parsing, so #backend and backend are equivalent and used to provide the default values. These default values can be overridden in a ~/.matplotlib/matplotlibrc file and must be uncommented.

Is this understanding correct? Yes, Spack has a way to include patches, I'll test and include yours.

tacaswell added a commit to tacaswell/matplotlib that referenced this issue Nov 18, 2021
tacaswell added a commit to tacaswell/matplotlib that referenced this issue Nov 18, 2021
@tacaswell
Copy link
Member

@adamjstewart Yes that is correct.

@KevKeating
Copy link
Contributor

It's still possible to hit this issue if mplsetup.cfg doesn't specify a backend. In that scenario, the string "##backend: Agg" (without a newline at the end) is used from line 204 of setup.py instead of the "#backend {}:\n" (with a newline) on line 202. I was able to fix this locally by (not surprisingly) adding a \n to the end of "##backend: Agg" on line 204 to make it "##backend: Agg\n". Should I file a separate bug for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants