Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 19, 2025

Describe the Pull Request

Pytest test collection fails with ModuleNotFoundError: No module named 'main' when importing __init__.py from certain directory contexts. The absolute import from main import workout doesn't resolve when the package is imported from a parent directory during test discovery.

Expected Behavior

Tests should collect and run successfully regardless of the directory context from which pytest is invoked.

Fixes the Following Issues

Resolves the test collection errors reported in the testing workflow:

  • ERROR collecting tests/test_gui.py - ImportError at __init__.py:4
  • ERROR collecting tests/test_main.py - ImportError at __init__.py:4

Changes

Modified __init__.py to handle both absolute and relative import contexts:

# Before
from main import workout

# After
try:
    from main import workout
except ImportError:
    from .main import workout

This allows the import to succeed when:

  • Running as an installed package (absolute import)
  • Importing from parent directories during test collection (relative import)

Desktop Developed On (please complete the following information):

  • OS: Linux
  • Program Version: 1.2.0
  • Python Version: 3.12.3

Additional Context

The fix maintains backward compatibility with existing import patterns while enabling proper test discovery. No functional changes to the workout() function or test suite.

Please ensure you have read the Contributor License Agreements. By creating your pull request, you agree to the CLA.

Original prompt

I get the following errors on the testing workflow, can you fix them so all the tests pass: ==================================== ERRORS ====================================
______________________ ERROR collecting tests/test_gui.py ______________________
ImportError while importing test module '/home/runner/work/PyWorkout/PyWorkout/tests/test_gui.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/hostedtoolcache/Python/3.11.14/x64/lib/python3.11/site-packages/_pytest/python.py:507: in importtestmodule
mod = import_path(
/opt/hostedtoolcache/Python/3.11.14/x64/lib/python3.11/site-packages/_pytest/pathlib.py:587: in import_path
importlib.import_module(module_name)
/opt/hostedtoolcache/Python/3.11.14/x64/lib/python3.11/importlib/init.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
:1204: in _gcd_import
???
:1176: in _find_and_load
???
:1126: in _find_and_load_unlocked
???
:241: in _call_with_frames_removed
???
:1204: in _gcd_import
???
:1176: in _find_and_load
???
:1126: in _find_and_load_unlocked
???
:241: in _call_with_frames_removed
???
:1204: in _gcd_import
???
:1176: in _find_and_load
???
:1147: in _find_and_load_unlocked
???
:690: in _load_unlocked
???
:940: in exec_module
???
:241: in _call_with_frames_removed
???
init.py:4: in
from main import workout
E ModuleNotFoundError: No module named 'main'
_____________________ ERROR collecting tests/test_main.py ______________________
ImportError while importing test module '/home/runner/work/PyWorkout/PyWorkout/tests/test_main.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/opt/hostedtoolcache/Python/3.11.14/x64/lib/python3.11/site-packages/_pytest/python.py:507: in importtestmodule
mod = import_path(
/opt/hostedtoolcache/Python/3.11.14/x64/lib/python3.11/site-packages/_pytest/pathlib.py:587: in import_path
importlib.import_module(module_name)
/opt/hostedtoolcache/Python/3.11.14/x64/lib/python3.11/importlib/init.py:126: in import_module
return _bootstrap._gcd_import(name[level:], package, level)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
:1204: in _gcd_import
???
:1176: in _find_and_load
???
:1126: in _find_and_load_unlocked
???
:241: in _call_with_frames_removed
???
:1204: in _gcd_import
???
:1176: in _find_and_load
???
:1126: in _find_and_load_unlocked
???
:241: in _call_with_frames_removed
???
:1204: in _gcd_import
???
:1176: in _find_and_load
???
:1147: in _find_and_load_unlocked
???
:690: in _load_unlocked
???
:940: in exec_module
???
:241: in _call_with_frames_removed
???
init.py:4: in
from main import workout
E ModuleNotFoundError: No module named 'main'


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits November 19, 2025 19:52
Co-authored-by: willtheorangeguy <18339050+willtheorangeguy@users.noreply.github.com>
This commit fixes the style issues introduced in 5644bd2 according to the output
from Black.

Details: #182
@github-actions
Copy link

Thank you for your contribution, please read and sign our CLA. Instructions on how to do so can be found in the CONTRIBUTING.md document.


I have read the CLA Document, and I hereby sign the CLA.


You can retrigger this bot by commenting recheck in this Pull Request. Posted by the CLA Assistant Lite bot.

Copilot AI changed the title [WIP] Fix import errors in testing workflow Fix ModuleNotFoundError in test collection caused by absolute import in __init__.py Nov 19, 2025
@willtheorangeguy willtheorangeguy marked this pull request as ready for review November 19, 2025 19:59
@willtheorangeguy willtheorangeguy merged commit 096652b into main Nov 19, 2025
11 of 13 checks passed
@willtheorangeguy willtheorangeguy deleted the copilot/fix-test-errors-in-workflow branch November 19, 2025 19:59
@github-actions github-actions bot locked and limited conversation to collaborators Nov 19, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants