简体   繁体   中英

ModuleNotFoundError: No Module Named '…' [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

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

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

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

Here's the full code including the __init__.py

File - db/__init__.py

from .DataSource import *
from .RecordSet import * 

File - main/__init__.py

from .main import *

File - PQF/__init__.py

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

def main():
   print("hello")

Run the main.py script as python3 -m main.main

What you need to do is put main.py hand over your other scripts.

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

In your main.py , you now make your imports

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM