如何在代码中间的其他文件中导入文件

时间:2015-03-26 10:09:03

标签: python sikuli

我想在主文件中导入一个文件,但是当我尝试这样做时,我收到如下错误:

[error] Error message: Traceback (most recent call last):
File "C:\DOCUME~1\kartheek.m\LOCALS~1\Temp\sikuli-tmp3883511940798183465.py", line 17, in
import MSTSC_SIM
File "C:\Program Files\Sikuli X\sikuli-script.jar\Lib\sikuli\SikuliImporter.py", line 45, in load_module
File "C:\Program Files\Sikuli X\sikuli-script.jar\Lib\sikuli\SikuliImporter.py", line 29, in _load_module
ImportError: Errors in loading sikuli module: MSTSC_SIM
'with' will become a reserved keyword in Python 2.6 (MSTSC_SIM.py, line 62)

我该如何解决?我尝试在模块中添加from sikuli import *,但我无法解决问题。以下是我的代码:

#### MSTSC_SIM.sikuli
from sikuli import *
import shutil
import os
def startMstsc(event):
    type('r',KeyModifier.WIN)
    wait(1)
    paste("mstsc")
    type(Key.ENTER)
    doubleClick(Pattern("RemoteDeskto.png").targetOffset(-79,0))
    type(Key.DELETE)
    type("simulatorpc1")
    click(Pattern("Qptions.png").targetOffset(-26,9))
    click(Pattern("GeneralDispl.png").targetOffset(-22,3))
    click(Pattern("UISDIGQCClll.png").targetOffset(144,16))
    type(Key.ENTER)
    alertImg = "Dol.png"
    if exists(alertImg):
        type(Key.LEFT)
        type(Key.ENTER)
        wait(5)
    else:
        wait(7)
    doubleClick(Pattern("UsernamePass.png").targetOffset(49,-5))
    type(Key.DELETE)
    type("simulatoruser")
    click(Pattern("IJSBYIIE1ITI.png").targetOffset(52,8))
    type("accord_123")
    type(Key.ENTER)
    wait(3)
    click(Pattern("1425987295129.png").targetOffset(-1,29))
    wait(3)
    click(Pattern("E72startYist.png").targetOffset(1,-23))
    click(Pattern("QelpandSuppo.png").targetOffset(-40,26))
    wait(2)
    type("C:\\Program Files\\Spirent Communications\\SimGEN\\SimGEN.EXE")
    wait(1)
    type(Key.ENTER)
    wait(2)
    if not exists("1426049129321.png"):
        wait(1)
    else:
        click(Pattern("ForWindowsap.png").targetOffset(-4,30))
        click(Pattern("1425990081814.png").targetOffset(-1,-22))
        wait(1)
        click(Pattern("EEIE.png").targetOffset(27,-14))
        click(Pattern("1425987295129.png").targetOffset(-1,29))
        wait(1)
        click(Pattern("E72startYist.png").targetOffset(1,-23))
        click(Pattern("QelpandSuppo.png").targetOffset(-40,26))
        wait(1)
        type("C:\\Program Files\\Spirent Communications\\SimGEN\\SimGEN.EXE")
        type(Key.ENTER)
        wait(2)
    click(Pattern("1425988278814.png").targetOffset(6,1)) 
    doubleClick(Pattern("I5AircraFt_E.png").targetOffset(-62,-85))
    doubleClick(Pattern("1425989177967.png").targetOffset(-15,2))
    click(Pattern("1425990081814.png").targetOffset(-1,-22))
    reg_Start = Region(Region(0,15,413,76)).inside()
    with reg_Start:
        if not exists("lQnmyi.png"):
            click(Pattern("ToolsWindowH.png").targetOffset(18,12)) # start the scenario
        else:
            click(Pattern("ToolsWindowH-start.png").targetOffset(18,12))
        wait(2)
        click(Pattern("IA414114ENoh.png").targetOffset(-3,42))
        wait(2)
        dir = os.path.dirname(getBundlePath()) # the folder, where your script is stored
        img = capture(SCREEN) # snapshots the screen
        shutil.move(img, os.path.join(dir, "running.png")) # to make it persistent
         wait(1)
        click(Pattern("yil.png").targetOffset(73,6))
        click(Pattern("EEndofsimula.png").targetOffset(154,114))
        wait(1)
        dir = os.path.dirname(getBundlePath()) # the folder, where your script is stored
        img = capture(SCREEN) # snapshots the screen
        shutil.move(img, os.path.join(dir, "stop.png")) # to make it persistent
         wait(2)

#main operation
#startMstsc(1)

#######Main.sikuli
import csv

if ((scnName == "general") & (stpTest == "STP-GPSBL-002")): #checking the condition
    print "Done"
    import MSTSC_SIM
    reload(MSTSC_SIM)
    from MSTSC_SIM import *
    startMstsc(1)
    try:
        startApp(1)
        onAppear("7Connected.png", handler)
        observe(FOREVER)
        startTest(1)
        stopMstsc(1)

    except FindFailed:
        popup("image not found")
        print "img not found on the screen"

我很感激任何帮助来解决我的疑问。我想在if条件中调用该函数。

1 个答案:

答案 0 :(得分:0)

导入MSTSC_SIM - 显示此模块。看起来有一些保留字“with”,导致了这个错误:

ImportError: Errors in loading sikuli module: MSTSC_SIM
'with' will become a reserved keyword in Python 2.6 (MSTSC_SIM.py, line 62)

另外,请告诉我们,您使用的是哪个版本的python?

但是,请务必尝试运行:

with open('testfile.txt', 'w') as f:
    f.write('this is just a test')