Skip to content

The sphinx plot directive does not find files relative to the document that contains them. #10350

Open

Description

@183amir

Bug report

Bug summary

According to the documentation when plot_basedir is empty or None. The source-code files are found relative to the document that contains them:

plot_basedir
Base directory, to which plot:: file names are relative to. (If None or empty, file names are relative to the directory where the file containing the directive is.)

but this is not true in the source code and filenames are found relative to conf.py instead:

rst_file = document.attributes['source']
rst_dir = os.path.dirname(rst_file)
if len(arguments):
if not config.plot_basedir:
source_file_name = os.path.join(setup.app.builder.srcdir,
directives.uri(arguments[0]))
else:
source_file_name = os.path.join(setup.confdir, config.plot_basedir,
directives.uri(arguments[0]))

On line 689, I believe rst_dir must be used instead of setup.app.builder.srcdir (which points to the folder of conf.py).

Code for reproduction

The minimal example to reproduce this is here:
minimal.zip
Here is how the folder structure looks like:

minimal
└── source
    ├── conf.py
    ├── index.rst
    ├── plots
    │   ├── plot.py
    │   └── plot.rst

where the conf.py contains (besides what is in template):

extensions = ['matplotlib.sphinxext.plot_directive']

and plot.rst contains:

.. plot:: plot.py

and index.rst contains:

.. toctree::
   :maxdepth: 2
   :caption: Contents:
   plots/plot.rst

Running make html on this minimal example fails with:

Running Sphinx v1.6.6
making output directory...
loading pickled environment... not yet created
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 2 source files that are out of date
updating environment: 2 added, 0 changed, 0 removed
/tmp/minimal/source/index.rst:9: WARNING: Error in "toctree" directive:                                                                                                             
invalid option block.

.. toctree::
   :maxdepth: 2
   :caption: Contents:
   plots/plot.rst

Exception occurred:
  File "/usr/lib/python3.6/site-packages/matplotlib/sphinxext/plot_directive.py", line 708, in run
    with io.open(source_file_name, 'r', encoding='utf-8') as fd:
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/minimal/source/plot.py'
The full traceback has been saved in /tmp/sphinx-err-36njex0o.log, if you want to report the issue to the developers.
Please also report this if it was a user error, so that a better error message can be provided next time.
A bug report can be filed in the tracker at <https://github.com/sphinx-doc/sphinx/issues>. Thanks!

Expected outcome

I expect the resolved paths to the Python file to be relative to the .rst file per documentation.
('/tmp/minimal/source/plots/plot.py' in the example above).

Matplotlib version

  • Operating system: Arch Linux
  • Matplotlib version: 2.1.2
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.6
  • Other libraries: Sphinx v1.6.6

Activity

  1. anntzer commented on Sep 26, 2018

    @anntzer
    Contributor

    Sounds like a reasonable feature request (I think I agree that the proposed semantics are preferable), but of course there's going to be backcompat issues. I think I have a reasonable way to handle them...

  2. 183amir commented on Oct 9, 2018

    @183amir
    Author

    If you want, I can open a pull request with the proposed fix.

  3. anntzer commented on Oct 9, 2018

    @anntzer
    Contributor

    Go for it.

  4. added a commit that references this issue on Oct 9, 2018
    bd024b8
  5. 183amir commented on Oct 9, 2018

    @183amir
    Author

    Done in #12456

  6. added a commit that references this issue on Mar 3, 2019
    78f423a
  7. github-actions commented on Apr 30, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  8. anntzer commented on Apr 30, 2023

    @anntzer
    Contributor

    Would still be nice to fix, just needs some deprecation management...

  9. github-actions commented on Jul 1, 2024

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  10. added
    keepItems to be ignored by the “Stale” Github Action
    on Jul 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions