how to add a package to sys path for testing
<p>This question is occasioned by instructions in the <a href="http://docs.python-guide.org/en/latest/writing/structure/" rel="noreferrer">python guide</a> for adding a project to sys path to use in tests, which do not seem to work unless I am misunderstanding the instructions</p>

<p>I have a directory structure for a python project like this</p>

<pre><code>sample/a.py
sample/b.py
sample/c.py
sample/__init__.py
test/context.py
test/test_something.py
test/__init__.py
docs
</code></pre>

<p>According to <a href="http://docs.python-guide.org/en/latest/writing/structure/" rel="noreferrer">the python guide</a>, I should create a test/context.py file and add this</p>

<pre><code>import os
import sys
sys.path.insert(0, os.path.abspath('..'))

import sample
</code></pre>

<p>Then, in my <code>test/test_something.py</code> file, it says I can do this</p>

<pre><code>from .context import sample
</code></pre>

<p>The guide says "This will always work as expected".</p>

<p>but, when I <code>cd</code> into test and run</p>

<pre><code>python -m unittest test_something
</code></pre>

<p>I get an error</p>

<pre><code>ValueError: Attempted relative import in non-package
</code></pre>

<p>and the error message specifically refers to this: <code>from .context import sample</code></p>

<p>Question: How can I add my sample package to the sys path correctly? </p>

<p>When answering, can you also clarify if the solution will handle absolute imports within the sample package. For example, my sample.a imports sample.b etc. When I had my tests structured a different way, I did an absolute import of sample.a, but since it has a relative import of <code>from .b import Boo</code>, it produced a similar error</p>

<p>Update</p>

<pre><code>`File "/usr/local/lib/python2.7/runpy.py", line 162 in _run_module_as_main "__main__", fname, loader, pkg_name)
File  "/usr/local/lib/python2.7/runpy.py", line 72, in _run_code exec code in run_globals
File  "/usr/local/lib/python2.7/unittest/__main__.py", line 12, in module main(module=None)
File  "/usr/local/lib/python2.7/unittest/main.py", line 94, in __init__ self.parseArgs(argv)
File  "/usr/local/lib/python2.7/unittest/main.py", line 149 in parseArgs self.createTests()
File  "/usr/local/lib/python2.7/unittest/main.py", line 158, in createTests self.module)
File  "/usr/local/lib/python2.7/unittest/loader.py", line 130, in loadTestsFromNames suites = [self.loadTestsFromName(name,module) for name in names]
File  "/usr/local/lib/python2.7/unittest/loader.py", line 91, in loadTestsFromName module = __import__('-'.join(parts_copy))
File "test_something.py", line 8, in module from .context import sample
</code></pre>

<p>Update</p>

<p>if I run the following command from root directory</p>

<pre><code>   python -m unittest test
</code></pre>

<p>It says , "Ran 0 tests in 0.000s"</p>

<p>If, as was suggested in the comments by @cuongnv, I run this from root directory</p>

<pre><code>python -m unittest test/test_something.py
</code></pre>

<p>or this (without the file extension)</p>

<pre><code>python -m unittest test/test_something 
</code></pre>

<p>It says "Import by filename is not supported"</p>