Skip to content

Instantly share code, notes, and snippets.

@brydavis
Last active July 4, 2019 05:49
Show Gist options
  • Save brydavis/9d3ace8f980fba955a145e963677327e to your computer and use it in GitHub Desktop.
Save brydavis/9d3ace8f980fba955a145e963677327e to your computer and use it in GitHub Desktop.

unittest workaround if ModuleNotFoundError

This short tutorial is related to lesson one of UW's PCE Python 220 - Summer 2019.

If you're experiencing a error like ModuleNotFoundError: No module named ..., then follow try one of the two options below.

Basically, you have to tell Python the folder path for where the tests will run using by setting the PYTHONPATH environment variable.

You can do this while running your tests.

First, navigate to the assignment folder.

Then, follow the instructions below based on the location of your test scripts.

Running tests from tests folder

If you place your tests in the tests folder like this.

/assignment
├── /inventory_management
│   ├── __init__.py
│   ├── electricAppliancesClass.py
│   ├── furnitureClass.py
│   ├── inventoryClass.py
│   ├── main.py
│   └── market_prices.py
└── /tests
    ├── __init__.py
    ├── test_integration.py
    └── test_unit.py

Then run: PYTHONPATH=inventory_management/ python -m unittest tests.test_unit

Running tests from main assignment folder

If you place your root project folder (aka the 'assignment' folder) like this.

/assignment
├── /inventory_management
│   ├── __init__.py
│   ├── electricAppliancesClass.py
│   ├── furnitureClass.py
│   ├── inventoryClass.py
│   ├── main.py
│   └── market_prices.py
├── test_unit.py
└── test_integration.py

Then run: PYTHONPATH=inventory_management/ python -m unittest test_unit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment