Ex46 ModuleNotFoundError

Python path seems to be one of the biggest issues that people face when starting out, and when using Nose (which is ivortually deprecated) the whole issue seems more so.

You can check that the module is in the path by adding the directory and checking the path afterwards.

Alternatively, bin Nosetest for unittest or better still, Pytest.

I know @zedshaw considers this book complete/done/finalised, but I do wonder if this section needs to be overhauled in light of the number of issues people have with Nose?

(@zedshaw I’m happy to have a go if it helps…)

1 Like

For now I’d like to stick with the book as-written. I’ll tackle unittest, Pytest, and whatever else is new and different, later.

So how do I fix this?

I made an addition to the code I posted above. I had “dumb_tests.py” print what it thinks the “path” is before attempting to import module “dumb.” To wit:

from os import sys
from nose.tools import *
print sys.path

When I run this revised version of “dumb_tests.py”, it looks like this:

PS C:\Users\WinTenProBox\Google Drive\Python\test_directory\projects\DumbProject> python .\tests\dumb_tests.py
['C:\\Users\\WinTenProBox\\Google Drive\\Python\\test_directory\\projects\\DumbProject\\tests', 'C:\\Windows\\SYSTEM32\\python27.zip', 'C:\\Python27\\DLLs', 'C:\\Python27\\lib', 'C:\\Python27\\lib\\plat-win', 'C:\\Python27\\lib\\lib-tk', 'C:\\Python27', 'C:\\Users\\WinTenProBox\\AppData\\Roaming\\Python\\Python27\\site-packages', 'C:\\Python27\\lib\\site-packages']
Traceback (most recent call last):
  File ".\tests\dumb_tests.py", line 5, in <module>
    import dumb
ImportError: No module named dumb

For some reason Python has the \tests subdirectory of my project in sys.path, but not the \dumb subdirectory. I guess that’s where the problem lies.

I think I found at least a temporary fix. If I add the following to “dumb_tests.py” before attempting to import module “dumb”:

sys.path.append("./dumb")

then “dumb_tests.py” completes successfully. Not sure whether this is the “textbook” answer but it seems to work.

I am having other difficulties with “setup.py” - it thinks one of my /bin scripts isn’t there. Another path problem I expect.

I noticed that and it is totally weird. Why would tests/ end up in your PYTHONPATH?

Close that powershell window, start a new one, or maybe even reboot your machine just to be sure, then go back and see if it’s there still.

Don’t know. /tests ends up in PYTHONPATH each time though. Tried rebooting and reopening Powershell.

I guess I have to add the appropriate folder to sys.path within each test script, or in anything else that is going to call something found in a subfolder (like /dumb/dumb.py)

You really need to find out why that’s happening because it means that somehow that directory was added to your system path and it will cause you problems later. Go into your system path and see if you can remove it. Also, the only way that I can think of this happening is if you added that directory to your system path then forgot about it.

You can temporarily fix this by typing this into your powershell:

$env:PYTHONPATH="."

That will tell python that the path to use should include whatever directory you’re currently in (that’s the dot.)

I’m getting the same error… How did you get through this thing?