3

I know this has been asked a lot of times, been looking at it for the last two hours and still can't figure it out. Would be very grateful for a solution to my actual problem rather than a link to someone else problem if possible.

Here goes...

/src
    /moduleA
        __init__.py
        messages.py
    /tests
        testA.py

messages.py

def get_messages(paramA):
    if paramA:
        return "A"
    else:
        return "B"

testA.py

import unittest
from moduleA import messages

My unit test will simply check whether A or B is returned from the get_messages(...) function.

As I understand it, moduleA is a module as it has an __init__.py file. VSCode does not complain about the from moduleA import messages line in my test however when I try to execute testA.py I get the following error message...

ImportError: No module named moduleA.

Also tried running it with python -m

Would be very grateful if someone can tell me what I'm doing wrong. I can change my file structure around if that would help. Perhaps the examples I've been reading are for Python 3 and I'm using 2.7?

Thanks.

2 Answers 2

5

Here's my suggestion on how to take care of this: ValueError: Attempted relative import in non-package not for tests package

So you'll need to add a __init__.py to your test directory, back up one directory to src then run python -m test.testA and you should be good to go.

Also, once you see that to run successfully, you'll see that you have a syntax error in get_messages: you're missing a colon after else.

Sign up to request clarification or add additional context in comments.

Comments

-2

Firstly, we should check configuration before running the file.

Cross check with spaces like:

ENVIRONMENT=mock
PYTHONPATH=./src

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.