question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ModuleNotFoundError when running tests from subfolders

See original GitHub issue

Describe the bug Running unittest tests from project subfolders is very fragile. It’s common to receive the error "ModuleNotFoundError: No module named ‘[your test module]’ when executing.

The following seems to work:

  • Test modules in the root of the project
  • Test modules in a subfolder, with the same name as the subfolder

Everything else fails.

Steps to Reproduce

  1. Unzip TestErrors.zip and try to execute tests.

Expected behavior Test methods are executed.

Additional context and screenshots

    Failed to import test module: tests_foo
    Traceback (most recent call last):
      File "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python37_64\lib\unittest\loader.py", line 154, in loadTestsFromName
        module = __import__(module_name)
    ModuleNotFoundError: No module named 'tests_foo'

Sample Code TestErrors.zip

Diagnostics file Diagnostic Info 2020-03-21T162604.txt

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
PeterRusznakcommented, Jun 18, 2021

To my experience the problem still exist. __init__py -pp ----init.py ----person.py -test_pp ----__init__py ----test_person.py

from pp.person import Person throws Error:

ModuleNotFoundError: No module named 'pp'
0reactions
svabracommented, Jun 30, 2022

by name, and by subdirectory.name

did you fix/workaround this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

unit testing - Run all tests from subdirectories in Python
A good approach is to run all the tests in a subdirectory from the command line. In order to find the following files...
Read more >
Good Integration Practices
Your tests can run against an installed version after executing pip install . . Your tests can run against the local copy with...
Read more >
Modules from specified sub-folders not recognized/loaded
is fully recognized by autocomplete. When I run it, I get the following: No module named folder1.module1.
Read more >
Python VSCode not seeing tests in subfolder
I've broken up my Python project into 2 modules, each one with its own folder. Each folder contains a file for unit tests...
Read more >
İmport a python function inside a subfolder
It still says ModuleNotFoundError: No module named 'my_functions' ... I am running test.py from the myProjects folder using python test.py .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found