Is there a method for importing external Cpython modules like Numpy or SciPy to use them with ACT.?

Pernelle Marone-Hitz
Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
100 Answers 500 Comments 250 Likes First Anniversary
✭✭✭✭

Is there a method for importing external Cpython modules like Numpy or SciPy to use them with ACT.?

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭

    Numpy and SciPy are numerical libraries developed for CPython. As ACT is based on IronPython, these libraries are not supported. There are two options for using Cpython modules with ACT.
    1) Using available alternative modules by referencing to their corresponding .dll files. More specifically, math.net libraries can be imported using available .dll

    2) In the second method, to use any Cpython based modules, the subprocess module in Ironpython can be leveraged. See below example where a python_file.py is executed in cpython using the call method of subprocess module.
    import subprocess
    Python39 = r"C:\Program Files\python.exe"
    subprocess.call([Python39,python_file,args])

  • PierreLCV
    PierreLCV Member Posts: 35
    10 Comments Name Dropper
    **

    Dear Pernel,
    I am currently trying to use the subprocess module in Ironpython to use numpy but it is not working, see just after my code:

    import subprocess
    Python312 = r"C:\Users...\AppData\Local\Programs\Python\Python312\python.exe"
    subprocess.call([Python312,pyNukiNical,args])

    When i execute, i have : name 'pyNukiNical' is not defined

    I have changed my current directory (os.chdir) to place myself in the directoy where pyNukiNical.py is and when i execute
    import pyNukiNical
    it appears that it finds pyNukiNical so i am a bit confused...

    Where should i save my pyNukiNical.py to be able to launch it with subprocess.call ?

    Thank you !

    best regards,
    Pierre

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭

    Hi @PierreLCV , have you tried subprocess.run() and/or subprocess.Popen()?
    You can also check this post I shared recently which shows how to write and execute CPython code from Mechanical: https://discuss.ansys.com/discussion/2880/plot-contact-pressure-along-deformed-edge#latest

  • PierreLCV
    PierreLCV Member Posts: 35
    10 Comments Name Dropper
    **

    Dear @Pernelle Marone-Hitz ,
    Thank you for your reply ! Sounds good, i will try !

    Have a nice day,
    Pierre

  • PierreLCV
    PierreLCV Member Posts: 35
    10 Comments Name Dropper
    **

    Dear @Pernelle Marone-Hitz
    I have tried many (many !) ways to make my code working without success for now...
    First, i think that subprocess.run() is not available in IronPython... So we only have access to subprocess.call() and subprocess.Popen(). However, subprocess.run() is available in CPython (beta) but I prefer to avoid using beta versions for the time being...
    So let's focus on subprocess.call(), i was not able to find documentation on Internet about the way to use this last command to execute a python code with another python version... Maybe do you know more about it ? I have checked the code in the link you sent me but i do not find where you use subprocess (you import it but I do not find the line where you use it...)
    I think what i am trying to do is straightforward but i was not able to find anyone trying to do the same on Internet:

    import subprocess
    Python312 = r"C:\Users...\AppData\Local\Programs\Python\Python312\python.exe"
    subprocess.call([Python312,'pyNukiyama.py'])

    When i launch it, i have "2" as results so there is a mistake... Whatever i am not sure that this code is doing what i want : run pyNukiyama.py with the python.exe indicated....

    Thank you for your help,
    Best regards,
    Pierre

  • PierreLCV
    PierreLCV Member Posts: 35
    10 Comments Name Dropper
    **

    Dear Pernel,
    Finally, it works ! I can call my Cpython code in IronPython, see just after my code:

    import os
    import subprocess
    os.chdir(r"My_code_location")
    Python312 = r"C:\Users...\AppData\Local\Programs\Python\Python312\python.exe"
    subprocess.call([Python312,'pyNukiyama.py'])

    So now, the code executes well in the Ansys Mechanical console, now I'll have to find a way to retrieve the output data...

    keep you in touch !

    best regards,
    Pierre

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭

    Hi @PierreLCV , thanks for the update and sorry for the late answer, I was out of office. Glad you managed to get this to work!