question

Mischa Spelt avatar image
0 Likes"
Mischa Spelt asked Mischa Spelt commented

Cannot bind to Python function

The ability to connect FlexSim and Python looks really cool so I decided to make a simple test case. Rather than controlling FlexSim from Python I first decided to see if I can call a function from FlexSim.

I created a Test.fsm model and put a simple Python script in FlexSimPy\FlexSimPy.py. But when I try to call the Python function I get

exception: Code Binding Error: could not bind to function
Node: /Tools/UserCommands/Add/code
Binding string: external python "FlexSimPy/FlexSimPy" "add"
Windows Error Code : 126

As far as I can see code 126 means missing DLL, but according to the GitHub documentation,

The PyConnector project implements the ability to bind the evaluation of nodes in FlexSim's tree to the evaluation of python functions. The project is implemented to work with python versions 3.7, 3.8, 3.9 and 3.10. The output DLLs from this project are included in the installed version of FlexSim.

(emphasis mine) and I found the DLLs in program\python so I assumed this should work out of the box.

I set the Python version in Global Preferences and made sure that python.exe is on my path. What am I missing? :-)

FlexSim 22.1.0
pythonuser command
flexsimpy.py (98 B)
test.fsm (26.1 KiB)
· 1
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Mischa Spelt avatar image Mischa Spelt commented ·
Hmm, it actually seems to work if I don't put the Python module in a subfolder of the model directory. Did I misread the docs that this should be possible?
0 Likes 0 ·

1 Answer

anthony.johnson avatar image
1 Like"
anthony.johnson answered Mischa Spelt commented

This is similar to this stack overflow question. First, you need to use dots instead of slashes as your path separators. So your module specifier should be "FlexSimPy.FlexSimPy" instead of "FlexSimPy/FlexSimPy". Second, in the folder where you put your FlexSimPy.py file, you need to have a blank file called __init__.py. Placing that file there signifies to python that this is a 'submodule' folder, so it can find the appropriate files.

I'll update the documentation to reflect this.

· 4
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Mischa Spelt avatar image Mischa Spelt commented ·
Thanks, dot-instead-of-slash did the trick!

The __init__.py does not seem to be strictly necessary but I'll include it anyway as part of a proper Python module.

0 Likes 0 ·
Guillem F avatar image Guillem F commented ·

I've followed all your steps, but I'm still having trouble binding my python function. I am getting the exact same error as Mischa, and neither the dots for subfolders nor the __init__.py seem to solve it.

The only thing I can't find is the "made sure that python.exe is on my path" part. May this be causing errors? Otherwise, am I missing something else, like downloading the github files?

Thanks in advance!


pymod.py

python-test.fsm


0 Likes 0 ·
pymod.py (34 B)
python-test.fsm (28.1 KiB)
Mischa Spelt avatar image Mischa Spelt Guillem F commented ·

I am not sure if this was needed, but I did it anyway. To test if you have it on your PATH, open a Windows Command Prompt from the start menu and type python. If it says "'python' is not recognized as an internal or external command" you can add it to the path. I pulled Python from my Visual Studio installation and had to add the following folder to the path: "C:\Program Files (x86)\Microsoft Visual Studio\Shared\Python39_64".


2 Likes 2 ·
Jason Lightfoot avatar image Jason Lightfoot ♦♦ Guillem F commented ·
Isn't the path reference just to your systems environment variables?
1 Like 1 ·