Procedure for installing python libraries from source

Is this the same procedure when attempting the same from a Ignition 8.1.X system?(Currently looking at a 8.1.4, but you guys upgraded to java 11 for a bunch of stuff and I figured I would check)

These instructions were written against 8.0, which also runs Java 11, and work fine. The Java installation is just for Jython’s pip, nothing to do with your actual Ignition system. You could also run Java 11 and I doubt it would cause any problems.

1 Like

I tried all these steps and I was able to successfully install the desired package to jython. One issue I am having is that when I try importing from the package I installed (literally 1st line of my .py script) I get the following error:

File “C:\jython2.7.1\Lib\site-packages\serial_ init _.py”, line 33, in
from serial.serialjava import Serial
File “C:\jython2.7.1\Lib\site-packages\serial\serialjava.py”, line 38, in
comm = detect_java_comm([
File “C:\jython2.7.1\Lib\site-packages\serial\serialjava.py”, line 32, in detect_java_comm
raise ImportError(“No Java Communications API implementation found”)
ImportError: No Java Communications API implementation found

Does this mean that Java does not have serial communications abilities? The package I want to use uses python’s pyserial package which I also installed.

I don’t think Java had ever included an implementation of the comm API. It’s very old and has always been BYO implementation because it’s just an API to program against.

Something called rxtx used to be a popular implementation but I don’t know if it works any more.

So you don't think there's a way to use this package with Ignition/Jython?

pySerial uses DLLs which you can't use in Jython.

That’s unfortunate… My python script simply queries a serial device and outputs a result. Is there any way I can display this result in Ignition?

The simplest option would probably be to use system.util.execute to launch your Python script directly and examine the result.
Alternately, you could (purchase and) install the serial module for the appropriate scope (Vision client or gateway) and then adapt your script to use system.serial scripting functions - then everything is kept within Ignition and is somewhat more ‘portable’.

4 Likes

I am trying to use that function in the script console, but I am seeing no output. Here’s how I’m executing the function:

system.util.execute(["C:\\Python27\\python.exe","C:\\Users\\mdia\\Desktop\\test.py"])

Pretty sure I’m doing it wrong… How can I execute the python script using this function?

Oh, right - system.util.execute is a 'fire-and-forget', it doesn't give you access to standard output. Try adapting this:

from java.lang import ProcessBuilder, String

pb = ProcessBuilder([r"C:\Users\pgriffith\AppData\Local\Microsoft\WindowsApps\python3.exe", r"C:\Users\pgriffith\Downloads\test.py"])
process = pb.start()
output = String(process.getInputStream().readAllBytes())

print output

(Note: This blocks until the process completes waiting on the input stream. It may or may not ever complete, depending on what you're running, which could block a thread forever. Be careful).

2 Likes

I tried the following and it’s still not printing out an output.

from java.lang import ProcessBuilder, String

pb = ProcessBuilder([r"C:\Users\mdia\AppData\Local\Microsoft\WindowsApps\python.exe", r"C:\Users\mdia\Desktop\test.py"])

process = pb.start()

output = String(process.getInputStream().readAllBytes())

print output

Not working with python3 either. The script that I am testing with simply prints out a string.

Where are you running this, and is the Python binary and test script located on the same machine?

That code worked fine for me in the Designer script console with some changes for my environment:

from java.lang import ProcessBuilder, String

pb = ProcessBuilder([r"python", r"/Users/kevin/Desktop/test.py"])

process = pb.start()

output = String(process.getInputStream().readAllBytes())

print output

1 Like

Running in the script console, and yes.

It finally worked for some reason after replacing the full python directory

“C:\Users\mdia\AppData\Local\Microsoft\WindowsApps\python.exe” with just “python”.

Hello all,
it works in the script console but not in an script event, any idea ?

Uhm, gateway events run in the gateway service, not in your workstation.

2 Likes

How did you figure it out? ( I'm genuinely curious about it )
The official link says that the packages are 100% pure python and that is also usable with Jython
https://pyserial.readthedocs.io/en/latest/pyserial.html

Cheers,
Cristian

That it uses DLLs isn’t quite accurate. It uses platform-specific modules like win32 and ctypes, which aren’t fully implemented in Jython.

It does mention that it “works on Jython” with the caveat that you have a JavaComm implementation on the ClassPath, which is basically saying “BYO serial implementation that conforms to this API”, but you don’t have that and the JavaComm API is old, crusty, and deprecated.

This probably deserves a separate topic if further discussion is necessary.

1 Like

Hey @PGriffith, are there similar instructions to follow with the Gateway running in Linux?

The instructions would be essentially the same, just with commands like ln instead of mklink.

If you’re in to transport protocols, here’s a two-liner for POSIX that you can use to get the libraries listed below:

git clone https://github.com/barbinbrad/ignition-jython-modules
rsync -r --exclude '.git' ignition-jython-modules/. /usr/local/ignition/user-lib/pylib

Note: There’s no security problems here, but you don’t know me, so it’s good to assume that there are. The better approach is to build your own jython modules instead of using trusting anyone. This is just an example of how you might do something similar.