ModuleNotFoundError error with installed package in PyCharm

Question:

I’m trying to create my first package using the format from the python tutorial and am getting ModuleNotFoundError when trying to run unittests from the tests subfolder in PyCharm, but can run the same test fine through IDLE.

As per the tutorial this is the general folder structure

packaging_tutorial

── LICENSE

── README.md

── example_pkg

└── __init__.py

── setup.py

── tests

└── example_test.py

Initially I couldn’t get the test to run at all on either IDLE or PyCharm. After using pip to install the package through Command Prompt I’ve been able to run the tests through IDLE. However when trying to run them in PyCharm, I’m getting ModuleNotFound.

I’ve tried using the suggestions from this previous question of Invalidate Cache/Restart, I’ve checked Project Interpreter & the confirmed the package is listed, I’ve tried doing pip install of the package in the PyCharm terminal.

I’d imagine this is a relatively straightforward fix. This is my first project using PyCharm, and I’m guessing there was/is some preliminary stuff that I could/should have done that would avoid this issue.

Answers:

You need to add an empty __init__.py file in your tests folder to allow the test platform to successfully discover modules in outer directories.

Answered By: abc

After some mucking about on the Pycharm and the Pycharm forum, I think I’ve come up with a solution. Right-clicking the file name and selecting "Modify Run Configuration", and reselecting the file path seems to have done the trick. I’m guessing that because I moved the files from the folder they were created to a different one, that Pycharm was trying to maintain the original link.

Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.