ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 16 Mar 2011 09:26:30 +0100trouble using import and doctest togetherhttps://ask.sagemath.org/question/7998/trouble-using-import-and-doctest-together/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?Mon, 14 Mar 2011 20:30:21 +0100https://ask.sagemath.org/question/7998/trouble-using-import-and-doctest-together/Comment by paragon for <p>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 </p>
<pre><code>sage -t myfile.py
</code></pre>
<p>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 </p>
<pre><code>import sys
sys.path.append(".")
</code></pre>
<p>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?</p>
https://ask.sagemath.org/question/7998/trouble-using-import-and-doctest-together/?comment=21995#post-id-21995yes, that is right. Or if I have "from myotherfile import g" just listed at the top of myfile, it also doesn't work.Tue, 15 Mar 2011 11:29:45 +0100https://ask.sagemath.org/question/7998/trouble-using-import-and-doctest-together/?comment=21995#post-id-21995Comment by Simon King for <p>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 </p>
<pre><code>sage -t myfile.py
</code></pre>
<p>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 </p>
<pre><code>import sys
sys.path.append(".")
</code></pre>
<p>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?</p>
https://ask.sagemath.org/question/7998/trouble-using-import-and-doctest-together/?comment=21997#post-id-21997You 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!Tue, 15 Mar 2011 03:43:02 +0100https://ask.sagemath.org/question/7998/trouble-using-import-and-doctest-together/?comment=21997#post-id-21997Comment by paragon for <p>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 </p>
<pre><code>sage -t myfile.py
</code></pre>
<p>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 </p>
<pre><code>import sys
sys.path.append(".")
</code></pre>
<p>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?</p>
https://ask.sagemath.org/question/7998/trouble-using-import-and-doctest-together/?comment=21996#post-id-21996yes, that is right. Or if I have "from myotherfile import g" just listed at the top of myfile, it also doesn't work.Tue, 15 Mar 2011 11:29:44 +0100https://ask.sagemath.org/question/7998/trouble-using-import-and-doctest-together/?comment=21996#post-id-21996Answer by Felix Lawrence for <p>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 </p>
<pre><code>sage -t myfile.py
</code></pre>
<p>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 </p>
<pre><code>import sys
sys.path.append(".")
</code></pre>
<p>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?</p>
https://ask.sagemath.org/question/7998/trouble-using-import-and-doctest-together/?answer=12194#post-id-12194I 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](http://docs.python.org/library/unittest.html). 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).Wed, 16 Mar 2011 09:26:30 +0100https://ask.sagemath.org/question/7998/trouble-using-import-and-doctest-together/?answer=12194#post-id-12194