Import python libraries

I’d like to import some Python libraries in order to add more functionability. Is it possible?

Thanks in advance

Right now, is not possible.

Consider the following things:

The scripting currently used in Ignition is Jython 2.5, not all the Python libraries (specifically those that are CPython library) are compatible: for more detail see this

There are a issue in the way that Ignition implements package/module organization for Jython that make that some jython code imported in app library that work outside ignition , not work inside: for more detail see this

If you need third-party funcionality, right now, the best way is using the java sdk and make available some java library in jython scripting.

Regards,

You also can import java classes to leverage your apps.

I believe 7.7 will have this functionality.

Are you talking about importing modules that exist in Python’s Standard Library? You can import many modules that exist in Python’s Standard Library.

Here’s some documentation about the modules you can import using Jython 2.5:
jython.org/docs/library/indexprogress.html

If you are talking about third-party Python libraries, you can copy and paste code from a third-party Python library into Ignition app modules. You will likely need to make scoping changes and change some names to use app.* module names.

In some cases this can be done and work. I’ve seen it work a couple times.

Kyle is right that Ignition 7.7 will have a better way to add third-party Python libraries to Ignition.

so if it’s not possible and I decide to use The Ignition Module SDK, am I able to import external Java libraries?

Yes

I would like to import a croniter 0.3.5 python library to do some cron time based scheduling.

would it be possible if someone could explain where and how I would import these .py files into Igntion’s directory and then be able to import them in a script. Attached is the package info.

Also if something like this already exists within ignition that would be a great help too.

Thanks - Zack
PKG-INFO.txt (4.41 KB)

Hi Zack,

Put the Python files in the user-lib/pylib directory where Ignition is installed. Then import the library into your scripts the regular Python way.

For example, in a Python script in Ignition:

from croniter import croniter

Best,

Thanks a million Nick!

You bet!

Joining this thread late, but I am trying to import the xlrd module in order to read one value once an hour from an existing excel spreadsheet. My short script runs fine in python, but I can’t figure out how to tie this into Ignition to run from a transaction group. I tried putting the xlrd folder in the user-lib/pylib directory, and also in the user-lib/pylib/site-packages directory. Then in the Interactive Script Tester I tried both “import xlrd” and “from xlrd import open_workbook”, but it invariably returns “ImportError: cannot import name open_workbook”. Can anyone point me in the right direction to make this work? Or maybe there is a better method to retrieve my excel value?? Thanks!

Bryce Troy
Left Hand Brewing Company
Longmont, Colorado

Bryce,

Try the following:

  1. find the “xlrd” directory that contains a file called init.py (this is the file that tells Python that the directory is to be treated as a Python package)
  2. copy that directory into Ignition’s {installation directory}\Inductive Automation\Ignition\user-lib\pylib folder
  3. In your code, your import statement and your calls will be (for example)

from xlrd import file_name filename.some_function()
where “file_name” is the name of the .py file / module in the xlrd folder / package that contains “some_function”

Since the jsonDecode builtin function works only for json arrays (not for json elements) (Ignition 7.6.7), I’m trying to place the [b]simplejson library /b in the gateway path.
No one of my test worked, with import simplejson as json I always get the “ImportError: No module named simplejson”.
Tried placing the dir there:
user-lib/simplejson
user-lib/pylib/simplejson

with no luck.
Any suggestion?
Thanks

Hi marioquark,

Putting Python libraries in the user-lib/pylib directory is an Ignition 7.7 feature.

Why do you say Ignition’s builtin system.util.jsonDecode function only works for json arrays? It should work for any json.
Best,

Hi,
@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.

1 Like

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:

Follow the Using pip section in the linked document here. Python, Jython, CPython, Libraries, Pip, and Python 2.7 vs 3 - A quick primer

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 reference the guidance doc with updated instructions.]

12 Likes

Hi,
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!
Thanks.

Hi Alistair, a few questions:

  1. Did you upgrade from a prior version of Ignition to Ignition 8? Which version of 8 are you running?
  2. 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.