[Python] Can't import my own module in my test file

Discussão em 'Python' iniciado por Stack, Junho 6, 2025.

  1. Stack

    Stack Membro Participativo

    I have the following project structure :

    .
    ├── README.md
    ├── document_processing
    │ ├── Config.py
    │ ├── DocumentParser.py
    │ ├── FileChecker.py
    │ └── __init__.py
    ├── indexing.py
    └── tests
    ├── CheckFileTest.py
    └── __init__.py


    In CheckFileTest.py, I have implemented some tests for the FileChecker.py functions.

    import unittest
    from document_processing.Config import Config
    from document_processing.FileChecker import FileChecker

    class CheckFileTest(unittest.TestCase):
    def __init__(self):
    self.config = Config()
    self.fc = FileChecker(self.config)

    def test_not_empty_line(self):
    ...

    def test_line_format(self):
    ...

    if __name__ == "__main__":
    unittest.main()


    Unfortunately, I got this error when I run the test file (launched from the project root) :

    Traceback (most recent call last):
    File "/Users/thomaslaurent/Dev/MIRCV_project/tests/CheckFileTest.py", line 3, in <module>
    from document_processing.Config import Config
    ModuleNotFoundError: No module named 'document_processing


    I have seen many things on that, but I can't figure out what's blocked with this structure.

    With relative import :

    from ..document_processing.Config import Config


    I got this error :

    ImportError: attempted relative import with no known parent package


    EDIT

    Following the comments of @sinoroc, I don't make my tests a package and I have created a setup.py file :

    from setuptools import setup

    setup(
    name="MIRCV_project",
    version="1.0",
    description="",
    author="Thomas LAURENT",
    author_email="",
    packages=["document_processing"],
    install_requires=[], # external packages acting as dependencies
    )


    And I finally run :

    python -m pip install --editable .


    You can vote for a previous answer of @sinoroc on setup.py configuration : How to make setup.py for standalone application

    Continue reading...

Compartilhe esta Página