Search code examples
pythonpython-3.xpython-sphinxread-the-docs

How to add 'matplotlib' to readthedocs.yaml or requirements.txt?


The scene:

  • wrote my own python package
  • used sphinx to create documentation for said package
  • I'm now trying to upload said documentation to readthedocs

The problem:

When I try to build the documentation, I end up with an error ModuleNotFoundError: No module named 'matplotlib'. I spent about two hours trying to figure out how to fix this, and I've added my .readthedocs.yaml and requirements.txt files, but I just copied what was on the readthedocs website, I can't find anything about how to add additional Python packages to these files (specifically what text I use to do so). I mean, everything says you can, and that you can add via the config or the yaml or through conda, but no example text. I really just need a simple example: "to add matplotlib module, add foo text to requirements.txt" or something like it

I'm sure it's a really simple obvious answer to people who work with this all the time, but this is my first time trying to publish documentation. Can anyone help?


Solution

  • Check the version of matplotlib that your package depends on like so:

    >>> import matplotlib
    >>> matplotlib.__version__
    '3.3.4'
    

    The format for requirements.txt is like packagename==version, so you should add the following line:

    matplotlib==3.3.4
    

    If using a Linux command-line, you can also do it this way:

    $ pip freeze | grep matplotlib
    matplotlib==3.3.4
    

    If working in a virtual environment where only the required packages are installed, you don't even need to do this by hand for each package; since pip freeze generates output in the correct format, you can just save the output of pip freeze to generate your whole requirements.txt file (see this Q&A for more details):

    $ pip freeze > requirements.txt