views:

42

answers:

2

I have a series of unit tests that I'm running with nose. For some of my tests, I'd like to remove a module's path from the sys.path so there is no conflict with what I am testing.

sys.path.remove('/path/to/remove/from/sys/path')

If I run the Python interpreter and call sys.path, the '/path/to/remove/from/sys/path' is there in the list. However, once nosetests is called, the above code cannot find it and gives me a "not found in list" error.

Why is nose not able to find the path in sys.path?

+1  A: 

Didn't you mean this?

sys.path.remove('/path/to/remove/from/sys/path')

If nose can't find it in sys.path then it wasn't there... nose does lots of diddling with sys.path on its own. Why not print sys.path and see what it actually is when run under nose

Nick Craig-Wood
Thank you for catching that. I've edited the question. I also printed several iterations of testing in nose and Eclipse, but I didn't see the pattern as to when certain paths were included or excluded. Right now, I've created a simple script to set the PYTHONPATH before running nosetests.
Wraith
A: 

Create a script, get_mod_py_path.py, to set the PYTHONPATH. In this case, it is dropping the conflicted path.

import os
import sys

# Remove the global Python modules from the PYTHONPATH.
path = os.environ['PYTHONPATH'].split(os.pathsep)
if os.environ['GLOB_PY_MODULES'] in path: 
    path.remove(os.environ['GLOB_PY_MODULES'])

# Construct the new path and print it. 
path = ':'.join(path)
print path

Then use it in a bash that calls nosetests.

PYTHONPATH=`python get_mod_py_path.py`   
nosetests --verbosity=1 --with-gae --where="../tests/unit" --gae-application="../app" 
Wraith