10.2: ImportError with other script in same folder --- sys.path has to contain each script folder, that you want to import

Asked by Fabian

Hi,

I read several thread from the forum, as well as Sikili's guide and no way to import a Sikuli module.
I'm on WinXP.
All files are in C:\Program Files\Sikuli

main.sikuli :
import VPsLogin
type("m", KEY_WIN)
doubleClick( imgpath)
VPsLogin.sub1()
....

VPsLogin.sikuli :
from sikuli import *
def sub1(self):
....

I got the error message :
"[sikuli] Error message:
Traceback (most recent call last):
  File "C:\DOCUME~1\LOCALS~1\Temp\sikuli-tmp4714538326415647707.py", line 1, in <module>
    import VPsLogin
ImportError: No module named VPsLogin"

If someone has an idea, I try a lots of things from the forum and it is always the same result.

Fabian

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Fabian
Solved:
Last query:
Last reply:

This question was reopened

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

might be, that this is because C:\Program Files\ needs special access rights and it is not writeable for everyone.

When importing a some_name.py file from some_name.sikuli in Jython (means Sikuli IDE), Jython writes a some_name$py.class file to that directory. this write operation might make problems deep down inside and comes to the surface as "No module ..." error.

So I suggest to put your files in a directory, that you have full access to (e.g. C:\Sikuli_Modules\ ) and put your .sikuli stuff there.

Revision history for this message
Fabian (fabian-ledoeuff) said :
#2

I moved my two Sikuli's scripts in C:\NonReg to avoid this kind of issue but it's not working.
I get the same message :
"[sikuli] Error message:
Traceback (most recent call last):
  File "C:\DOCUME~1\LOCALS~1\Temp\sikuli-tmp5123543583718084860.py", line 1, in <module>
  import VPsLogin
ImportError: No module named VPsLogin"

I tested also with 'from VPsLogin import *' and 'import VPsLogin' but always the same error

Revision history for this message
Fabian (fabian-ledoeuff) said :
#3

I've added at the top main.sikuli these lines :
myScriptPath="C:\\NonReg\\VPsLogin.sikuli"
if not myScriptPath in sys.path: sys.path.append(myScriptPath)

And now it works fine !

Thanks for your help RainMan

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

---1. If the main script and the imported scripts (all as .sikuli) are in the same folder and running the main script, the import should find the stuff to import.

So if you have:
# c:\NonReg\main.sikuli
import sub

and
# c:\NonReg\sub.sikuli
from sikuli import *

running main in Sikuli IDE should give no error

---2. sys.path
In sys.path you need only the folder that contains the .sikuli scripts and not the scripts itself.

your case:
myScriptPath=r"C:\NonReg" # raw string r"no double \ needed"
if not myScriptPath in sys.path: sys.path.append(myScriptPath)

Revision history for this message
Fabian (fabian-ledoeuff) said :
#5

In my case it doesn't work.
I tested :
# c:\NonReg\main.sikuli
myScriptPath=r"C:\NonReg"
if not myScriptPath in sys.path: sys.path.append(myScriptPath)
import sub

# c:\NonReg\sub.sikuli
from sikuli import *

and I have always the same issue "ImportError: No module named VPsLogin"

It's working only if I have :
# c:\NonReg\main.sikuli
myScriptPath=r"C:\NonReg\sub.sikuli"
if not myScriptPath in sys.path: sys.path.append(myScriptPath)
import sub

# c:\NonReg\sub.sikuli
from sikuli.Sikuli import *

That's really weird but if I'm able to work in this way, it doesn't matter for me

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

Please, do me a favor and run the following test script after saving it to the c:\NonReg folder as main.sikuli:

print "***** before import"
for e in sys.path: print e
okImport = False

try:
    import subNotExists
    okImport = True
except:
    print "***** after import not ok"
    for e in sys.path: print e

if okImport:
    print "***** after import ok"
    for e in sys.path: print e

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

... and paste the content of the message area here, please.

Revision history for this message
Fabian (fabian-ledoeuff) said :
#8

Here are the content of the message area :

"***** before import
C:\Program Files\Sikuli\sikuli-script.jar
C:\Program Files\Sikuli\Lib
C:\Program Files\Sikuli\sikuli-script.jar\Lib
__classpath__
__pyclasspath__/
C:\NonReg\VPsLogin.sikuli
***** after import not ok
C:\Program Files\Sikuli\sikuli-script.jar
C:\Program Files\Sikuli\Lib
C:\Program Files\Sikuli\sikuli-script.jar\Lib
__classpath__
__pyclasspath__/
C:\NonReg\VPsLogin.sikuli"

Revision history for this message
Fabian (fabian-ledoeuff) said :
#9

