Windows 10: imports no longer working after moving to newest version (run from comand line, sys.path not containing location of common modules)

Asked by Richard Chubb

I am having the same issue as in https://answers.launchpad.net/sikuli/+question/265660
I have followed the suggestions in that question but it has not resolved my issue.

I get the following error

------------------------------------------------------
[error] script [ design_loading1 ] stopped with error in line 3
[error] ImportError ( No module named commonFunctions )
[error] --- Traceback --- error source first
line: module ( function ) statement
3: main ( <module> ) from commonFunctions import *
[error] --- Traceback --- end --------------
-----------------------------------------------------

I was previously using sikuli(x) 1.0.0 and wanted to upgrade to the latest version to take advantage of some of the newest features (including the OCR).

Previously the scripts were run from a .bat/.cmd file that issued a command line to run the sikilu-script.jar, but I see that this is no longer used and that I should be using the new sikuli-ide.jar for running a script from the command line.

The previous command issued was :

"C:/Program Files (x86)/\Java\jre1.8.0_271\bin\java.exe" -Xms64M -Xmx512M -Dfile.encoding=UTF-8 -Dpython.path=C:/Users/richardc/dev/eCS21_1_tpmgr/ncs_autotest/utils/SikuliScripts -jar "C:\Users\richardc\dev\eCS21_1_tpmgr\ncs_autotest\utils\Sikuli\sikuli-script.jar" -r C:/Users/richardc/dev/eCS21_1_tpmgr/ncs_autotest/ncs_vbp_p/start/SikuliScripts/design_loading1.sikuli

where Dpython.path is the location of the common scripts.

Trying to run the exact same script using the new version (2.0.5) and the command I use is the following:

"C:\Program Files\Java\jre1.8.0_291\bin\java.exe" -Xms64M -Xmx512M -Dfile.encoding=UTF-8 -Dpython.path=C:/Users/richardc/dev/eCS21_1_tpmgr/ncs_autotest/utils/SikuliScripts -jar "C:\Users\richardc\dev\eCS21_1_tpmgr\ncs_autotest\utils\Sikuli\sikulixide-2.0.5.jar" -r C:/Users/richardc/dev/eCS21_1_tpmgr/ncs_autotest/ncs_vbp_p/start/SikuliScripts/design_loading1.sikuli

I have tried setting up the PYTHONPATH, JYTHONPATH and CLASSPATH but this had no affect.

... log deleted - not relevant

as can be seen my required path is not being added to sys.path.

Any help would be appreciated.
Thaknks

Question information

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

--- I have tried setting up the PYTHONPATH, JYTHONPATH and CLASSPATH but this had no affect.
This only works, if you are running with Jython from command line.
... but this is not possible easily with 2.0.5, since Jython is bundled

I have 3 solutions:
--- JYTHONPATH
Set it in the environment and use the python features to extract it at runtime

--- parameter
add the path as parameter after -- to the command line

... in both cases you can now add the path to sys.path at runtime

--- site-packages
see https://sikulix-2014.readthedocs.io/en/latest/scenarios.html#access-python-packages-from-sikulix-scripts-run-by-sikulix-gui-or-commandline

Revision history for this message
Richard Chubb (r1chbb) said :
#2

Thanks for the feedback RaiMan.

"
I have 3 solutions:
--- JYTHONPATH
Set it in the environment and use the python features to extract it at runtime

--- parameter
add the path as parameter after -- to the command line

... in both cases you can now add the path to sys.path at runtime

--- site-packages
see https://sikulix-2014.readthedocs.io/en/latest/scenarios.html#access-python-packages-from-sikulix-scripts-run-by-sikulix-gui-or-commandline
"

for the first point:
do you mean set it up in system/user environment variables? If so this is not a viable solution. These are autotests/regession tests that are run on all sort of machines and we can't rely on env vars set on each machine, so we have to set up any env vars in the scripts every time a test is run, which is why I was trying to use 'set JYTHONPATH......'.

What do you mean by 'extract it at runtime'?
do you mean in the actual sikuli script? Again this is not a viable solution as there a 10000s of scripts that would need changing, I need a common soultion.

for the second point:
can you please explain what you mean with an example perhaps?
do you mean add it as optional command line parameter?

'in both cases you can add the path to sys.path at runtime'
if you mean in the sikuli scripts themselves, then as above that is not a viable solution as I need a common solution.

