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.
Node Explorer
Linktype
Import User Modules into Flowsheet
backlink
2021.08.27
It is possible to use Python User Module Definition in DWSIM simulation (flowsheet) by importing them.
Node Explorer
Linktype
Python User Module Definition
link
2021.08.27
To import a user module using absolute path use as below.
mylib
is a python user module to be importedmylib
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...")
mylib_path
defines the absolute path to folder contains user modulesr
in mylib_path = r'G:\DWSIM\python\import-user-modules\libs'
sys.path.append(mylib_path)
adds the module path to the python search path
Node Explorer
Linktype
Import User Module Using Relative Path
backlink
2021.08.27
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 importedmylib
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...")
fullpath = Flowsheet.FilePath
home = fullpath.rsplit(delimiter,1)[0]
. Note that the delimiter on windows is \
which is escaped here.libs
and utils
through sys.path.append(home +r"\libs")
sys.path.append(home +r"\utils")
utils
and libs
can be imported.constants
user module is importedmylib
is imported
Node Explorer
Linktype
Import User Module Using Absolute Path
link
2021.08.27
Hot reload means to relaod the user module every time you run the code!
importlib
standrad module lets you reload the moduleHere 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...")
importlib.reload
python.net
.