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

Python跨脚本导入带**kwargs函数报错,如何获取函数内变量?

Let's tackle your issues one by one, step by step.

First: Fixing the ImportError

The error ImportError: cannot import name 'function1' from 'file1' (script.py) gives a clear clue here—your actual script file is named script.py, not file1.py. Python uses the file name (without the .py extension) as the module name, so you need to adjust your import statement to match:

from script import function1  # Not from file1 import function1

If you intended the file to be named file1.py, double-check that you’ve renamed it correctly and that it’s located in the same directory as your calling script (or in a directory included in Python’s sys.path).

Another possible cause: Make sure function1 is defined at the top level of script.py—not nested inside another function or class, which would make it inaccessible for direct import.

Second: Accessing variable_name from function1

Right now, variable_name is a local variable inside function1, meaning it only exists while the function runs and can’t be accessed from outside by default. Here are the cleanest solutions to get that value:

Option 1: Return variable_name from the function (Recommended)

Modify function1 to return the value you need. If you need multiple values, return a dictionary or tuple for clarity:

# In script.py
def function1(**kwargs):
    name = kwargs['customer_name']
    date = kwargs['sale_date']
    # Fix: Define xyz first (your original code has an undefined xyz here!)
    xyz = "calculated_value"  # Replace with your actual logic/value for xyz
    variable_name = xyz
    
    # Return just variable_name, or a collection of values
    return variable_name

# OR, if you need multiple values:
def function1(**kwargs):
    name = kwargs['customer_name']
    date = kwargs['sale_date']
    xyz = "calculated_value"
    variable_name = xyz
    
    return {
        "customer_name": name,
        "sale_date": date,
        "variable_name": variable_name
    }

Then in your calling script:

from script import function1

# For single value return
var = function1(customer_name="Alice", sale_date="2024-05-20")
print(var)  # This is variable_name

# For dictionary return
result = function1(customer_name="Bob", sale_date="2024-05-21")
print(result["variable_name"])

Option 2: Use a global variable (Not Recommended)

If you absolutely must access variable_name without returning it (though this is bad practice for modular, maintainable code), you can declare it as a global variable inside the function:

# In script.py
variable_name = None  # Initialize at module level

def function1(**kwargs):
    global variable_name  # Tell Python to use the global variable
    name = kwargs['customer_name']
    date = kwargs['sale_date']
    xyz = "calculated_value"
    variable_name = xyz

Then in your calling script:

from script import function1, variable_name

function1(customer_name="Charlie", sale_date="2024-05-22")
print(variable_name)  # Now holds the value set by function1

⚠️ Note: Global variables make your code harder to debug and reuse, since the function’s state depends on external variables. Stick to returning values whenever possible.

Bonus: Fix the undefined xyz error

In your original function1 code, variable_name = xyz will throw a NameError when you call the function because xyz isn’t defined. Make sure you assign a value to xyz inside the function (like a calculation based on name/date, or a static value) before using it.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.07 10:12:49