@nmudge I have seen all the questions about how to import python libraries to ignition in the forum, I find it troublesome to realize it. Now the ignition is Version 8.0, I doubt whether the software have improved better. If I’d like to use 3rd python or jython libraries in V8.0, can you give me some suggestion?
If your external python library uses DLLs via CPython’s native interface, it will not (ever) work. If your external library needs python 2.7, you are in luck, as Ignition v8 has upgraded jython to 2.7.
It’s also worth noting that we enabled python site-packages/ in Ignition 8. (Ignition/user-lib/pylib/site-packages)
What this means is that you can drop libraries in there to make them available in Ignition.
You can also use pypi / pip by doing the following:
- Install jython 2.7.1 on your system from https://jython.github.io/download. (Alongside Ignition) Then run “jython -m ensurepip” to install pip
- Run “jython -m pip install requests” (‘requests’ is just an example; do whatever package you want) It’ll resolve dependencies for you and put everything in jython2.7.1/Lib/site-packages
- Copy all contents from jython2.7.1/Lib/site-packages to Ignition/user-lib/pylib/site-packages
As others have mentioned, this doesn’t work for CPython specific packages that have native dependencies, but it does work for a whole slew of utility, parsing, and web libraries (things like AWS client libraries).
[Edited to add download link for Jython 2.7.1]
Using pip to install packages
I’m trying to get requests working. I’ve followed Kevin’s instructions above (a vertical learning curve for me!). I eventually made it to step 3, but when I try to run a gateway script with the first line:
from requests import requests
(Hopefully I’m doing this right). I’m getting an error:
Caused by: org.python.core.PyException: Traceback (most recent call last): File “”, line 1, in File “C:\Program Files\Inductive Automation\Ignition\user-lib\pylib\site-packages\requests_init_.py”, line 112, in from . import utils File “C:\Program Files\Inductive Automation\Ignition\user-lib\pylib\site-packages\requests\utils.py”, line 26, in from .internal_utils import to_native_string File “C:\Program Files\Inductive Automation\Ignition\user-lib\pylib\site-packages\requests_internal_utils.py”, line 11, in from .compat import is_py2, builtin_str, str File “C:\Program Files\Inductive Automation\Ignition\user-lib\pylib\site-packages\requests\compat.py”, line 31, in import json File "C:\Program Files\Inductive Automation\Ignition\user-lib\pylib\json_init.py", line 108, in from .decoder import JSONDecoder File “C:\Program Files\Inductive Automation\Ignition\user-lib\pylib\json\decoder.py”, line 24, in NaN, PosInf, NegInf = _floatconstants() File “C:\Program Files\Inductive Automation\Ignition\user-lib\pylib\json\decoder.py”, line 18, in _floatconstants BYTES = ‘7FF80000000000007FF0000000000000’.decode(‘hex’) File "user-lib\pylib\encodings_init.py", line 142, in search_function raise CodecRegistryError,\ encodings.CodecRegistryError: incompatible codecs in module “encodings.hex_codec” (C:\Program Files\Inductive Automation\Ignition\user-lib\pylib\encodings\hex_codec$py.class)
… 147 common frames omitted
The version of requests I have is 2.22.0
Any suggestions will be hugely appreciated!
Hi Alistair, a few questions:
- Did you upgrade from a prior version of Ignition to Ignition 8? Which version of 8 are you running?
- Can you try changing your first line to “import requests”? You shouldn’t need the “from requests” part.
I ask question 1 since some upgrades in the past from Ignition 7.9 left a few python directories around, which could have older versions of certain parts. I think we’ve resolved all of those issues in the latest version.
Just for reference, when you copy over the contents of site-packages from jython’s lib directory to Ignition’s for requests 2.22.0, you should be copying the dependencies as well. These are the ones I think requests needs: certifi, chardet, idna, urllib3.
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?:
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.
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!
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.
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.
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.
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:
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.
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.
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
Thanks Tate and Kevin, I will give this a try when i get some time.