I moved sikuli-script.jar in C:\NonReg and also trying to modify Environment Variables but I have always this outcome :
ScreenMatchProxy loaded.
VDictProxy loaded.
Win32Util loaded.
ScreenMatchProxy loaded.
***** before import
C:\Program Files\Sikuli\sikuli-script.jar
C:\Program Files\Sikuli\Lib
C:\Program Files\Sikuli\sikuli-script.jar\Lib
__classpath__
__pyclasspath__/
***** after import not ok
C:\Program Files\Sikuli\sikuli-script.jar
C:\Program Files\Sikuli\Lib
C:\Program Files\Sikuli\sikuli-script.jar\Lib
__classpath__
__pyclasspath__/

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

I just realized, that I never asked:
What version of Sikuli are you using?

Does not seem to be the latest version or even rc3?

Revision history for this message
Fabian (fabian-ledoeuff) said :
#11

I'm using 0.10.2

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

Uuuups, I already doubted that ;-)

10.2 does not have the import support for .sikuli scripts, that is only available in X-1.0rc3.
In 10.2 you have to do it as you found out already.

Any reason for using 10.2 instead of X?

X is much faster and has more features.

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

One more thing:

10.2 import does not make available the images, that are captured in the imported scripts.

All images have either be referenced with absolute path or be in the main script (where bundlePath points to)

Revision history for this message
Fabian (fabian-ledoeuff) said :
#14

