ModuleNotFound error in Python when trying to import nested modules

Ranajoy Saha :

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! :-)

a_guest :

When you run python generate_recommendations.py this puts the script's directory on the path (sys.path which is searched for modules when importing). When you use from core.rating import Rating in ratingDAO.py then it will search the path for a package called core but since the dao directory is not on the path it cannot be found.

A solution is to use a relative import in the ratingDAO.py module:

from .core.rating import Rating

This way it will search relative to its own location for the core package. If you want to run ratingDAO.py from the top-level directory you can do so via python -m dao.ratingDAO (this put's the current working directory on the path and then searches sys.path for a module called dao.ratingDAO and executes it).

Or you can use an absolute import relative to the top-level directory of the hierarchy:

from dao.core.rating import Rating

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=360313&siteId=1