For the third point:
I did start looking into this, but I ran into some difficulties.
I added the path to sites.txt in the app data location but that was not getting copied to the scripts lib location.
I think the scripts copy of sikulix and the lib folder is coming from the .jar file.
I tried to add this in but I do not know the metho to add to a .jar file, I can extract the .jar using 7-zip but I couldn't find out how to create the new .jar after adding the file in.
I did have a look at that page that you linked to , but will take another look because it looks like it may be able to tell me how to repack the .jar.

thanks in advance

Revision history for this message
Richard Chubb (r1chbb) said :
#3

The sites_examples.txt states
# rename/copy this file as sites.txt and it will be seen by SikuliX
#
# add absolute paths one per line, that point to other directories/jars,
# where importable modules (Jython, plain Python, SikuliX scripts, ...) can be found.
# They will be added automatically at startup to the end of sys.path in the given sequence
#
# lines beginning with # or // are ignored and can be used as comments

but I can't use absolute paths, because the tests/scripts are run on different machines.
Can I use a defined env var e.g. %NCS_SIKULI_COMMON% ?

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

ok, completely understood.

Supposing you are using the SikuliX IDE jar, I will create a modified 2.0.5 version until monday for you, that accepts
-Dpython.path=C:/Users/richardc/dev/eCS21_1_tpmgr/ncs_autotest/utils/SikuliScripts

as you used it before.
I will add a link here, where you can download it then.

In 2.0.6 I will add this as a basic solution (allowing more than 1 path separated by ; or :) and additionally the sites.txt accepting relative paths to the working folder and with %...% variables taken from current environment.

Revision history for this message
Richard Chubb (r1chbb) said :
#5

Hi, thanks for that RaiMan.
With the hardcoded path in sites.txt, in my repacked SikuliX IDe jar, the common modules were imported.
although now I have a new different fail, below..... unrelated to this issue I suspect, so I will scan the docs and existing questions for a solution.
----------------------------------------------
[error] script [ design_loading1 ] stopped with error in line 2
[error] ImportError ( !!WHILE IMPORTING!! No module named natives )
[error] --- Traceback --- error source first
line: module ( function ) statement
2: commonFunctions ( <module> ) from org.sikuli.script.natives import Vision
2: main ( <module> ) from commonFunctions import *
[error] --- Traceback --- end --------------
----------------------------------------------

Thanks for your feedback and response
"Supposing you are using the SikuliX IDE jar, I will create a modified 2.0.5 version until monday for you, that accepts"
Yes I am using the SikuliX IDE jar.

"In 2.0.6 I will add this as a basic solution (allowing more than 1 path separated by ; or :) and additionally the sites.txt accepting relative paths to the working folder and with %...% variables taken from current environment."
Both these solutions would be a great option.

thanks for your prompt feedback and the effort in supplying a solution

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

--- my repacked SikuliX IDE jar
for the sites.txt solution, there is no need to pack anything into the SikuliX IDE jar.
A path given in sites.txt is simply added to sys.path at runtime init.

I have a version, that accepts -Dpython.path=some-path
where some-path might either be given
- absolute
- relative to the working directory (including .. placeholder) - the folder where the java command is run from.

Since I want to send you the download link exclusively, I need an e-mail address (you do not have one here in LP).
mine is sikulix---at---outlook---dot---com.

Revision history for this message
Richard Chubb (r1chbb) said :
#7

just sent you an email

Revision history for this message
Richard Chubb (r1chbb) said (last edit ):
#8

--- my repacked SikuliX IDE jar
for the sites.txt solution, there is no need to pack anything into the SikuliX IDE jar.
A path given in sites.txt is simply added to sys.path at runtime init.

for your cases where are you putting the sites.txt file so that it is picked up when running the scripts?
I put it here originally as per the instructions in the docs
C:\Users\richardc\AppData\Roaming\Sikulix\Lib\site-packages
but this didn't work.
The only thing that worked was to etract the jar file and put sites.txt in
sikulixide-2.0.5\Lib\site-packages
and run
Sikulix.buildJarFromFolder("F:\Autotester\New Sikuli\sikulixide-2.0.5.jar", "F:\Autotester\New Sikuli\sikulixide-2.0.5")
to create a new jar file with it in and use that in place of the original 2.0.5 IDE jar

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

got it :-)

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

--- sites.txt
I admit, that the current implementation is a bit complicated.

- start the IDE
- File Menu -> Open Special files
- 3
- the sites.txt file is opened for edit
- add your paths
- save and close the file
- restart the IDE

the stuff is added at init

Can you help with this problem?

Provide an answer of your own, or ask Richard Chubb for more information if necessary.

To post a message you must log in.