Python.Runtime.PythonException: since Python.NET 3.0 int can not be converted to Enum implicitly. Use Enum(int_value)

Question:

I recently moved my code to a new computer at work. A basic example code is as follows (but you wouldn’t be able to run it as you can’t connect to my server – sorry that I couldn’t make it any more reproducible).

With the new computer, I get the following error:

System.ArgumentException: since Python.NET 3.0 int can not be converted to Enum implicitly. Use Enum(int_value) in method OSIsoft.AF.Asset.AFValue RecordedValue(OSIsoft.AF.Time.AFTime, OSIsoft.AF.Data.AFRetrievalMode) —> Python.Runtime.PythonException: since Python.NET 3.0 int can not be converted to Enum implicitly. Use Enum(int_value)*".

My old computer uses Spyder 4 and Python 3.7 and Python.NET 2.5.2. The new computer uses Spyder 5 and Python 3.9 and Python.NET 3.0. Because of IT restrictions, I am unable to install the same version of Spyder and Python on my computer. However, I do not think it is causing this error.

Would anyone have any idea what would cause an Enum-related problem? Thanks!

import PIconnect as PI

def pidownload(tag):
    with PI.PIServer() as server:
        point = server.search(tag)[0]
        data = point.recorded_value('-1m')
        data=data.to_frame()
    return data
tag='xxxx.pv' #confidential data tag replaced with xxxx
print(pidownload(tag))

screenshot of the error

Asked By: bitterjam

||

Answers:

Judging by the documentation for the RecordedValue function in OSIsoft’s documentation and the documentation for recorded_value in the PIConnect documentation, it seems that the Python-side function is sending an enum and the C#-side function is receiving an enum value. However, when inspecting the GitHub documentation for PIConnect, we can see that the issue is with the retrieval_mode variable, which has a default value of RetrievalMode.AUTO, which is an IntEnum. It appears that the conversion is not working properly, as I noticed this GitHub issue on their repository.

EDIT

After further inspection, it looks like your issue is an installation problem and can be fixed by reinstalling piconnect, see this question.

Answered By: Woody1193

It worked after downgrading to Python 3.8 (from 3.9) and to Python.NET 2.5 (from Python.NET 3.0). I think it simply doesn’t work for python 3.9 onwards: http://pythonnet.github.io/.

Answered By: bitterjam
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.