Repository navigation
Description
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:
matplotlib/lib/matplotlib/sphinxext/plot_directive.py
Lines 684 to 693 in 44a9036
| 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.pyand index.rst contains:
.. toctree::
:maxdepth: 2
:caption: Contents:
plots/plot.rstRunning 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
anntzer commentedon Sep 26, 2018
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...
183amir commentedon Oct 9, 2018
If you want, I can open a pull request with the proposed fix.
anntzer commentedon Oct 9, 2018
Go for it.
sphinx plot directive: sources relative to rst file
183amir commentedon Oct 9, 2018
Done in #12456
sphinx plot directive: sources relative to rst file
github-actions commentedon Apr 30, 2023
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!
anntzer commentedon Apr 30, 2023
Would still be nice to fix, just needs some deprecation management...
github-actions commentedon Jul 1, 2024
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!