Joshua Segal
Joshua Segal

Reputation: 583

Python Import Errors: No module named <myPackage>

I know this should be easy and I know that other people online have asked this question, but I feel like I understand all of their answers and the concepts, yet my import is still throwing errors.

I have this directory structure

root/myPackage/
root/tests/test_1.py

in test_1.py I have:

import myPackage
...

I am running on the command line from the root:

$ pytest tests OR python tests/test_1.py

...just anything to get this script to run really. I've used pytest and have run python scripts before

and I keep getting the

No module named 'myPackage' error

I thought if I run the test from the root I would still be able to access myPackage... this is really contradicting my previous understanding and the internet... send help if you know why I'm getting importing errors.

ALSO, when I take the test_1.py out of the tests directory and run it from the root it works without errors. So this structure works

root/myPackage/
root/test_1.py

Upvotes: 1

Views: 1033

Answers (1)

Beethoven's 7th
Beethoven's 7th

Reputation: 116

Recreating your project from the information provided, I was able to reproduce your error and fixed it by including an __init__.py file under the tests directory.

Here's my project structure:

root/myPackage

  • __init__.py

  • module.py

root/tests

  • __init__.py

  • test_1.py

That should allow you to run pytest tests successfully.

Upvotes: 1

Related Questions