Import python libraries

Hi Kevin,
No, this was a clean install of 8.0.
It’s working now. When I came back to it I got a different error. Then I removed the “from requests” and it worked!
Yes, I had copied the entire site-packages.
Thanks, much appreciated.

I this where we download jython?:
https://jython.org/downloads.html

Was only able to download 2.7.0 (not 2.7.1). The file I downloaded was named remotecontent, I needed to add .jar to it…

After I ran the installer, I tried jython -m ensurepip in command prompt but I get the error:

'jython' is not recognized as an internal or external command,
operable program or batch file.

I must be doing something wrong, please help!

The Jython web site isn’t really maintained, unfortunately; the project is kind of on life support, mostly everything is via the mailing list now, but you can find 2.7.1 at: https://search.maven.org/search?q=a:jython-installer

If you can’t run jython after installing it, then you probably need to add it to your path; just add its folder to your system path variable. I believe that’s an option in the installer you might not have checked. The painful way is to run it via java.exe, but jython.exe is supposed to take care of that for you.

1 Like

Silverbacknet - Thanks for filing in some information in the last post.

Dion - To add a little to this:

The Jython website is going through an update. The new website can be found here:

(The maintainers need to get the site switched over, but seem to be slow in doing so. Eventually this will be the new site at jython.org)

Go to this page:

You’ll see the “Jython Installer”. That’s the one you want. Make sure you have Java installed first, as Jython relies on Java for the installation. Oracle’s Java 11 is a fine option, or you can use Zulu 11 if you want to match the version Ignition uses. ( https://www.azul.com/downloads/zulu-community/ )

When you run that file, which is the installer, it’ll let you choose the install location. It won’t put it on the path, though, so “jython” shouldn’t work from the command line in general. You’ll need to open a command prompt, change (cd) to the right directory, and then run “jython” from there.

Hope this helps!

2 Likes

Hi Kevin,

Thanks for the info. I downloaded Zulu: 11.33.15 JRE for WIndows 64-bit (zip file) and extracted it to C:\Program Files\Zulu as stated in the documentation.

I then downloaded and installed Jython 2.7.1 to C:\jython2.7.1

If I open a command line and cd to C:\jython2.7.1 and run “jython” but I am still getting

'jython' is not recognized as an internal or external command,
operable program or batch file.

Hi Dion, just explore the jython directory. I believe there’s a “bin” directory that contains the binaries, where you’ll find jython.exe. As long as you cd to the directory with jython.exe first, you should have no problem running it.

1 Like

Also, if you used the zip download of Zulu you might need to add java to the path on Windows. If you double clicked on the jython installer .jar file and it walked you through the installation, you’re probably good. If you haven’t done that yet, I’d suggest getting the Zulu .msi installer, since it will set up the path and file associations for you.

1 Like

Dear Anna, I need help in importing XLRD, currently it is unable to import
I am using Ignition 7.3.7

xlrd requires python 2.7, which means you need Ignition v8. It claims to be pure python, so no worries about DLLs. You might find an older version of xlrd that works with jython 2.5.x if you dig around on the xlrd release site. Beware–Ignition v7.3.7 is ancient, and may not be able to do even that.

1 Like

Hi, thanks for sharing concept about python which I don’t know yet. I start to learn python from CETPA so this is useful for me.

Quick note on CETPA python training: Their training will likely be for Python 3, not Python 2. They’re also likely to provide training on CPython as opposed to Jython, so a number of parts of the training likely won’t apply to Python directly in Ignition, which is Jython 2.7 in Ignition 8. (You can always run Python outside of Ignition and invoke it from inside Ignition if you need to.)

CETPA Python training will likely give you really good information on how to use the basics of Python. However, keep in mind that a good number of the "import " statements would be specific to Python 3 and/or CPython. There are also a few language constructs that are a little different between Python 2 and Python 3.

If you’re looking for Python support directly inside Ignition, this is a good place to learn the basics:
https://docs.inductiveautomation.com/display/DOC80/Python+Scripting
https://docs.inductiveautomation.com/display/DOC80/Scripting+Functions
https://docs.python.org/2/library/
https://docs.python.org/2/py-modindex.html

1 Like

Hi Kevin, how can I do this?

I do this by “warpping” the python in an api app using either Bottle or Flask for python. I then use the [system.net.httpPost] class to post to the python REST API from with in Ignition, then parse the returned text (json) and use as needed. I have ran this right next to ignition both server and edge. Hope this is helpful. Regards.
(https://docs.inductiveautomation.com/display/DOC80/system.net.httpPost)

1 Like

Thanks Tate! Dion - What Tate wrote is generally the best way to do it.

Your other options:

  • If you don’t need a return value from your outside Python, use Ignition’s system.util.execute()
  • If you do need a return value, you can get fancy and call Java’s executor Runtime.getRuntime().exec(cmd), and then get the string output value from that.

As mentioned, Bottle or Flask are going to be more robust and cleaner than doing an execute() / exec(), so go that route if you can.

Bottle example:

from bottle import route, run

@route('/hello')
def hello():
    return "Hello World!"

run(host='localhost', port=8080, debug=True)

This example serves out the responses to http://localhost:8080/hello

1 Like

Thanks Tate and Kevin, I will give this a try when i get some time.

@Kevin.McClusky
I am getting this error when going through these steps.

That doesn’t surprise me. psutil uses OS functions. It looks like _winreg is direct access to the Windows Registry, which CPython probably does via native C calls. A quick search shows _winreg isn’t available in Jython, probably because of the dependencies on C.

As mentioned in my original post, a good portion of Python libraries won’t work, but a number do. If you get installation errors like this, it normally means there are C dependencies or other requirements, where the author has written the library for CPython.

If you have to use CPython libraries, you can choose to run CPython 3 alongside Ignition. If you want to go this route, take a look at the bottle example above for how to do it.

1 Like

@Kevin.McClusky
Getting same error when installing ‘requests’ library

Looks like there was a recent update to pip and that version (20.2.2) isn’t currently working with Jython.

If you’re getting this error, you probably ran “jython -m pip install --upgrade pip” at some point.

To restore pip back to a version that’s compatible with Jython 2.7.2, delete the contents of your site-packages directory, then run “jython -m ensurepip” again. (It appears that 19.1 works well, which I think is the default that ships with Jython 2.7.2.)

2 Likes

I dug into this pip error a little more, and it looks like the pip developers made their latest updates with the expectation that if a user was running Jython, they would have the Java JNA libraries available.

As mentioned, running with the older 2019 release of pip called pip 19.1 should work fine, and you don’t need to worry about this.

If you do want to run with the latest version of pip, it is possible to resolve that error and update to pip 20.2.2 currently. Here’s how:

  1. Download two jars from the JNA project: jna-5.6.0.jar and jna-platform-5.6.0.jar
  2. Invoke pip passing in the JNA jars into the classpath. For example, “jython -J-cp jna-5.6.0.jar;jna-platform-5.6.0.jar -m pip install requests”
4 Likes