How to reaload module which has been imported by a string

Asked by HuyNguyen

Hi all,

In my testing environment, I want to load modules of test cases at runtime from a list defined in a setupEnvironment.py file
Here is the code in the main script to load modules:

import setupEnvironment #it is from setupEnvironment.py file
reload(setupEnvironment)

TestCaseList = setupEnvironment.TestCase_List #get list of test cases (each test case is defined in a sikuli module)
TestCaseDir = setupEnvironment.TestCase_Dir
ParentPath = getParentPath()

test_id = 0
for test_name in TestCaseList:
    testmodule_path = getParentPath() + "\\" + TestCaseDir[test_id][test_name] + ".sikuli" #get path to sikuli folder of test case
    testmodule_name = TestCaseDir[test_id][test_name]
    if testmodule_path not in sys.path:
        sys.path.append(testmodule_path)
    test = __import__(testmodule_name) #load sikuli module of the test case
   ### how to reload the the test case here ???? ########
    test.MainEntry() #execute the test case
    test_id += 1

I want to know which command I can use to make Sikuli reload the test module when it runs?
Since I may be making changes to the test modules and run the main script many times. Now each time, I need to restart the IDE to make it reloads.

Note: This case, I load the module by using a string defined in "testmodule_name" variable.
 I tried with "reload(testmodule_name)" but it reports that reload() requires module object, not string object

Thanks,
HuyK

Question information

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

I already told you with your other question about a similar case:

JUST PUT EVERY you-name-it.py file that you want to import (no matter what it is intended to be used for) into a folder named you-name-it.sikuli and then use

import you-name-it

with the latest nightly 1.1.1 there would be no need to use reload anymore and if the imported script folder is in the same folder as the importing script: no need to edit sys.path.

I really do not understand, why you refuse to accept this solution, that is tailored to what one needs with the SikuliX IDE.

to issue an import for a script name in a variable:
exec("import " + varOfModuleName)

Revision history for this message
HuyNguyen (huy-nguyen) said :
#2

Hi RaiMan

I will consider about updating the importing style I am using now. Since it still works now so I do not touch that part and focus on new function to develop.

You are saying about no need to reload other sikuli modules anymore in 1.1.1 right?
But for me, when I make changes to sikuli modules, the script that imports them does not see the changes until I re-start the IDE.
That's why I think about reloading sikuli modules.

Lets me double check if your saying is valid. I also use sikuli 1.1.1

Thanks,
HuyK

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

--- Lets me double check if your saying is valid
Just checked:

- open a main.sikuli in IDE
- open a sub.sikuli in IDE
- run main: get outcome from sub (whatever)
- change something in sub and save it
- run main: get CHANGED outcome from sub (whatever)

... so it works as I said.

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

I do not really understand your behavior:
since you are constantly changing something apparently, why not change to use such a basic concept, that is easier in usage.

... but of course it is your decision
... and this is my last comment on that.

Revision history for this message
HuyNguyen (huy-nguyen) said :
#5

-----Original Message-----
From: <email address hidden> [mailto:<email address hidden>] On Behalf Of RaiMan
Sent: Wednesday, July 27, 2016 5:38 PM
To: Nguyen Khac Huy (BRT-VN)
Subject: Re: [Question #304447]: How to reaload module which has been imported by a string

Your question #304447 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/304447

    Status: Open => Answered

RaiMan proposed the following answer:
--- Lets me double check if your saying is valid Just checked:

- open a main.sikuli in IDE
- open a sub.sikuli in IDE
- run main: get outcome from sub (whatever)
- change something in sub and save it
- run main: get CHANGED outcome from sub (whatever)

... so it works as I said.

--
If this answers your question, please go to the following page to let us know that it is solved:
https://answers.launchpad.net/sikuli/+question/304447/+confirm?answer_id=2

If you still need help, you can reply to this email or go to the following page to enter your feedback:
https://answers.launchpad.net/sikuli/+question/304447

You received this question notification because you asked the question.

Revision history for this message
HuyNguyen (huy-nguyen) said :
#6

Hi Raiman,

Thank you for your enthusiastic support.
I checked as your guidance, it works fine with sikuli modules meaning that it does not need to reload modules to see new changes
However, when I import setupEnvironment.py file (which locates in the same sikuli folder of the main script) in the main script, it needs to reload to see new changes in that file.
Does it need to reload with a python file and no need for reload with a sikuli module when importing?

Thanks,
HuyK

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

Exactly, that is my poit from beginning:
All these import automatics ONLY work for youNameIt.sikuli scripts.

So as already mentioned:
Just pack your setupEnvironment.py in a folder setupEnvironment..sikuli located in the same folder as the main script and all the others and you have the easiest solution.

Revision history for this message
HuyNguyen (huy-nguyen) said :
#8

Hi Raiman,

I understand very clear now :)

Thanks
HuyK