#๐Ÿ”’ Unit test .py fails to import module

39 messages ยท Page 1 of 1 (latest)

serene rain
#

Below is the folder structure

--src
init.py
game.py
player.py
board.py
--tests
init.py
test_board.py

in my test_board.py I have the following code.

import unittest
import src.player
import src.board
import src.game


class TestBoard(unittest.TestCase):
    def test_space_occupied(self):
        ...

For some reason I am getting module = __import__(module_name) ^^^^^^^^^^^^^^^^^^^^^^^ error.

If it help, I also have setup.py above the src and the tests folder and have this setup.

packages=find_packages(where='src'),  # This function automatically finds all packages in the 'src' directory.
    package_dir={'': 'src'},  # This specifies that the packages are located under the 'src' directory.

Why can't my unit test simply import modules from the src folder?

limber steepleBOT
#

@serene rain

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fresh glen
#

paste the full error please

serene rain
#
D:\CodingProjects\RefactoringAssignment\portfolio-por-part-1-modularisation-and-2d-structures-Mirai999\.venv\Scripts\python.exe "D:/Program Files/JetBrains/PyCharm 2024.1.4/plugins/python/helpers/pycharm/_jb_unittest_runner.py" --target test_board.TestBoard 
Testing started at 6:07 pm ...
Launching unittests with arguments python -m unittest test_board.TestBoard in D:\CodingProjects\RefactoringAssignment\portfolio-por-part-1-modularisation-and-2d-structures-Mirai999\tests


Error
Traceback (most recent call last):
  File "D:\Softwares\Python\Lib\unittest\loader.py", line 33, in testFailure
    raise self._exception
ImportError: Failed to import test module: test_board
Traceback (most recent call last):
  File "D:\Softwares\Python\Lib\unittest\loader.py", line 137, in loadTestsFromName
    module = __import__(module_name)
             ^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\CodingProjects\RefactoringAssignment\portfolio-por-part-1-modularisation-and-2d-structures-Mirai999\tests\test_board.py", line 3, in <module>
    import src.board
  File "D:\CodingProjects\RefactoringAssignment\portfolio-por-part-1-modularisation-and-2d-structures-Mirai999\src\board.py", line 1, in <module>
    from player import Player
ModuleNotFoundError: No module named 'player'




Ran 1 test in 0.003s

FAILED (errors=1)

Process finished with exit code 1

Thank you for the reply

harsh kite
#

did you try import player instead of import src.player?

serene rain
#

I did, but the IDE throw red squiggle with underline comment stating no module called "player" =(.

fresh glen
#

the issue is opposite

undone eagle
#

your top level package should not be named src

harsh kite
#

the linter errors but does your code error

#

does it error if you try to run your code

serene rain
#

lemme try

fresh glen
#

you haven't packaged your code properly so in some spots it expects src.player

undone eagle
#

src is the folder where you put your sources, but it should contain your top level package

fresh glen
#

the actually erroring line is from player import Player

serene rain
#

ModuleNotFoundError: No module named 'player'

hmm

serene rain
fresh glen
#

well yeah because you haven't packaged your code

serene rain
#

do I do that with install pip?

fresh glen
#

no

undone eagle
#

do you have a standard pyproject.toml in your project root?

fresh glen
#

create a pyproject.toml with proper packaging set up

serene rain
#

I don't =(. Will look that up. Thanks everyone

fresh glen
#

you shouldn't import anything from src in the end

serene rain
#

I have setup.py, I think that's the prefered method by the lecturer

fresh glen
#

setup.py should work too but you have to install your package as editable either way

#

do pip install -e .

#

(your lecturer is wrong btw you should definitely use pyproject.toml over setup.py)

serene rain
#

gotcha, but I've been marked down for even including the wrong unit test module in the past.

undone eagle
#

and is still needed if you use setuptools and have a C extension

fresh glen
#

tell your lecturer to learn about uv

#

actually don't, but learn about uv on your own

#

makes this sort of thing way easier

serene rain
#

Will do once I finished the assignment. Got it working now after installing the package.
Guess I have to read doc more often and not to chatgpt things from the get go.

undone eagle
#

using AI when you're a beginner can be very detrimental to your learning as AI often gives you wrong advice

#

more experienced programmers can figure out when the AI is just making up stuff like it does quite often

limber steepleBOT
#
Python help channel closed for inactivity

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.