Error Opening Script Console

Hi all,

We are trying to post some alarms to a slack channel, and using this test code causes it to fail (and kills our script console as well):

from urllib import request, parse
import json
def send_message_to_slack(text, slackURL):
    post = {"text": "{0}".format(text)}
 
    try:
        json_data = json.dumps(post)
        req = request.Request(slackURL,
                              data=json_data.encode('ascii'),
                              headers={'Content-Type': 'application/json'}) 
        response = request.urlopen(req)
    except Exception as e:
        print("Exception: " + str(e))
 
send_message_to_slack('Test Alarm')

We get an error message when trying to open a script console, and it prevents us from access. Here is our error message:

Exception in thread "AWT-EventQueue-0" ImportError: Cannot import site module and its dependencies: Error loading module net: Traceback (most recent call last):
  File "<module:net>", line 6, in <module>
  File "C:\Users\acecola\.ignition\cache\gw172.16.40.251_8088\C0\pylib\urllib.py", line 26, in <module>
    import socket
  File "C:\Users\acecola\.ignition\cache\gw172.16.40.251_8088\C0\pylib\socket.py", line 3, in <module>
    from _socket import (
  File "C:\Users\acecola\.ignition\cache\gw172.16.40.251_8088\C0\pylib\_socket.py", line 12, in <module>
    from collections import namedtuple, Iterable
ImportError: cannot import name Iterable

Determine if the following attributes are correct:
  * sys.path: [C:\Users\acecola\.ignition\cache\gw172.16.40.251_8088\C0\pylib, C:\Users\acecola\.ignition\cache\resources\platform\jython-2.7.1-ia2.jar\00000000FD8C6322\Lib, __classpath__, __pyclasspath__/]
    This attribute might be including the wrong directories, such as from CPython
  * sys.prefix: C:\Users\acecola\.ignition\cache\resources\platform\jython-2.7.1-ia2.jar\00000000FD8C6322
    This attribute is set by the system property python.home, although it can
    be often automatically determined by the location of the Jython jar file

You can use the -S option or python.import.site=false to not import the site module
Exception in thread "AWT-EventQueue-0" ImportError: Cannot import site module and its dependencies: Error loading module net: Traceback (most recent call last):
  File "<module:net>", line 6, in <module>
  File "C:\Users\acecola\.ignition\cache\gw172.16.40.251_8088\C0\pylib\urllib.py", line 26, in <module>
    import socket
  File "C:\Users\acecola\.ignition\cache\gw172.16.40.251_8088\C0\pylib\socket.py", line 3, in <module>
    from _socket import (
  File "C:\Users\acecola\.ignition\cache\gw172.16.40.251_8088\C0\pylib\_socket.py", line 12, in <module>
    from collections import namedtuple, Iterable
ImportError: cannot import name Iterable

Determine if the following attributes are correct:
  * sys.path: [C:\Users\acecola\.ignition\cache\gw172.16.40.251_8088\C0\pylib, C:\Users\acecola\.ignition\cache\resources\platform\jython-2.7.1-ia2.jar\00000000FD8C6322\Lib, __classpath__, __pyclasspath__/]
   This attribute might be including the wrong directories, such as from CPython
  * sys.prefix: C:\Users\acecola\.ignition\cache\resources\platform\jython-2.7.1-ia2.jar\00000000FD8C6322
    This attribute is set by the system property python.home, although it can
    be often automatically determined by the location of the Jython jar file

You can use the -S option or python.import.site=false to not import the site module
Exception in thread "AWT-EventQueue-0" ImportError: Cannot import site module and its dependencies: Error loading module net: Traceback (most recent call last):
  File "<module:net>", line 6, in <module>
  File "C:\Users\acecola\.ignition\cache\gw172.16.40.251_8088\C0\pylib\urllib.py", line 26, in <module>
    import socket
  File "C:\Users\acecola\.ignition\cache\gw172.16.40.251_8088\C0\pylib\socket.py", line 3, in <module>
    from _socket import (
  File "C:\Users\acecola\.ignition\cache\gw172.16.40.251_8088\C0\pylib\_socket.py", line 12, in <module>
    from collections import namedtuple, Iterable
ImportError: cannot import name Iterable

Determine if the following attributes are correct:
  * sys.path: [C:\Users\acecola\.ignition\cache\gw172.16.40.251_8088\C0\pylib, C:\Users\acecola\.ignition\cache\resources\platform\jython-2.7.1-ia2.jar\00000000FD8C6322\Lib, __classpath__, __pyclasspath__/]
    This attribute might be including the wrong directories, such as from CPython
  * sys.prefix: C:\Users\acecola\.ignition\cache\resources\platform\jython-2.7.1-ia2.jar\00000000FD8C6322
    This attribute is set by the system property python.home, although it can
    be often automatically determined by the location of the Jython jar file

You can use the -S option or python.import.site=false to not import the site module

I assume this is some sort of import error, removing the code causes it to work again, and we can open our script console after that.

Another problem, after deleting that function and replacing it with some other function (a simple gateway logger), it looks like we not longer have “system” imported into the script, so we have to type in “import system” at the top of the script. Re-naming the script allows us to ignore “import system”, but even deleting the script and making a new script with the same name continually gives us this importing issue. Any ideas as to what is happening here?

We get this error:

Traceback (most recent call last):
  File "<buffer>", line 4, in <module>
  File "<module:net>", line 3, in logAlarm
NameError: global name 'system' is not defined 

It looks like deleting scripts doesn’t actually get rid of them (we deleted / re-named the modified script, but it still appeared to be callable in our script console). Unless the script console is the real culprit here?

Best,
Roger

It looks like the urllib references you’re using are perhaps for Python 3? Ignition 8.0 uses Python 2.7, so you’ll need to reference the documentation for that. The implementation for urllib has definitely changed between Python 2 and 3.

Very likely, I didn’t bother checking urllib when I tried it, I just expected a simple error telling me it couldn’t import it, not a complete crash of the script console.

I’m more concerned as to why I can no longer freely use “system” in that script.

You have a literal script file named net? That may be conflicting with builtin package naming, which could be the reason import system isn’t working. Also, was this an upgrade from a 7.9 or previous system? There was an issue (very recently fixed) with upgrades ending up with two versions of the collections module (Jython 2.5 has it in a single file, 2.7 has it in a package) causing other problems.

That’s pretty hilarious. Yeah, we have a script file called net. After deleting the net script, we are not encountering the issue on any other script file (i’m not sure if it’s because of package naming conflict, or because that weird error we had with the urllib import). In any case, we’ll probably try with a test package to see if we really cannot import urllib, or if the net script was the true culprit.

This was an original 8.0 installation. We’re also having trouble getting scripts into alarm pipelines, but I recall that being possible before project/shared were obsoleted. Is that a known issue or are we just unlucky?

That should work, although there is an issue (fixed in 8.0.1) with calculated rosters in particular - but a script block should be able to use any local-to-the-project script module (including inherited ones).