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

xlwings:如何将现有UDF导入至其他工作簿?

Fixing ModuleNotFoundError When Importing xlwings UDFs to Another Workbook

Ah, I see the issue here—you're mixing up what the "UDF Modules" field in the xlwings ribbon expects! Let's break this down and fix it step by step:

Why the Error Happens

The ModuleNotFoundError pops up because you're entering the full file path (C:\Test\myproject\myproject.py) into the UDF Modules field, but xlwings doesn't work that way. It expects a module name (not a path), and it needs the folder containing your script to be in Python's search path so it can locate the module.

Step-by-Step Fix

  1. Identify your module's root folder
    Your script myproject.py lives in C:\Test\myproject—this folder is the one we need Python to recognize as a valid location for modules.

  2. Add the folder to Python's search path
    You have two options here:

    • Temporary (per-workbook) fix:
      Open the xlwings ribbon in your target workbook, click "Settings", then look for the "Extra Paths" section. Add C:\Test\myproject here and save the settings.
    • Permanent (system-wide) fix:
      Add C:\Test\myproject to your system's PYTHONPATH environment variable. This way, any Python environment (including xlwings) will be able to find your module without extra per-workbook setup.
  3. Update the UDF Modules field
    Go back to the "UDF Modules" field in the xlwings ribbon, and replace the full path with just myproject (that's your script name without the .py extension).

  4. Import the UDFs
    Click the "Import UDFs" button in the xlwings ribbon—your functions should now load without errors!

Extra Checks

  • Make sure myproject.py has no syntax errors—even a tiny typo can block UDF loading.
  • If you're using a virtual environment, double-check that xlwings is configured to use the correct Python interpreter (you can set this in the xlwings Settings under "Python Interpreter").

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 09:10:50