本文介绍了ModuleNotFoundError:没有名为"..."的模块[Python]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前只用Python编写过独立脚本.现在,我正在尝试编写一个可以在两个数据库之间转换和迁移数据的应用程序.但是,当我尝试创建不同的模块时,它们无法彼此查找".

I have only written stand alone script before in Python. Now I am trying to write an app which can transform and migrate data between two databases. But when I am trying to create the different modules, they can not "find" each other.

这就是我现在的工作空间.

This is what my workspace looks like right now.

  Project
     -PQF
         -db
             -__init__.py
             - DataSource.py
             - RecordSet.py
          -main
             -main.py
             - __init__.py
        - __init__.py

正如我理解的那样,我需要在每个模块中创建这些"init.py"文件,以使其了解这些是模块,这些模块当前为空.

As I understod it, I need to create these "init.py" files in each module to make it understand that these are modules, these are currently empty.

我要在主文件中执行的操作只是导入不同的模块.

What I am trying to do in the main file is to just import the different modules.

       from PQF.db import DataSource as database
       from PQF.db import RecordSet

       def main():
         print("hello")

但是我得到了错误: ModuleNotFoundError: No Module named "PQF"

But I get the error: ModuleNotFoundError: No Module named "PQF"

我做错了什么?感谢您的任何帮助.

What is it that I am doing wrong? Thank you for any help.

推荐答案

以下是完整的代码,包括__init__.py

Here's the full code including the __init__.py

文件-db/__init__.py

from .DataSource import *
from .RecordSet import *

文件-main/__init__.py

from .main import *

文件-PQF/__init__.py

from .db import *
from .main import *
from db import DataSource as database
from db import RecordSet

def main():
   print("hello")

python3 -m main.main

这篇关于ModuleNotFoundError:没有名为"..."的模块[Python]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 18:00