After import a module, how to use the function defined in that module?

Asked by lynn

first if I define the file named MyFunctions which has below function put under /home/lynn folder,
def Add(x,y):
    return x+y

, then also in /home/lynn folder there is a test as below
import MyFunctions
print Add(5,6)

it should be added with MyFunctions.Add(5,6) or just Add(5,6)?
I found sometimes both of these two ways works but sometimes neither of them work.

Question information

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

I met the problem is sometimes when I use
 Add(5,6) in test file, system prompt error message to say "Add is not define"

Then I add MyFunctionsAdd(5,6), but it prompts error message to say that Module object has no attribute "Add"

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

Are you talking about SikuliX?
if yes what version? Using IDE?

looks like some Linux?

Pls. show exactly how you have setup your folders and script files.

Revision history for this message
lynn (lynn-wu) said :
#3

hi,I am using Sikuli IDE_v1.0.1 on linux remote host.

the folder is /home/lynn/MyFunctions.sikuli and /home/lynn/test.sikuli

in MyFunctions.sikuli, there is below function
def Add(x,y):
    return x+y

and in test.sikuli, there is as below
import MyFunctions
print Add(5,6)

first time it is working. Then someday when I re-ran the script, it is say "Add is not defined". Then I try to add as
print MyFunctions.Add(5,6), then it is working.
But again in another day when I still tried the script it says "Module object has no attribute 'Add'

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

according to the Python import rules you either use:

import MyFunctions
print MyFunctions.Add(5,6)

or

from MyFunctions import *
print Add(5,6)

the 2nd version has the risk to silently overwrite an already existing name Add in the global namespace.

Not related to your problem:
When working with Sikuli scripting in Python/Jython you should always put
from sikuli import *
at the beginning of submodules (just to be prepared)

About your intermittent problem situations I cannot tell you anything.

Revision history for this message
lynn (lynn-wu) said :
#5

Thanks you, I will try more.