Skip to content

[Bug]: Relative paths in plt.style.use() no longer work in 3.7 #25242

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
st-bender opened this issue Feb 17, 2023 · 1 comment · Fixed by #25268
Closed

[Bug]: Relative paths in plt.style.use() no longer work in 3.7 #25242

st-bender opened this issue Feb 17, 2023 · 1 comment · Fixed by #25268
Milestone

Comments

@st-bender
Copy link

Bug summary

Version 3.7 broke using relative paths in plt.style.use() raising a TypeError. It was working fine in 3.6. Prepending with a ./ as suggested in the error message did not help.

Code for reproduction

import matplotlib.pyplot as plt
plt.sylte.use("../mystyle.style")

Actual outcome

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[11], line 1
----> 1 plt.style.use("../mystyle.style")

File ~/envs/py39/lib/python3.9/site-packages/matplotlib/style/core.py:153, in use(style)
    151 pkg, _, name = style.rpartition(".")
    152 try:
--> 153     path = (importlib_resources.files(pkg)
    154             / f"{name}.{STYLE_EXTENSION}")
    155     style = _rc_params_in_file(path)
    156 except (ModuleNotFoundError, IOError) as exc:
    157     # There is an ambiguity whether a dotted name refers to a
    158     # package.style_name or to a dotted file path.  Currently,
   (...)
    161     # either use Path objects or be prepended with "./" and use
    162     # the slash as marker for file paths.

File ~/envs/py39/lib/python3.9/site-packages/importlib_resources/_common.py:46, in package_to_anchor.<locals>.wrapper(anchor, package)
     44 elif anchor is undefined:
     45     return func()
---> 46 return func(anchor)

File ~/envs/py39/lib/python3.9/site-packages/importlib_resources/_common.py:56, in files(anchor)
     51 @package_to_anchor
     52 def files(anchor: Optional[Anchor] = None) -> Traversable:
     53     """
     54     Get a Traversable resource for an anchor.
     55     """
---> 56     return from_package(resolve(anchor))

File ~/envs/py39/lib/python3.9/functools.py:888, in singledispatch.<locals>.wrapper(*args, **kw)
    884 if not args:
    885     raise TypeError(f'{funcname} requires at least '
    886                     '1 positional argument')
--> 888 return dispatch(args[0].__class__)(*args, **kw)

File ~/envs/py39/lib/python3.9/site-packages/importlib_resources/_common.py:82, in _(cand)
     80 @resolve.register
     81 def _(cand: str) -> types.ModuleType:
---> 82     return importlib.import_module(cand)

File ~/envs/py39/lib/python3.9/importlib/__init__.py:122, in import_module(name, package)
    119 if not package:
    120     msg = ("the 'package' argument is required to perform a relative "
    121            "import for {!r}")
--> 122     raise TypeError(msg.format(name))
    123 for character in name:
    124     if character != '.':

TypeError: the 'package' argument is required to perform a relative import for '../mystyle'

Expected outcome

Style is loaded.

Additional information

No response

Operating system

No response

Matplotlib Version

3.7.0

Matplotlib Backend

module://matplotlib_inline.backend_inline

Python version

3.9.16

Jupyter version

6.5.2

Installation

conda

@anntzer
Copy link
Contributor

anntzer commented Feb 17, 2023

Oops. I guess the fix is either to also catch TypeError in style/core.py, or to explicitly check for pkg not being empty?

@tacaswell tacaswell added this to the v3.7.1 milestone Feb 18, 2023
@rcomer rcomer linked a pull request Feb 20, 2023 that will close this issue
1 task
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