The problem stems from the fact that python does not know where to look for address_book. You can fix this by setting the PYTHONPATH environment variable before starting python.
Given the following directory structure:
base_dir
+-- address_book
| +-- __init__.py
| +-- models.py
+-- test
| +-- test_models.py
Then this will work:
$ env PYTHONPATH=/path/to/base_dir python address_book/test/test_models.py
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
PYTHONPATH can be a relative path as well, just so long as the relative path points to the source directory from the current working directory
To avoid typing out the env PYTHONPATH=/path/to/base_dir each time you can set it using your shells appropriate syntax. Below I set the PYTHONPATH environment variable using a bash syntax.
$ cd /path/to/base_dir
$ export PYTHONPATH=. # the current directory as a relative link
$ python address_book/test/test_models.py
.
----------------------------------------------------------------------
Ran 1 test in 0.001s
OK
As a side note I would alter your directory structure as it's not a great idea to keep your tests in the same directory tree as your source directory. Typically you might do something like this:
base_dir
+-- address_book
| +-- __init__.py
| +-- models.py
+-- test # no __init__, not a module
| +-- test_models.py
This allows you to more easily package your project without also including your tests.
__init__.pyis a file, not a directory, right?python address_book/test/test_models.pyworks wherepython test_models.pydoes not.