Menu

Import external python packages

2019-10-29
2022-08-12
  • Patrick Veiltl

    Patrick Veiltl - 2019-10-29

    Hey guys,

    I have a problem concerning the import of packages like numpy and matplotlib.pyplot into Scripts in the newest version of DWSIM.
    I already tried to reference the file location of python.exe at Global Settings>Miscelaneous>Python Settings>binaries path (I use Spyder in Anaconda).
    Importing by appending the path to system does also not work for me.

    import sys
    sys.path.append("C:Path...")
    

    I even attempted to copy all available packages from Anaconda directly to C:\Users\user1\AppData\Local\DWSIM5\Lib
    but all I got was an error while calling numpy.

    ImportError: 
    Importing the multiarray numpy extension module failed.  Most
    likely you are trying to import a failed build of numpy.
    If you're working with a numpy git repo, try `git clean -xdf` (removes all
    files not under version control).  Otherwise reinstall numpy.
    
    Original error was: cannot import multiarray from numpy.core
    

    Numpy works just fine in spider, so I don't see any fault with the version of numpy I run.

    What is the correct way to import these packages?

     
  • Daniel Medeiros

    Daniel Medeiros - 2019-10-29

    IronPython cannot import external packages other than the standard libraries included with DWSIM (lib folder). This is why I've included Python.NET as an alternative interpreter.

    The compiled version of Python.NET included with DWSIM requires a Python 3.6 environment. To use it in DWSIM, you must set the Python path on General Settings to the folder where python36.dll is located.

    I recommend installing a portable Python package like WinPython, specifically this one: https://sourceforge.net/projects/winpython/files/WinPython_3.6/3.6.5.0/WinPython64-3.6.5.0Qt5.exe/download

    The syntax for .NET/DWSIM integration is a little bit different when compared to IronPtyhon. Check the Cantera sample and you'll spot the differences.

    Regards
    Daniel

     
  • Daniel Medeiros

    Daniel Medeiros - 2019-10-29

    While IronPython works like an internal python interpreter, exclusive for DWSIM, Python.NET integrates an external Python environment with DWSIM so you can use DWSIM objects (flowsheet, unit operations, streams, spreadsheet) and get/set their properties dynamically from memory as if you were running it on an external python editor.

    More Python.NET info can be found at https://github.com/pythonnet/pythonnet

     
  • Daniel Medeiros

    Daniel Medeiros - 2019-10-29

    For instance, what I did in this article => https://www.linkedin.com/pulse/integrating-chemical-process-simulator-tensorflow-daniel-medeiros/

    would not be possible with IronPython, as there is no way to import TensorFlow libraries. In this case, TensorFlow, numpy, scipy and all dependencies are installed in an external Python 3.6 environment which is called by Python.NET and loaded in DWSIM each time the script is run.

     
  • Patrick Veiltl

    Patrick Veiltl - 2019-10-30

    I've taken a look at your article about simulating tensor flows and downloaded WinPython. It was very helpfull advice.

    I then opened "Biodiesel Combustion (Cantera)", linked the path to python36.dll in global settings as you can see on the screenshot and ran the script to see if it works.
    It gave me this error:
    Combustion (Cantera): Unable to load DLL 'python36.dll': The specified module could not be
    found. (Exception from HRESULT: 0x8007007E)

    Do you have any idea why the program has problems accessing python36.dll?

     
  • Patrick Veiltl

    Patrick Veiltl - 2019-10-30

    Where should I insert

    dumpbin /DEPENDENTS my.dll
    

    It doesn't lead me very far when typed into the script editor, because the program searches python36.dll before accessing the script, so I still get the same old error.

     
  • Daniel Medeiros

    Daniel Medeiros - 2019-10-30

    You must run this command from cmd.

    You must determine the version of Visual C++ Redistributable required by python36.dll and install it accordingly.

     
  • Dennis Arigbe

    Dennis Arigbe - 2022-08-12

    Hello together,
    i tried also to simulate the example which Daniel did for importing tensorflow.
    But i have a very strange behaviour which i can´t explain myself.
    I can run the Script in the Script Manager one time after i opened DWSIM. After that everytime i run the script the error "a bytes-like object is required, not str" appears. Does anybody had the same problem?
    I have tried it with python version 3.8.0 and Python 3.9.4 and my DWSIM version is 8.0.4
    Thanks in advance.

     
    • Daniel Medeiros

      Daniel Medeiros - 2022-08-12

      I also have this error from time to time and I still can't figure out why it happens.

       

Log in to post a comment.