简体   繁体   English

从另一个python脚本执行python文件,如果python文件有嵌套函数则失败

[英]Execute python file from another python script, failed if python file has nested function

I'm creating a python script to do I2C read and write.我正在创建一个 python 脚本来进行 I2C 读写。 The script also handles test scripts (also in python) to send hardware.该脚本还处理测试脚本(也在 python 中)以发送硬件。 The test scripts previously are sent through GUI, but my script by-passes GUI usage and talks to I2C driver directly.之前的测试脚本是通过 GUI 发送的,但我的脚本绕过了 GUI 使用并直接与 I2C 驱动程序对话。 If the test scripts are flat, no problem.如果测试脚本是扁平的,没问题。 However, if there's nested function or global variable, there's error:但是,如果有嵌套函数或全局变量,则会出现错误:

def main():
    board=usb2any()
    board.ReadI2C(0x58,0x0)
    board.WriteI2C(0x58,0xBC,0x40)
    execfile(f1)
if __name__ == "__main__":
    main()

The test code "f1", if it only contains codes like below, it runs fine测试代码“f1”,如果它只包含如下代码,它运行良好

board.ReadI2C(0x58,0x2)
board.WriteI2C(0x58,0x20,0x10)

however if it looks like this: test script example 1但是,如果它看起来像这样:测试脚本示例 1

V1=0
def fun1():
    if V1==1: # error here saying global variable V1 not defined

Another example of failed case: test script example 2失败案例的另一个例子:测试脚本示例2

ff(0x10,0x9)
def ff(v1,v2):
    fun2(v1)   #Error here saying fun2 not defined

def fun2(vv): vv=0 def fun2(vv): vv=0

Any idea how to solve this?知道如何解决这个问题吗? prefer not touching test scripts because those have been used with GUI.不喜欢接触测试脚本,因为它们已与 GUI 一起使用。

Example 2 doesn't work because you are calling the function before declaring it.示例 2 不起作用,因为您在声明之前调用了该函数。 Try this instead:试试这个:

def ff(v1,v2): 
    fun2(v1) 
ff(0x10,0x9)

Example 2 needs some rearranging so you define the functions before calling them.示例 2 需要一些重新排列,以便您在调用函数之前定义它们。 You might want to make a class if you're calling lots of functions from each other.如果您要相互调用大量函数,您可能想要创建一个类。

def fun2(vv): 
    vv=0

def ff(v1,v2): 
    fun2(v1)

` `

Here's what's working and what's not: For test script example 1, need to add global in test script global V1 V1=0 def fun1(): if V1==1: #now there's no error这是有效的和无效的:对于测试脚本示例 1,需要在测试脚本中添加 global V1 V1=0 def fun1(): if V1==1: #now theres no error

Another example of failed case: test script example 2, which shifting the sequence, still failed失败案例的另一个例子:测试脚本示例2,转移序列,仍然失败

def fun2(x):
    x = 0

def ff(vv):
    fun2(vv)
ff(2) #when calling ff, error saying no fun2 defined

Printout info:打印信息:

Traceback (most recent call last):回溯(最近一次调用最后一次):

File "c:\\Users\\a0272122\\Documents\\Python Scripts\\I2C_USB2Any\\i2c_wr.py", line 68, in main()文件“c:\\Users\\a0272122\\Documents\\Python Scripts\\I2C_USB2Any\\i2c_wr.py”,第 68 行,在 main() 中

File "c:\\Users\\a0272122\\Documents\\Python Scripts\\I2C_USB2Any\\i2c_wr.py", line 42, in main execfile(file_name)文件“c:\\Users\\a0272122\\Documents\\Python Scripts\\I2C_USB2Any\\i2c_wr.py”,第 42 行,在主 execfile(file_name) 中

File "pattern_test.py", line 122, in apb_read_modifywrite (0x1A4, 0x000000E0, 0x00000020)文件“pattern_test.py”,第 122 行,在apb_read_modifywrite (0x1A4, 0x000000E0, 0x00000020)

File "pattern_test.py", line 113, in apb_read_modifywrite read_data = apb_read_reg (addr16b)文件“pattern_test.py”,第 113 行,在 apb_read_modifywrite read_data = apb_read_reg (addr16b)

NameError: global name 'apb_read_reg' is not defined NameError: 全局名称 'apb_read_reg' 未定义

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM