尝试导入嵌套模块时,Python中的ModuleNotFound错误 [英] ModuleNotFound error in Python when trying to import nested modules

查看:47
本文介绍了尝试导入嵌套模块时,Python中的ModuleNotFound错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面列出的基本文件夹结构进行Python项目开发,每个Python文件包含的示例都放在大括号中.

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

目标:我想在ratingDAO.py中导入rating.py,然后又想将ratingDAO.py导入generate_recommendations.py,并使所有导入工作.

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.

我已经在文件ratingDAO.py中添加了以下导入语句

I have added the following import statement in the file ratingDAO.py

from core.rating import Rating

我还在文件generate_recommendations.py

And I have also added the following import statement in the file generate_recommendations.py

from dao.ratingDAO import RatingDAO

当我执行ratingDAO.py时,它会运行而没有任何错误.

When I execute ratingDAO.py, it runs without any error.

但是当我尝试执行generate_recommendations.py时,出现以下错误

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'

我无法解决该错误.我在StackOverflow上看到了大约十个与嵌套导入有关的帖子,但是我找不到作者尝试深度导入两个级别的示例.

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.

如果在Python中无法进行此类导入,那么我对如何处理Python项目中的文件持开放态度.

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.

在Java中,我将使用以下文件夹结构,

In Java, I would've used the following folder structure,

├───recommendation
|   |   GenerateRecommendations.java
│   ├───core
|   |     Rating.java
│   └───dao
|         RatingDAO.java 

并使用以下代码将Rating.java导入RatingDAO.java中,

and used the following code to import Rating.java in RatingDAO.java,

import recommendation.core.Rating;

并使用以下代码将RatingDAO.java导入GenerateRecommendations.java

and used the following to code to import RatingDAO.java into GenerateRecommendations.java

import dao.RatingDAO;

一切都会正常运行,但对于Python而言却不起作用,这就是为什么我选择了初始指定的文件夹结构的原因.

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这是我第一次在StackOverflow上提问.我通过参考其他帖子尽力描述我的问题.如果不符合常见问题的标准,请提前道歉.

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.

希望得到答复!:-)

推荐答案

当您运行 python generate_recommendations.py 时,会将脚本目录放在路径( sys.path 导入时在其中搜索模块).当您从 ratingDAO.py 中的core.rating import Rating 中使用时,它将在路径中搜索名为 core 的软件包,但由于> dao 目录不在无法找到的路径上.

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.

一种解决方案是在 ratingDAO.py 模块中使用相对导入:

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

from .core.rating import Rating

这样,它将相对于其自身位置搜索 core 包.如果要从顶层目录运行 ratingDAO.py ,则可以通过 python -m dao.ratingDAO 来运行(这是路径上的当前工作目录,然后在 sys.path 中搜索名为 dao.ratingDAO 的模块并执行该模块.)

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

这篇关于尝试导入嵌套模块时,Python中的ModuleNotFound错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