NameError (!! WHILE IMPORTING!!)

Asked by Raza

Hi,

Using Sikuli IDE 1.1.0-Beta1.

Trying to execute tests using the HTMLTestRunner reporter and getting the following error on line <<from POSTests import *>> in main.sikuli

[error] script [ main ] stopped with error in line 9
[error] NameError ( !!WHILE IMPORTING!! name 'sys' is not defined )

# script named POSTests.sikuli
==============================
importPath = r"C:\SikuliScripts\UtilityFunctions.sikuli"
if not importPath in sys.path: sys.path.append(importPath)

from sikuli import *
import unittest
import UtilityFunctions

class LiqPOS(unittest.TestCase):

    def test_Sale1(self):
        print "SALE 01"

    def test_Sale2(self):
        print "SALE 02"

    def test_Sale3(self):
        print "Sale 03"

# script named main.sikuli
========================
from sikuli import *
import unittest

# import path for HTMLTestRunner
importPath = r"C:\SikuliScripts\HTMLTestRunner.sikuli"
if not importPath in sys.path: sys.path.append(importPath)

import HTMLTestRunner

from POSTests import *

# path to report folder
dir = "C:\\SikuliReports\\LiquorPOS\\"
import os
fp = file(os.path.join(dir, "Rep.html"), "wb")

suite = unittest.TestLoader().loadTestsFromTestCase(LiqPOS)
runner = HTMLTestRunner.HTMLTestRunner(stream=fp, title='POS', description='Simple POS Sale')
runner.run(suite)
fp.close()

If i move the code from main.sikuli to the POSTests.sikuli, it works fine. It must be to do with the way I'm importing, both scripts are in the same directory. Any help is appreciated.

Thanks.

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Tak Eda
Solved:
Last query:
Last reply:
Revision history for this message
Best Tak Eda (takeda) said :
#1

Put "import sys" before the "from sikuli import *" line.

Also, a better code to handle the import will be:

import sys
import os

_path = r'C:\SikuliScripts\HTMLTestRunner.sikuli'

if os.path.exists(_path):
    sys.path.append(_path)

Revision history for this message
Raza (razarf) said :
#2

Thanks Tak,

As per your suggestion, I added the import in both the files but it still gave the same error.

Revision history for this message
Raza (razarf) said :
#3

Updated POSTests.sikuli to this

import sys
from sikuli import *
import unittest

# import path for utility functions
_path = r"C:\SikuliScripts\UtilityFunctions.sikuli"
if os.path.exists(_path):
   sys.path.append(_path)
import UtilityFunctions

and it worked.... didn't have to change main.sikuli

Thanks :-)

Revision history for this message
Raza (razarf) said :
#4

Thanks Tak Eda, that solved my question.