Importing Python library smtplib

Asked by Gary Yiu

I want to import the smtplib to the sikuli script I'm writing so Sikuli can send email automatically when the test is finished.

However, I encounter a problem that Sikuli cannot find the smtplib module in Python which I am sure it is installed and located in the Python27/Lib directory. Below is the code I am using. I use SikuliX 1.1.0 and Python 2.7.

-------------------------------------------------------------------------

import smtplib
sender = '<email address hidden>'
receivers = ['<email address hidden>']

message = """From: From Person <email address hidden>
To: To Person <email address hidden>
Subject: SMTP e-mail test

This is a test e-mail message.
"""

try:
   smtpObj = smtplib.SMTP('test.com.hk')
   smtpObj.sendmail(sender, receivers, message)
   print "Successfully sent email"
except:
   print "Error: unable to send email"

-------------------------------------------------------------------------

When I run it in Sikuli IDE, it gives me:

"[error] script [ send ] stopped with error in line 2
[error] ImportError ( No module named utils )
[error] --- Traceback --- error source first line: module ( function ) statement 46: smtplib ( <module> ) import email.utils
[error] --- Traceback --- end --------------"

Can anyone help? Thanks

Question information

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

Python is not relevant for SikuliX, since it internally uses the bundled Java based Jython interpreter.
SikuliX only allows to run scripts written in Python language.

With version 1.1.0 (final) it is Jython 2.7, which says, it is compatible to the Python language level 2.7 including the standard modules, that come with the Python 2.7 package.

Python modules (usually pip installable) can only be used in the SikuliX context, if they are completely written in Python language and do not depend on any C-based stuff or native libraries.

I just tested your script with the final 1.1.0 and it runs through to the final statement telling
Error: unable to send email

So be sure, not to have any pointers to any stuff from your Python 2.7 in the SikuliX environment at runtime.

Revision history for this message
Gary Yiu (cpgaryyiu) said :
#2

Thanks for your reply Raimund.

I look for my system path using print(sys.path) and found out that u'__pyclasspath__' is in the classpath, does it mean it is pointed to the python library?

u'C:\\jython2.7.0\\Lib' is also in the classpath, I suppose it is pointed to the jython library.

As you are saying SikuliX is using Jython, but the smtplib module is also in the Jython 2.7 library, is there any reason why I am getting the above mentioned error?

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

you say that you are running the script in SikuliX IDE (final version)
... but then the sys.path should look like this:
C:\Users\RaiMan\AppData\Local\Temp\Sikulix_1336490921\tmp-2133697922.sikuli
C:\SikuliX\Lib
C:\SikuliX\sikulix.jar\Lib
__classpath__
__pyclasspath__/

1st entry is the script folder
2nd and 3rd are standard entries created by the interpreter Jython at startup with respect to the containing jar. The 3rd entry points to the container of all the Jython stuff including the Jython standard libraries (inside the jar).
the last two entries are standard place holders for internal use by the interpreter, so __pyclasspath__ is definitely Jython stuff.

So please make sure, you have the final SikuliX 1.1.0 and no pointers to anything when running the SikuliX IDE (it is completely self contained).

The picture is a bit different, when you want to run a SikuliX script from commandline using your own installed Jython (e.g. your
C:\jython2.7.0) and even a bit more different, when running scripts in Eclipse/PyDev.
Come back if it is that, what you want.

Just tested on Windows: works.

Revision history for this message
RaiMan (raimund-hocke) said :
#4
Revision history for this message
Gary Yiu (cpgaryyiu) said :
#5

Thanks for your answer Raimund.

I suspect there is some pointers the I don't know of affect SikuliX. So I use a fresh new Windows 7 in a VirtualBox. The script import the library with no problem and successfully send the email. However, when I try to send the email again, I get error message like this:

Oct 15, 2015 10:40:19 AM org.python.netty.channel.AbstractChannel$AbstractUnsafe register
WARNING: Force-closing a channel whose registration task was not accepted by an event loop: [id: 0x20d185d7]
java.util.concurrent.RejectedExecutionException: event executor terminated
at org.python.netty.util.concurrent.SingleThreadEventExecutor.reject(SingleThreadEventExecutor.java:745)
at

Then, I restart the machine and it works again, but whenever I send the second mail, it fails like the above mentioned.

Any idea what cause this? Thanks a lot.

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

no idea.

If this works with Python, then it seems to be a problem in the smtp module implementation in Jython 2.7.

The alternative would be, to trigger a script run by Python via the run() command (see docs) in your SikuliX script, so you do not depend on the Jython smtp implementation.

The external script could be completely built in your SikuliX script based on a template, then written to a file, which in turn is used with the Python command.

Revision history for this message
Gary Yiu (cpgaryyiu) said :
#7

Thanks Raimund! Much appreciated.