I am working on a Python project with the basic folder structure listed below, and examples of what each Python file contains is in curly brackets.
| generate_recommendations.py
├───.ipynb_checkpoints
├───.vscode
├───csv
├───dao
| | ratingDAO.py { contains a class named RatingDAO }
│ ├───config
│ ├───core
| | rating.py { contains a class named Rating }
│ ├───db
OBJECTIVE: I want to import rating.py in ratingDAO.py, and in turn want to import ratingDAO.py into generate_recommendations.py, and have all imports working.
I have added the following import statement in the file ratingDAO.py
from core.rating import Rating
And I have also added the following import statement in the file generate_recommendations.py
from dao.ratingDAO import RatingDAO
When I execute ratingDAO.py, it runs without any error.
But when I try to execute generate_recommendations.py, I get the following error
Traceback (most recent call last):
File "generate_recommendations.py", line 3, in <module>
from dao.ratingDAO import RatingDAO
File "D:\MEGASync\BSc Computer\Research Papers\recommendation-engine\dao\ratingDAO.py", line 3, in <module>
from core.rating import Rating
ModuleNotFoundError: No module named 'core'
I am unable to resolve the error. I have seen approx ten posts on StackOverflow related to nested imports but I was unable to find examples where the author tried to import two levels deep.
If such imports are not possible in Python, I am open to ideas on how I should go about managing the files in my Python project.
In Java, I would've used the following folder structure,
├───recommendation
| | GenerateRecommendations.java
│ ├───core
| | Rating.java
│ └───dao
| RatingDAO.java
and used the following code to import Rating.java in RatingDAO.java,
import recommendation.core.Rating;
and used the following to code to import RatingDAO.java into GenerateRecommendations.java
import dao.RatingDAO;
and everything would have worked, but the same wasn't working for Python which is why I chose the initial specified folder structure.
P.S This is my first time asking a question on StackOverflow. I tried my best to describe my issue by referring to other posts. Apologies in advance if it does not match the standards of good questions.
Hoping for a reply! :-)
ratingDAO.py { contains a class named RatingDAO }=> I was already thinking hah! a Java programmer before you mentioned the language.