You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python导入detect_simple.py失败:报错Import by filename is not supported

Fixing the "Import by filename is not supported" Error

Hey there! Let's sort out that import problem you're facing with detect_simple.py. The error you're seeing happens because __import__() doesn't accept file paths (like Face_recognition/detect_simple) as input—it expects valid Python module names instead. Here's how to fix it step by step:

1. Check Your Directory Structure

First, make sure your project is structured correctly so Python recognizes Face_recognition as a valid package. You'll need an empty __init__.py file inside the Face_recognition folder (this tells Python it's an importable package):

your_project_folder/
├── app.py
└── Face_recognition/
    ├── __init__.py
    └── detect_simple.py

2. Use Valid Import Syntax

Replace your problematic __import__("Face_recognition/detect_simple") line with one of these valid import statements:

Option 1: Import the module directly

from Face_recognition import detect_simple

Option 2: Import with an alias (for shorter references)

import Face_recognition.detect_simple as detect

3. If the Folder Isn't in Python's Path (Optional)

If Python still can't locate the Face_recognition folder, you can manually add its path to sys.path at the top of app.py:

import sys
from pathlib import Path

# Add the Face_recognition folder to Python's search path
sys.path.append(str(Path(__file__).parent / "Face_recognition"))

# Now you can import directly
import detect_simple

Modified Working Code

Here's your updated app.py with the correct import logic:

from Face_recognition import detect_simple
from flask import Flask

app = Flask(__name__)

@app.route("/")
def main():
    return "OK"

@app.route('/api1')
def api1():
    return "OK"

if __name__ == "__main__":
    app.run()

That should resolve the import error and let your Flask app run smoothly!

内容的提问来源于stack exchange,提问作者Nguyen Nguyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 07:39:25