Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Testing WorkLog w DB (TechDegree)

So I haven't done a ton of test writing in Python, I am sure this is a common issue run into by those learning to write tests.

I was following Python Testing

Kenneth his simple scripts separating into their respective folders, but he isnt using init.py files to create modules and isnt using sys.path.append() to create some patch for importing modules. He just references the imports and goes.

But if I separate my files into their respective sub-folders so I can have a tests.py for each script I get errors one of two ways.

1st: So if I leave my imports as is, my script for the WorkLog project will run as it is suppose to but I cannot run python -m unittest tests.py in that folder nor can I run coverage run tests.py in that folder because of ImportError It either ends up reading ImportError: No module named 'foo' or ImportError: attempted relative import with no known parent package.

2nd: If I adjust my imports so that I am able to run coverage or python unittest properly then I am unable to run the actual program script with python app.py then I get the same style of import errors.

I searched around other forums and have tried some recommendations like putting init.py in subfolder so python recognizes it as a module. Then I was going to try doing an import sys to do sys.path.append() with location of the script. But at this point I am trying to really understand why any of this is necessary seeing as my scripts arent set up much different from that in this Python Testing course. The only difference I am seeing that there is, is Mac vs Windows Python setup is affecting my imports?


So I am having success with using a structure like what was done here: (throwing my tests in a separate "module")

StackOverflow Link

But I am still really curious what is causing tests and coverage to fail with ImportErrors from within the same module, while in working directory. Just as Kenneth Love was doing in his Python Testing Course. It even says in that stack overflow post that the unittest loader does the sys.path automatically so you dont have to.