简体   繁体   中英

Importing multiple python files in subfolders

I want to import a python file Loader within a subfolder sub1 . This file that I want to import, imports another file detector within that same subfolder. However, Loader gives the following error:

ModuleNotFoundError: No module named 'detector'

I've tried using the exec command in Python and

import sub1.Loader

The folder structure looks like this:

Project
    | 
    +-- File_for_loading_Loader.py
    | 
    +-- sub1
         |  
         +-- __init__.py
         +-- Loader.py
         +-- detector.py

Can anyone help?

Edit

I now use:

    import subprocess
    subprocess.call(["python", "Loader.py"], cwd="sub1")

which does the trick nicely. No need for relative imports etc.

Since you created sub1 as module you have to import files from it always like sub1.<module_name> .

So for you it should be from sub1.detector import detect_faces in your Loader.

You need to import loader in File_for_loading_Loader.py by using import sub1.Loader as abc .

And in Loader.py you need to import detector by using import sub1.detector as xyz .

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