how to import a file in other file between middle of the code

Asked by manisha

Hello,
I want import one file in main file, but when i try to do that i got an error like,

[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)
 - HOW TO FIX? Try adding "from sikuli import *" in the module.

my code is:
#### 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))

#main operation
#startMstsc(1)

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

if ((scnName == "general") & (stpTest == "STP-GPSBL-002")): #checking the condition
    print "Done"
    import main
    reload(main)
    from main 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"

please solve my doubt, Here i want to call the function inside the if condition.
Thank you in advance :)

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

generally:
imports are static (only down once) and as such should be at the top of the script.
especially in this case, where you force a re-import (I guess since you are working in the Sikuli IDE on both modules in parallel).

so this is better in any case:

#######Main.sikuli
import csv
import main
reload(main)
from main import *

if ((scnName == "general") & (stpTest == "STP-GPSBL-002")): #checking the condition
    print "Done"
    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"

now you are doing an import(main), but I cannot see any main.sikuli.

if I understand your script right then this should be:
import MSTSC_SIM
reload(MSTSC_SIM)
from MSTSC_SIM import *

… or I understand everything completely wrong.

Revision history for this message
manisha (manishareddy1111) said :
#2

Apologies from my side, You are the right.
I have tried it but I got an error like below:

[error] Error message: Traceback (most recent call last):
 File "C:\DOCUME~1\kartheek.m\LOCALS~1\Temp\sikuli-tmp8900339240024860525.py", line 2, 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)
 - HOW TO FIX? Try adding "from sikuli import *" in the module.

and it is showing error on "import MSTSC_SIM"

my updated code is:

#######Main.sikuli
import csv
import MSTSC_SIM
reload(MSTSC_SIM)
from MSTSC_SIM import *

if ((scnName == "general") & (stpTest == "STP-GPSBL-002")): #checking the condition
    print "Done"
    startMstsc(1)
else:
    popup ("complete")

Revision history for this message
RaiMan (raimund-hocke) said :
#3

You are using the old Sikuli RC3 and seem to use the with keyword.

In the MSTSC_SIM add at the beginning
from future import with

Revision history for this message
manisha (manishareddy1111) said :
#4

In this case it is showing error like below,

ImportError: Errors in loading sikuli module: MSTSC_SIM
no viable alternative at input 'with' (MSTSC_SIM.py, line 2)
 - HOW TO FIX? Try adding "from sikuli import *" in the module.

Revision history for this message
RaiMan (raimund-hocke) said :
#5

LOL, sorry:

from __future__ import with_statement

Revision history for this message
manisha (manishareddy1111) said :
#6

Now i got error inside the function call,
i.e
[error] Error message: Traceback (most recent call last):
 File "C:\DOCUME~1\kartheek.m\LOCALS~1\Temp\sikuli-tmp2868521354483321816.py", line 19, in
 startMstsc(1)
 File "D:\Sikuli\test\MSTSC_SIM.sikuli\MSTSC_SIM.py", line 9, in startMstsc
 type('r',KeyModifier.WIN)
NameError: global name 'KeyModifier' is not defined

Revision history for this message
RaiMan (raimund-hocke) said :
#7

if you have
from sikuli import *
in your MSTSC_SIM.sikuli

then you might try with the "RC3" approach
from Sikuli.sikuli import *

If this does not help, then you have to check your complete Sikuli setup (mixing versions?)

Revision history for this message
manisha (manishareddy1111) said :
#8

In my script i have 'from __future__ import with_statement' instead of 'from sikuli import *'

Revision history for this message
RaiMan (raimund-hocke) said :
#9

from sikuli import *
is always required in imported modules containing Sikuli features.

I think, I did not tell you to not use it anymore !?

Revision history for this message
manisha (manishareddy1111) said :
#10

Ok sir, Now is there any alternative for my script. Please help me.

Revision history for this message
manisha (manishareddy1111) said :
#11

If you have interest I'll send you my scripts, Can you help me

Revision history for this message
RaiMan (raimund-hocke) said :
#12

what is the problem left?

You might send the stuff zipped together silently to my mail at https://launchpad.net/~raimund-hocke (top left)

Revision history for this message
manisha (manishareddy1111) said :
#13

Thank you sir, I sent you the Zip file

Revision history for this message
Best RaiMan (raimund-hocke) said :
#14

got it, but only to see, that you did not do what I suggested

in MSTSC_SIM.sikuli replace
#from sikuli import *

with
from sikuli import *
from __future__ import with_statement

Revision history for this message
manisha (manishareddy1111) said :
#15

Thanks RaiMan, that solved my question.

Revision history for this message
manisha (manishareddy1111) said :
#16

Thanks a lot.. you did fantastic job... :)