Python smtplib module breaking arcpy?

2506
4
12-17-2012 05:40 AM
TomWilson2
New Contributor
Hi,

In my organization we have Python scripts used for data checks, which use ArcPy (ArcGIS 10.0 SP4) and also the smtplib modul,e so we can receive automated emails regarding data errors.

Problem we are finding is if try these scripts on our laptops. Not only cannot get script to connect to email server (which is not a problem I expect help for here), but it also semi-permanently breaking ArcPy AFTER failing to connect to email server via smtp module. I cannot understand why the two are linked? Simplest case from command prompt:

C:\>python

Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on

win32

Type "help", "copyright", "credits" or "license" for more information.

>>> import smtplib

>>> server=smtplib.SMTP('<MAIL SERVER>') 

Traceback (most recent call last):

  File "<stdin>", line 1, in <module>

  File "C:\Python26\lib\smtplib.py", line 239, in __init__

    (code, msg) = self.connect(host, port)

  File "C:\Python26\lib\smtplib.py", line 295, in connect

    self.sock = self._get_socket(host, port, self.timeout)

  File "C:\Python26\lib\smtplib.py", line 273, in _get_socket

    return socket.create_connection((port, host), timeout)

  File "C:\Python26\lib\socket.py", line 514, in create_connection

    raise error, msg

socket.error: [Errno 10061] No connection could be made because the target machine actively refused it

>>> import arcpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\__init__.py", line 17, i
n <module>
    from geoprocessing import gp
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\__init__.p
y", line 14, in <module>
    from _base import *
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py",
 line 568, in <module>
    env = GPEnvironments(gp)
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py",
 line 565, in GPEnvironments
    return GPEnvironment(geoprocessor)
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py",
 line 521, in __init__
    self._refresh()
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\geoprocessing\_base.py",
 line 523, in _refresh
    envset = (set(env for env in self._gp.listEnvironments()))
RuntimeError: NotInitialized



Now, ArcPy is broken on this laptop, running any ArcPy script with or without smtplib module. If I tried a script from a Toolbox, or any script with import ArcPy it will fail with the above error. I can import arcgisscripting successfully, however.

Rebooting makes no difference, uninstalling Python and ArcGIS 10.0 makes no difference. However, over time (maybe a week or two) ArcPy returns.

I just wondered if anyone had experienced this? There is a definite link with smtplib causing the problem to the ArcPy module and have reproduced it on multiple laptops here, never desktop PCs where the same is fine. Thanks.
Tags (2)
0 Kudos
4 Replies
MathewCoyle
Frequent Contributor
Your smtp error is most likely due to your server listening on a different port than default. To correct for this you will have to specify which port you want to connect to.

Have you tried installing Python using the default settings? Your paths indicate you had a previous version of python installed prior to installing ArcGIS or used some other customization for your python install. Are you sure you are on version 2.6.5?
0 Kudos
ChrisSnyder
Regular Contributor III
To get my machine to send emails via Python, I had to get our sysadmin people to unblock port 25.

Otherwise you can specify another port - for example:

def sendEmail(emailServer, login, password, port, to, fro = "", subject="", text="", files=[]):
    """Sends an email using a specified mail server and your username credentials"""
    try:
        import smtplib
        from email.MIMEMultipart import MIMEMultipart
        from email.MIMEBase import MIMEBase
        from email.MIMEText import MIMEText
        from email.Utils import COMMASPACE, formatdate
        from email import Encoders
        msg = MIMEMultipart()
        msg['From'] = fro
        msg['To'] = COMMASPACE.join(to)
        msg['Date'] = formatdate(localtime = True)
        msg['Subject'] = subject
        msg.attach(MIMEText(text))
        for file in files:
            part = MIMEBase('application',"octet-stream")
            part.set_payload(open(file,"rb").read())
            Encoders.encode_base64(part)
            part.add_header('Content-Disposition','attachment; filename="%s"' %os.path.basename(file))
            msg.attach(part)
        #try to connect to the email server
        server = smtplib.SMTP()
        server.connect(emailServer, port) #port = 25 or 587 or ?        
        if login != "" and password != "":
            server.ehlo()
            server.starttls()
            server.ehlo()
            server.login(login, password)
        server.sendmail(fro,to,msg.as_string())
        server.quit()
        server.close()
        return True
    except:
        return False
0 Kudos
TomWilson2
New Contributor
Thanks for the suggestions. It is not really the email functionality I am posting about here.  smtplib works fine for us to email when script used on desktop PCs/ servers and we can live with this as a scheduled task would not normally be set on laptops.

What I really would like to resolve is why this failed attempt to connect to email server via smtplib on our laptops breaks the ability to import ArcPy afterwards. Users will normally test a script on the first thing to hand. In our case if tried script using smtplib and it only simply failed to run/ email results this would be fine- I know not to use a laptop if need that working. The problem, however, is this failed attempt to connect via smtplib has for some reason broken the ability to import ArcPy and run any arcpy script whether smtplib is involved or not.

mzcoyle - the Python home is different under the example I showed as I uninstalled and re-installed Python to attempt to fix the problem when it first occurred, which made no difference. Python home was intially C:\Python26\ArcGIS10.0\. I know about setting the Desktop10.pth under site-packages.

I have been using ArcPy fine from the current home for a while. Then test the smtplib connection again- it breaks ability to import arcpy for a week or two and then for seemingly no reason it's back. I have no idea what breaks and fixes it, hence this post.

Thanks
0 Kudos
ChrisSnyder
Regular Contributor III
Like Matt said, I too think your Python install is hosed...

See how stmplib is "looking" under: C:\Python26\lib\smtplib.py... With ArcGIS properly installed the stmplib library should be located under C:\Python26\ArcGIS10.0\Lib. If you have both a C:\Python26\lib folder and a C:\Python26\ArcGIS10.0 folder I can almost 99.9 % assure you that your Pythoin install is to blame. Not sure why things would all of a sudden start working after a few weeks.... that seems weird, but...

As a test, I would recommend on one of your boxes you:
1. Unstall ArcGIS completely
2. Uninstall any and all Python versions including any Python extras like PythonWin.
3. Delete the C:\Program Files\ArcGIS folder.
4. Delete any C:\Python* folders
5. Delete the C:\Users\<usernames>\AppData\Roaming\ESRI folder
6. Reinstall ArcGIS
7. After all that, it "should" be back to normal.
0 Kudos