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

Python:Web应用中实现导入库/模块后打印自定义消息

Hey there! Great question—adding custom prompt messages whenever users import any Python library/module in your web app is totally feasible, and I’ll walk you through two solid approaches that work for dynamic user code.

Approach 1: Static Code Modification with AST (Abstract Syntax Tree)

This method parses the user’s code without executing it, finds all import statements, and injects your custom prompt right before or after each import. It’s safe because you’re modifying the code text before running it.

How to implement it:

  • Use Python’s built-in ast module to parse the user’s code into an abstract syntax tree.
  • Traverse the tree to locate all Import and ImportFrom nodes.
  • Insert a print statement (or your custom prompt logic) either before or after each import node.
  • Convert the modified AST back to executable code.

Example code snippet:

import ast
import astunparse

def add_import_prompts(user_code, prompt_text="⚠️ Custom message: You're importing a library!"):
    tree = ast.parse(user_code)
    
    # Collect nodes to insert (avoids issues from modifying the tree mid-traversal)
    insertions = []
    
    for idx, node in enumerate(tree.body):
        if isinstance(node, (ast.Import, ast.ImportFrom)):
            # Create a print statement node for the prompt
            prompt_node = ast.Expr(
                value=ast.Call(
                    func=ast.Name(id='print', ctx=ast.Load()),
                    args=[ast.Constant(value=prompt_text)],
                    keywords=[]
                )
            )
            # Insert prompt before the import
            insertions.append((idx, prompt_node))
    
    # Insert in reverse order to prevent index shifting
    for idx, node in reversed(insertions):
        tree.body.insert(idx, node)
    
    # Convert AST back to code text
    modified_code = astunparse.unparse(tree)
    return modified_code

# Test with sample user code
user_code = """
import numpy as np
from pandas import DataFrame
print("Hello World")
"""

modified_code = add_import_prompts(user_code)
print(modified_code)

When executed, the modified code will print your custom prompt before every import statement.

Approach 2: Runtime Import Interception with Custom Hooks

If you want to trigger the prompt while the import happens (instead of altering the user’s code directly), you can use Python’s import system hooks. This works for any import—even those nested inside functions or conditionals.

How to implement it:

  • Create a custom finder class that implements the find_spec method.
  • Register this finder in sys.meta_path so Python uses it when resolving modules.
  • In find_spec, trigger your custom prompt before letting the normal import process continue.

Example code snippet:

import sys
from importlib.abc import MetaPathFinder
from importlib.util import spec_from_file_location

class ImportPromptFinder(MetaPathFinder):
    def __init__(self, prompt_text="⚠️ Custom message: Library imported!"):
        self.prompt_text = prompt_text
    
    def find_spec(self, fullname, path, target=None):
        # Trigger prompt for every import
        print(f"{self.prompt_text} Module: {fullname}")
        
        # Let Python's default import system handle the rest
        for finder in sys.meta_path:
            if finder is self:
                continue
            spec = finder.find_spec(fullname, path, target)
            if spec is not None:
                return spec
        return None

# Register the finder before running user code
sys.meta_path.insert(0, ImportPromptFinder())

# Execute user code
user_code = """
import numpy as np
from pandas import DataFrame
print("Hello World")
"""

exec(user_code)

This will print your prompt (with the imported module name) every time a module is loaded, no matter where the import lives in the user’s code.

Critical Notes for Your Web App

  • Security is non-negotiable: Executing arbitrary user code carries risks. Always run code in a sandboxed environment—consider tools like executing (a safer alternative to raw exec), PyPy’s sandbox, or isolated Docker containers with limited permissions.
  • Dynamic prompts: You can tailor prompts to the module being imported (like the example above that includes fullname), making messages more relevant.
  • Performance: AST parsing is fast for most code snippets, but the import hook method is more efficient for large codebases since it only triggers on actual imports.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.19 10:18:54