question

rub avatar image
0 Likes"
rub asked Jason Lightfoot commented

Calling python script (not located in the same directory as the model)

Hello everyone. I am trying to call a python script from flexsim. The idea is getting some data from that python script and use it then in flexsim. What I want to do is to call it without being in the same directory as the model, meaning in an specific directory. Is it possible? Thanks in advance.

FlexSim 23.2.2
python connection with flexsim
· 2
5 |100000

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

Joerg Vogel avatar image Joerg Vogel commented ·

@rub, in calling a python script there are two programming languages involved: Python and C++. Each of them have a syntax to build a path to a file. I would think, that you can test each of them in your model and answer your question yourself.
At least there is an information that a python module can be in a subdirectory of a model file. Now it would be interesting to know if a traversal to an upper directory is possible by relative path description, too. And it would be interesting to investigate, if an absolute path description of a root directory is allowed.

0 Likes 0 ·
Jason Lightfoot avatar image Jason Lightfoot ♦ commented ·

Hi @rub, was Jason Lightfoot's answer helpful? If so, please click the "Accept" button at the bottom of their answer. Or if you still have questions, add a comment and we'll continue the conversation.

If we haven't heard back from you within 3 business days we'll auto-accept an answer, but you can always comment back to reopen your question.

0 Likes 0 ·

1 Answer

·
Jason Lightfoot avatar image
0 Likes"
Jason Lightfoot answered Jason Lightfoot commented

Add an empty text file to the directory containing your python module/script and name it __init__.py

In your model directory create a symbolic link to the directory containing your script. For example I put my 'central' python script in FlexSim's libraries folder and then ran this in the model folder via a command prompt:

mklink /D FlexSimLibs "C:/Program Files/FlexSim 2023/libraries"

Then the external python is set up using something like:

/**external python: */ /**/"FlexSimLibs.myPythonModule"/**/
/** \nfunction name:*/ /**/"myPythonFunction"/**/

where FlexSimLibs is the name of the symbolic link.

· 2
5 |100000

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

rub avatar image rub commented ·

Thanks Jason, but I don't get the first part:

1712124798760.png

What do you mean by "symbolic link"? How am I supposed to run the script? Via FlexSim or CMD or Python?

Could you give me more detail regarding that part? Thanks!

0 Likes 0 ·
1712124798760.png (12.3 KiB)
Jason Lightfoot avatar image Jason Lightfoot ♦ rub commented ·
Create the symbolic link via the command prompt (dos)
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

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