Ask Your Question
0

trouble using import and doctest together

asked 2011-03-14 14:30:21 -0500

paragon gravatar image

I have a project that contains several .py files that I want to doctest. They are interrelated and each has a few imports that load the dependencies. When I run

sage -t myfile.py

then it says that it can't find the module that myfile wants to import, even though they are in the same directory. If I put

import sys
sys.path.append(".")

at the top of every file, it works fine, provided that I run sage -t from the sage directory. This is a little annoying, is there a better way?

edit retag flag offensive close merge delete

Comments

You mean: You have, myfile.py containing a test with "sage: from myotherfile import f", and you have myotherfile.py containing the definition of f? Then the import works if you do it in a sage session, but not when you do it in "sage -t myfile.py"? That's a good question that I'd like to know the answer of, myself!

Simon King gravatar imageSimon King ( 2011-03-14 21:43:02 -0500 )edit

yes, that is right. Or if I have "from myotherfile import g" just listed at the top of myfile, it also doesn't work.

paragon gravatar imageparagon ( 2011-03-15 05:29:44 -0500 )edit

yes, that is right. Or if I have "from myotherfile import g" just listed at the top of myfile, it also doesn't work.

paragon gravatar imageparagon ( 2011-03-15 05:29:45 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2011-03-16 03:26:30 -0500

Felix Lawrence gravatar image

I don't know how to solve your problem for doctests, but if you're otherwise feeling limited by doctests you may be better served by Python's more heavy-duty unittest framework. If you used this framework, your tests would be in different files to the code and you shouldn't have problems like this (I think!). Note the "see also" box near the top of the unittest page for even more alternative testing frameworks (though most of those are not built in to sage).

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2011-03-14 14:30:21 -0500

Seen: 198 times

Last updated: Mar 16 '11