Python User Module Definition

27th August 2021 at 11:26am

Ref: Python docs

A module is a file containing Python definitions and statements. The file name is the module name with the suffix .py appended.

  • A python module is stored physically on the disk
  • A python module can contains
    • solvers
    • utility routins
    • constants
    • any other scripts

Node Explorer

Linktype
Import User Modules into Flowsheet backlink 2021.08.27

|

Import User Modules into Flowsheet

27th August 2021 at 11:26am

It is possible to use Python User Module Definition in DWSIM simulation (flowsheet) by importing them.

  • Importing user modules is supported by
    • Ironpython
    • Python.net
  • Script Manager lets to import user module
  • Custom unit operation (e.g. python script) lets to import user module

Node Explorer

Linktype
Python User Module Definition link 2021.08.27

|

Import User Module Using Absolute Path

27th August 2021 at 11:47am

To import a user module using absolute path use as below.

  • mylib is a python user module to be imported
  • mylib is located in G:\DWSIM\python\import-user-modules\libs
import sys
mylib_path = r'G:\DWSIM\python\import-user-modules\libs'

# add new path to python search path
sys.path.append(mylib_path)

# Print the path
Flowsheet.WriteMessage(str(sys.path))

# Import a module located in new path G:\DWSIM\python\read-write-object-data\libs
import mylib

# Use module for testing
x = mylib.double(2)
Flowsheet.WriteMessage(str(x))

Flowsheet.WriteMessage("finished successfully...")
Remarks
the path of user module shall be added to python search path
the variable mylib_path defines the absolute path to folder contains user modules
the string variable gets the path as raw string. note to r in mylib_path = r'G:\DWSIM\python\import-user-modules\libs'
the sys.path.append(mylib_path) adds the module path to the python search path
next you can import any user module located in the added path

Node Explorer

Linktype
Import User Module Using Relative Path backlink 2021.08.27

|

Import User Module Using Relative Path

27th August 2021 at 12:02pm

Using relative path is a powerful tool lets you create and keep your python scripts in a sub folder related to the location of DWSIM simulation file and this makes your DWSIM simulation project portable.

For importing module using absolute path see Import User Module Using Absolute Path.

To import a user module using absolute path use as below.

  • mylib and constants are two python user modules to be imported
  • mylib is located in G:\DWSIM\python\import-user-modules\libs
  • constants is located in G:\DWSIM\python\import-user-modules\libs
# get the full path of current runing simulation (e.g. flowsheet)
fullpath = Flowsheet.FilePath

# print the full path
Flowsheet.WriteMessage(fullpath)

# retrive the home directory (no file name)
delimiter = '\\' # this is for Windows, use proper delimiter in your system
home = fullpath.rsplit(delimiter,1)[0]

Flowsheet.WriteMessage(home)

# add relative path now, NOTE to r before the string
import sys
sys.path.append(home +r"\libs")
sys.path.append(home +r"\utils")

# TEST I
import constants
R = constants.Rg
Flowsheet.WriteMessage(str(R))

# TEST II
from mylib import double as f
from mylib import triple as g
u = f(2)
w = g(2)
Flowsheet.WriteMessage(str(u))
Flowsheet.WriteMessage(str(w))

Flowsheet.WriteMessage("finished...")
Remarks
the path of user modules shall be added to python search path
the full path of current simulation file is retrived using fullpath = Flowsheet.FilePath
the home directory is calculates by removing the file name from the path e.g. home = fullpath.rsplit(delimiter,1)[0]. Note that the delimiter on windows is \ which is escaped here.
this code adds two different paths to python search path e.g libs and utils through
sys.path.append(home +r"\libs")
sys.path.append(home +r"\utils")
now every user module in utils and libs can be imported.
as example i, constants user module is imported
as example ii mylib is imported

Node Explorer

Linktype
Import User Module Using Absolute Path link 2021.08.27

|

Hot Reload User Modules

27th August 2021 at 12:21pm

Hot reload means to relaod the user module every time you run the code!

  • If you import a module into a DWSIM simulation and at the same time change it (r.g. modifiy it adding or removing part of codes) DWSIM will not detect the chage
  • This is an issue in Python, that means when you import a module, python do not detetcs the change except you restart the python
  • To address this use one needs to reload the imported module after change
  • Use the importlib standrad module lets you reload the module

Example

Here mylib is a user module and is reloaded everytime code runs. So, every change in the user module will be reflected in DWSIM simulation.

# module and related path
fullpath = Flowsheet.FilePath
# retrive the home directory (no file name)
home = fullpath.rsplit('\\',1)[0]
import sys
# add module path to python search path
sys.path.append(home +r"\libs")
# first import user module
import mylib
# next import the standrad importlin
import importlib
# now relaod the user module
importlib.reload(mylib)

# Test
from mylib import double as f
from mylib import triple as g

u = f(2)
w = g(2)
Flowsheet.WriteMessage(str(u))
Flowsheet.WriteMessage(str(w))

Flowsheet.WriteMessage("finished...")
Remarks
  • first import the user module
  • next reload it using importlib.reload

It seems if the Ironpython interpreter is used, DWSIM reload the user module and this solution is not required. So only use with python.net.

|