OK RaiMan.
So I install Sikuli X rc3 (https://launchpad.net/sikuli/+milestone/x1.0-rc3)
So now, my first test with main and sub in C:\Program Files\Sikuli
#main.sikuli:
print "***** before import"
for e in sys.path: print e
okImport = False

try:
    import subNotExists
    okImport = True
except:
    print "***** after import not ok"
    for e in sys.path: print e

if okImport:
    print "***** after import ok"
    for e in sys.path: print e

#sub.sikuli
from sikuli import *

In message area, I got :
"
[error] Stopped
[error] An error occurs at line 1
[error] Error message: Traceback (most recent call last):
 File "", line 1, in
 File "C:\Program Files\Sikuli\sikuli-script.jar\Lib\sikuli\__init__.py", line 3, in
 File "C:\Program Files\Sikuli\sikuli-script.jar\Lib\sikuli\Sikuli.py", line 26, in
java.lang.NoClassDefFoundError: Could not initialize class org.sikuli.script.App
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at org.python.core.Py.loadAndInitClass(Py.java:895)
at org.python.core.Py.findClassInternal(Py.java:830)
at org.python.core.Py.findClassEx(Py.java:881)
at org.python.core.packagecache.SysPackageManager.findClass(SysPackageManager.java:133)
at org.python.core.packagecache.PackageManager.findClass(PackageManager.java:28)
at org.python.core.packagecache.SysPackageManager.findClass(SysPackageManager.java:122)
at org.python.core.PyJavaPackage.__findattr_ex__(PyJavaPackage.java:137)
at

org.python.core.PyObject.__findattr__(PyObject.java:863)
at org.python.core.imp.import_name(imp.java:849)
at org.python.core.imp.importName(imp.java:884)
at org.python.core.ImportFunction.__call__(__builtin__.java:1220)
at org.python.core.PyObject.__call__(PyObject.java:357)
at org.python.core.__builtin__.__import__(__builtin__.java:1173)
at org.python.core.imp.importFromAs(imp.java:978)
at org.python.core.imp.importFrom(imp.java:954)
at sikuli.Sikuli$py.f$0(C:\Program Files\Sikuli\sikuli-script.jar\Lib\sikuli\Sikuli.py:211)
at sikuli.Sikuli$py.call_function(C:\Program Files\Sikuli\sikuli-script.jar\Lib\sikuli\Sikuli.py)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.imp.createFromCode(imp.java:386)
at org.python.core.util.importer.importer_load_module(importer.java:109)
at org.python.modules.zipimport.zipimporter.zipimporter_load_module(zipimporter.java:161)
at org.python.modules.zipimpor

t.zipimporter$zipimporter_load_module_exposer.__call__(Unknown Source)
at org.python.core.PyBuiltinMethodNarrow.__call__(PyBuiltinMethodNarrow.java:47)
at org.python.core.imp.loadFromLoader(imp.java:513)
at org.python.core.imp.find_module(imp.java:467)
at org.python.core.PyModule.impAttr(PyModule.java:100)
at org.python.core.imp.import_next(imp.java:715)
at org.python.core.imp.import_name(imp.java:824)
at org.python.core.imp.importName(imp.java:884)
at org.python.core.ImportFunction.__call__(__builtin__.java:1220)
at org.python.core.PyObject.__call__(PyObject.java:357)
at org.python.core.__builtin__.__import__(__builtin__.java:1173)
at org.python.core.imp.importAll(imp.java:998)
at sikuli$py.f$0(C:\Program Files\Sikuli\sikuli-script.jar\Lib\sikuli\__init__.py:3)
at sikuli$py.call_function(C:\Program Files\Sikuli\sikuli-script.jar\Lib\sikuli\__init__.py)
at org.python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at or

g.python.core.imp.createFromCode(imp.java:386)
at org.python.core.util.importer.importer_load_module(importer.java:109)
at org.python.modules.zipimport.zipimporter.zipimporter_load_module(zipimporter.java:161)
at org.python.modules.zipimport.zipimporter$zipimporter_load_module_exposer.__call__(Unknown Source)
at org.python.core.PyBuiltinMethodNarrow.__call__(PyBuiltinMethodNarrow.java:47)
at org.python.core.imp.loadFromLoader(imp.java:513)
at org.python.core.imp.find_module(imp.java:467)
at org.python.core.imp.import_next(imp.java:713)
at org.python.core.imp.import_name(imp.java:824)
at org.python.core.imp.importName(imp.java:884)
at org.python.core.ImportFunction.__call__(__builtin__.java:1220)
at org.python.core.PyObject.__call__(PyObject.java:357)
at org.python.core.__builtin__.__import__(__builtin__.java:1173)
at org.python.core.imp.importAll(imp.java:998)
at org.python.pycode._pyx38.f$0(:1)
at org.python.pycode._pyx38.call_function()
at org.

python.core.PyTableCode.call(PyTableCode.java:165)
at org.python.core.PyCode.call(PyCode.java:18)
at org.python.core.Py.runCode(Py.java:1261)
at org.python.core.Py.exec(Py.java:1305)
at org.python.util.PythonInterpreter.exec(PythonInterpreter.java:206)
at org.sikuli.script.ScriptRunner.runPython(ScriptRunner.java:61)
at org.sikuli.ide.SikuliIDE$ButtonRun.runPython(SikuliIDE.java:1572)
at org.sikuli.ide.SikuliIDE$ButtonRun$1.run(SikuliIDE.java:1677)

java.lang.NoClassDefFoundError: java.lang.NoClassDefFoundError: Could not initialize class org.sikuli.script.App"

Revision history for this message
Fabian (fabian-ledoeuff) said :
#15

I made a brand new install of Sikuli X and all exceptions are gone.
main.sikuli and sub.sikuli are in the same folder (c:\NonReg)

So I ran the main.sikuli :
print "***** before import"
for e in sys.path: print e
okImport = False

try:
    import subNotExists
    okImport = True
except:
    print "***** after import not ok"
    for e in sys.path: print e

if okImport:
    print "***** after import ok"
    for e in sys.path: print e

Here is my message area :
"
***** before import
C:\Program Files\Sikuli X\sikuli-script.jar
C:\Program Files\Sikuli X\Lib
C:\Program Files\Sikuli X\sikuli-script.jar\Lib
__classpath__
__pyclasspath__/
C:/DOCUME~1/genesys/LOCALS~1/Temp/tmp-470306579.sikuli/
C:\DOCUME~1\genesys\LOCALS~1\Temp
C:/Program Files/Sikuli/main.sikuli/
C:\Program Files\Sikuli
.
C:/NonReg/main.sikuli/
C:\NonReg

***** after import not ok
C:\Program Files\Sikuli X\sikuli-script.jar
C:\Program Files\Sikuli X\Lib
C:\Program Files\Sikuli X\sikuli-script.jar\Lib
__classpath__
__pyclasspath__/
C:/DOCUME~1/LOCALS~1/Temp/tmp-470306579.sikuli/
C:\DOCUME~1\LOCALS~1\Temp
C:/Program Files/Sikuli/main.sikuli/
C:\Program Files\Sikuli
.
C:/NonReg/main.sikuli/
C:\NonReg"

Revision history for this message
Fabian (fabian-ledoeuff) said :
#16

I made a new test with the x release :
main.sikuli :
myScriptPath=r"C:\NonReg"
if not myScriptPath in sys.path:sys.path.append(myScriptPath)
import VPsLogin
VPsLogin.sub1()
....

VPsLogin.sikuli :
from sikuli import *
def sub1(self):
....

And it works !!
So it was clearly a release issue.
Thanks a lot for your time and your help RaiMan !!!

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

BTW: def sub1(self):
does not make sense if you do not have self defined classes with methods.
So it should be:
def sub1():

So this should give you a runtime error in main like:
sub1 needs 1 parameter 0 given

Revision history for this message
Fabian (fabian-ledoeuff) said :
#18

Indeed, it's just a copy/paste issue ;)