Doctest in external packages

74 views
Skip to first unread message

Benjamin Schmidt

unread,
Aug 20, 2019, 8:27:32 AM8/20/19
to sage-devel
I ran into an issue regarding sage doctests. Once I don't test individual files, but make a package things go south. From what I understand testing packages requires to add import statements to the doctests, but I cannot make it work.

I have a folder example with two files: An empty __init__.py and an example.py with the following code:

def square(n):
    r"""
    EXAMPLES::

        sage: from example.example import square
        sage: square(2)
        4
    """
    return n**2

I then run "sage -t example/" and get "ImportError: No module named example.example". I wonder how to resolve this and what the intended way of testing external packages with doctests is? Copying the example folder into the $SAGE_ROOT/src/bin/ makes things works, but obviously that's not a real solution. I also tried "sage -t --force_lib example/", but not difference.

Thanks!
Benjamin

Vincent Delecroix

unread,
Aug 20, 2019, 9:19:56 AM8/20/19
to sage-...@googlegroups.com
You first need to *install* your module. Otherwise your example.py
can not be found by Python (or Sage). You can also hack the
PYTHON_PATH if it is for testing purposes.

Vincent

Michael Orlitzky

unread,
Aug 20, 2019, 9:22:01 AM8/20/19
to sage-...@googlegroups.com
On 8/20/19 8:24 AM, Benjamin Schmidt wrote:
> I have a folder example with two files: An empty __init__.py and an
> example.py with the following code:
>
> def square(n):
>     r"""
>     EXAMPLES::
>
>         sage: from example.example import square
>         sage: square(2)
>         4
>     """
>     return n**2
>
> I then run "sage -t example/" and get "ImportError: No module named
> example.example".

You have to run the tests with PYTHONPATH="." sage -t ..., otherwise
sage/python doesn't know where to look for the "example" module.

Benjamin Schmidt

unread,
Aug 20, 2019, 9:55:32 AM8/20/19
to sage-devel
I'm not sure I fully understand. What does it precisely mean to install my module? I'm able to put the package into the sage src folder, add imports to all.py etc. This will make the doctests work. However, that has the major drawback that I have to run "make" in the sage folder everytime I want to run a test. It takes quite a while. For now I also don't intend to have the package I'm currently working on included in sage. I don't think many people besides my collaborators and me would ever use it.

Running it with PYTHONPATH=''.", as suggested by both of you, works though.

Thank you,
Benjamin

Vincent Delecroix

unread,
Aug 20, 2019, 10:01:56 AM8/20/19
to sage-...@googlegroups.com
Typing "Installing Python modules" in a reasonable search engine
gives me reasonable links

https://docs.python.org/3/installing/index.html
https://docs.python.org/3/distributing/index.html

You definitely don't need to modify the Sage source code.

If it is jut a matter of testing then the PYTHONPATH hack is fine.
If you want to share your module with others, you should pay
attention to the links above.

Vincent
Reply all
Reply to author
Forward
0 new messages