Open shavilya opened 1 year ago
Even I was facing the same issue, try entering the below line in terminal -> export PYTHONPATH="ProjectDirectory", do not include /src only the project directory
(D:\project\ML-Ops\venv) D:\project\ML-Ops>python D:\project\ML-Ops\src\components\data_ingestion.py
Traceback (most recent call last):
File "D:\project\ML-Ops\src\components\data_ingestion.py", line 3, in
Can anyone let me know how to fix it ?
Solved: exception and logger files needed to be in the src folder, not the main directory. Hence could not import src module.
Based on the information provided, it seems that the issue is related to the Python module import and the project directory structure. Here's a step-by-step guide to help you resolve the "ModuleNotFoundError: No module named 'src'" issue:
src
directory should be a subdirectory of your project root directory. It should look like this:ProjectDirectory/
└── src/
├── components/
│ └── data_ingestion.py
├── exception.py
└── logger.py
export PYTHONPATH="ProjectDirectory"
Replace "ProjectDirectory" with the actual path to your project's root directory.
conda activate your_env_name
Replace "your_env_name" with the name of your Miniconda environment.
data_ingestion.py
script, update the import statement to use the correct path:from exception import CustomException
Since the script is already running from within the "src/components" directory, you don't need to include "src" in the import statement.
exception.py
and logger.py
files are located inside the src
directory, not in the main project directory.After making these changes, try running the script again:
python ProjectDirectory/src/components/data_ingestion.py
If you've followed these steps correctly, the "ModuleNotFoundError" should be resolved, and your script should run without any issues. If you encounter any other issues, double-check the file paths and import statements to ensure they are correct.
paste it as it is and then run LOGFILE = f"{datetime.now().strftime('%m%d%Y%H%M%S')}.log" logs_path = os.path.join(os.getcwd(), "logs") os.makedirs(logs_path, exist_ok=True)
LOG_FILE_PATH = os.path.join(logs_path, LOG_FILE)
logging.basicConfig( filename=LOG_FILE_PATH, format="[%(asctime)s] %(lineno)d %(name)s - %(levelname)s - %(message)s", level=logging.INFO, )
(D:\project\ML-Ops\venv) D:\project\ML-Ops>python D:\project\ML-Ops\src\components\data_ingestion.py Traceback (most recent call last): File "D:\project\ML-Ops\src\components\data_ingestion.py", line 3, in from src.exception import CustomException ModuleNotFoundError: No module named 'src'
Can anyone let me know how to fix it ?
you have to only import import exception
and last except Exception as e: raise exception.CustomException(e,sys)
The code below will resolve the 'ModuleNotFoundError' that occurs when running the 'ingestion.py' file.
from pathlib import Path sys.path.append(str(Path(file).parent.parent)) from exception import CustomException from logger import logging
sys.path.append(str(Path(file).parent.parent)) ---> This line adds the parent's parent directory of the current script (ingestion.py) to the list of places Python looks for modules.
from pathlib import Path
sys.path.append(str(Path('src').parent.parent))
just add this command in your logger and data ingestion file after
import sys
Try to include exception and logger files needed to be in the src folder. and also for running data_ingestion.py file
python -m ProjectDirectory.src.components.data_ingestion so wont be able to get the No Module found error..
from src.mlproject.logger import logging
if name == 'main': logging.info("The execution has started")
output:- ModuleNotFoundError: No module named 'src.mlproject.logger'
After running the src/components/data_ingestion.py , It gives ModuleNotFoundError : no module name 'src' , have tried every fix from bard , maily :
os.environ['PYTHONPATH'] = os.path.dirname(file) + '/src'
from src.exception import CustomException
2.import shutil
shutil.move('src', 'my_project/src')
from my_project.src.exception import CustomException
but haven't got it resolved.
Also , I am using miniconda env . Can anyone let me know how to fix it ?