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

如何将主脚本变量传入被其调用的副脚本函数?解决导入异常

解决主副脚本间变量传递的循环导入与延迟问题

你的问题核心在于循环导入和全局变量引用失效,咱们一步步拆解问题,然后给出最优解决方案:

问题根源分析

  1. 循环导入 + 重复模块加载:你在script1.py的while True循环里每次都执行from script2 import primitives1, main,这会导致script2模块被反复加载,直接引发延迟;同时script2里又反向导入script1的变量,形成循环依赖,首次执行时模块加载顺序混乱,就会出现"未生效"的异常。
  2. 全局变量的副本问题:from script1 import variable_of_interest导入的是变量的副本值,不是引用。当script1里的variable_of_interest自增后,script2里的变量并不会同步更新,这也是你看不到最新值的原因。

最优解决方案:参数传递(显式、无耦合)

这是Python中变量传递的最佳实践,完全避免循环导入,代码逻辑清晰。

修改后的script1.py

import time
# 只导入一次模块,不要放在循环里!
from script2 import primitives1, main

variable_of_interest = 1
while True:
    # 把变量作为参数传递给main,再由main传给primitives1
    main(primitives1, variable_of_interest)
    time.sleep(0.01)
    print("test print")
    variable_of_interest += 1

修改后的script2.py

# 假设你已经正确导入了ssd1351和canvas相关模块
device = ssd1351(serial)

# 新增参数接收主脚本传来的变量
def primitives1(device, draw, variable):
    # 去掉循环导入的代码
    padding = 2
    shape_width = 20
    top = padding
    bottom = device.height - padding - 1
    
    # 直接使用传入的variable参数
    size = draw.textsize('123456789101112131415')
    x = device.width - padding - size[0]
    draw.rectangle((x, top + 4, x + size[0], top + size[1]), fill="black")
    draw.rectangle((x, top + 2, x + size[0], top + 16 + size[1]), fill="black")
    draw.text((device.width - padding - size[0], top + 44), f'My variable is: {variable} ', fill="white")
    
    time.sleep(0.01)

# 修改main函数,新增参数接收变量,再传给prim_func
def main(prim_func, variable):
    print("Testing basic canvas graphics...")
    for _ in range(2):
        with canvas(device) as draw:
            prim_func(device, draw, variable)
        print("Drawing on display")
        time.sleep(0.01)

其他可选方案:共享配置模块

如果你的项目里有多个脚本需要共享变量,可以创建一个独立的配置模块,利用Python模块的单例特性实现变量同步:

  1. 创建config.py:
variable_of_interest = 1
  1. 修改script1.py:
import time
import config
from script2 import primitives1, main

while True:
    main(primitives1)
    time.sleep(0.01)
    print("test print")
    config.variable_of_interest += 1
  1. 修改script2.py:
import config
device = ssd1351(serial)

def primitives1(device, draw):
    # 直接引用config模块的变量(是引用,不是副本)
    variable = config.variable_of_interest
    # 后续逻辑和之前一致,替换成variable即可
    padding = 2
    shape_width = 20
    top = padding
    bottom = device.height - padding - 1
    
    size = draw.textsize('123456789101112131415')
    x = device.width - padding - size[0]
    draw.rectangle((x, top + 4, x + size[0], top + size[1]), fill="black")
    draw.rectangle((x, top + 2, x + size[0], top + 16 + size[1]), fill="black")
    draw.text((device.width - padding - size[0], top + 44), f'My variable is: {variable} ', fill="white")
    
    time.sleep(0.01)

def main(prim_func):
    print("Testing basic canvas graphics...")
    for _ in range(2):
        with canvas(device) as draw:
            prim_func(device, draw)
        print("Drawing on display")
        time.sleep(0.01)

总结

  • 优先选择参数传递:代码耦合度低,逻辑清晰,完全避免循环导入问题,是最符合Python编程规范的方式。
  • 共享配置模块适合多脚本共享变量:但要注意全局变量的修改可能带来的副作用,需谨慎使用。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:40:14