0

Sphinx-gallery offers the option to create .py files that can be generated into .ipynb files and directly execute them for documentation.

I have the following file structure:

Project/
|--doc
|  |--Makefile
| 
|--examples
|  |--data
|  |--plot_example_1.py
|  
|--tests
|  |--test_run_examples.py

In plot_example_1.py I now want to read the example data stored in data, and therefore require the path of the current directory. When first generating the documentation I can resolve it with

from pathlib import Path

Path().resolve()

I want to test the examples however additionally using pytest. This fails when calling pytest tests from the root directory.

Instead I could succesfully test it when adapting the path call to Path(__file__).absolute(), but the __file__ option is not available in the ipynb as explained here

So I need an option that can resolve the path for both the python file and ipynb. Is there any option for that?

mzjn
  • 48,958
  • 13
  • 128
  • 248
Merk
  • 171
  • 12

1 Answers1

0

A simple option would be to pass the absolute pass with pytest to the module, and then check if the argv has that parameter.

Otherwise a potential option is to shift the data folder in the main project directory and load it from there:

import project_name
Path(project_name.__file__).parent / "data"

Of course with the trade-off that the data needs to be installed with the main package. Hopefully there is a better solution in future.

@larsoner provided an answer here

Merk
  • 171
  • 12