'ModuleNotFoundError error with installed package in PyCharm [duplicate]
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.
Solution 1:[1]
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 Condition", 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.
Solution 2:[2]
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.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | kevin_insert_somethin_fun_here |
Solution 2 | abc |