krishnaik06 / mlproject

453 stars 587 forks source link

Module Not Found Error #11

Open shavilya opened 1 year ago

shavilya commented 1 year ago

After running the src/components/data_ingestion.py , It gives ModuleNotFoundError : no module name 'src' , have tried every fix from bard , maily :

  1. import os

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 ?

balaji-bhaskarr commented 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

shrigulhane100 commented 1 year ago

(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 ?

shrigulhane100 commented 1 year ago

Solved: exception and logger files needed to be in the src folder, not the main directory. Hence could not import src module.

Anuragmukati commented 1 year ago

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:

  1. Ensure Proper Directory Structure: Make sure that your project directory structure is set up correctly. The 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
  1. Check PYTHONPATH: You mentioned that you already tried setting the PYTHONPATH, but please double-check that you have set it correctly. Open a terminal or command prompt and run:
export PYTHONPATH="ProjectDirectory"

Replace "ProjectDirectory" with the actual path to your project's root directory.

  1. Check Virtual Environment: Ensure that you are running the script within the correct virtual environment. Since you mentioned using Miniconda, activate your environment before running the script:
conda activate your_env_name

Replace "your_env_name" with the name of your Miniconda environment.

  1. Fixing the import statements in the data_ingestion.py file: In your 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.

  1. Ensure exception and logger files are in the src folder: As you mentioned in the "Solved" section, make sure that the 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.

itsemran commented 1 year ago

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, )

Harshchitaliya commented 1 year ago

(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)

KalpanaMehta commented 10 months ago

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.

whogaurab commented 8 months ago
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

shivagur commented 8 months ago

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..

Akash638932 commented 1 week ago

from src.mlproject.logger import logging

if name == 'main': logging.info("The execution has started")

output:- ModuleNotFoundError: No module named 'src.mlproject.logger'