[Python] ModuleNotFoundError: No module named 'src' when trying to import parent directory...

Discussão em 'Python' iniciado por Stack, Agosto 9, 2025.

  1. Stack

    Stack Membro Participativo

    I'm using VS code and Conda. I want to import logger.py or exception.py file from the src directory to the data_ingestion.py file which is at subdirectory called components in src directory. This is how directory look like:

    MLProjects
    |
    --src
    |
    -- __init__.py
    -- logger.py
    -- exception.py
    |
    --components
    --__init__.py|
    --data_ingestion.py


    Error message as follows

    Traceback (most recent call last):
    File "src/components/data_ingestion.py", line 8, in <module>
    from src.exception import logging
    ModuleNotFoundError: No module named 'src'


    Current working directory is ....\MLProjects

    I tried

    from src.exception import CustomException
    from src.logger import logging


    and then

    from ..exception import CustomException
    from ..logger import logging


    also tried with

    from .exception import CustomException
    from .logger import logging

    Continue reading...

Compartilhe esta Página