---
title: 'How to write your modulefile ?'
tags: Guide, TWNIA3, TW
---
<p style="text-align:right;">更新日期:Mar. 22, 2024</p>
如何加入自訂的Environment Module
===
## Table of Contents
[TOC]
## What is Environment Module
Environment Module是可透過預先寫好的modulefile,動態設定變更或刪除環境變數的工具,目前主要有[Environment Module](https://modules.readthedocs.io/en/latest/) 與 [Lmod](https://lmod.readthedocs.io/en/latest/)較為常用。
其特點如下:
1. 動態快速設定shell環境變數,如PATH、LD_LIBRARY_PATH、MANPATH、CC…
2. 可依設定檔單獨或同時多個依序讀取
3. 設定檔可分享給其他人
modulefile範例
---
台灣杉三號使用Lmod作為modulefile管理工具,支援Tcl與Lua語法寫的modulefile。
以下以demo_user安裝一個軟體SoftABC version 1.0 到\~demo_user下,安裝時prefix設定為~demo_user/pkg(實際目錄為/home/demo_user/pkg)、libdir為prefix/lib、mandir為prefix/man為例,列出此二種語法的範例,實際請**擇一即可**。
### Tcl
```gherkin=
#%Module1.0##################################################################
##
## SoftABC version 1.0 modulefile
##
## Set the paths
set version 1.0
set prefix "/home/demo_user/pkg"
## Initialize module commands
module-whatis "Sets up environment for SoftABC version $version"
## Set environment variables
setenv SOFTABC_ROOT $prefix
prepend-path PATH $prefix/bin
prepend-path LD_LIBRARY_PATH $prefix/lib
prepend-path MANPATH $prefix/man
prepend-path PKG_CONFIG_PATH $prefix/lib/pkgconfig
## Provide information
module-version "SoftABC" $version
```
<!--
> Read more about Gherkin here: https://docs.cucumber.io/gherkin/reference/
-->
#### 解析
* 起始列以 #%Module 開始,表示這是Environment Module格式的modulefile, 1.0是modulefile格式的版本,目前也沒有其他版次,所以1.0可寫也可省略。
* #之後表示註解
* set
語法:set 變數 值
該變數值只於此modulefile中有效
例: set version 1.0,將"version" 設為1.0
* module-whatis
語法: module-whatis "xxxxx"
執行module whatis 顯示之說明
* setenv
語法: setenv 環境變數 值
setenv設定之環境變數值可傳遞給shell,相當於sh/bash的 export 環境變數=值 或csh/tcsh 的setenv 變數 值
* prepend-path
語法:prepend-path 環境變數 值
將"值"放到"環境變數"的前方,讓系統優先搜尋,如 prepend-path PATH \$prefix/bin,即等於 bash的export PATH=/home/demo_user/pkg/bin:$PATH
* 詳細內容請參考[官方網站](https://modules.readthedocs.io/en/latest/modulefile.html)
### Lua語法
```gherkin=
-- -*- lua -*-
-- vim:ft=lua:et:ts=4
whatis("Sets up environment for SoftABC version 1.0")
local version = "1.0"
local prefix = "/home/demo_user/pkg"
-- Initialize module commands
help([[
Sets up environment for SoftABC version ]] .. version)
-- Set environment variables
setenv("SOFTABC_ROOT", prefix)
prepend_path("PATH", pathJoin(prefix, "bin"))
prepend_path("LD_LIBRARY_PATH", pathJoin(prefix, "lib"))
prepend_path("MANPATH", pathJoin(prefix, "man"))
prepend_path("PKG_CONFIG_PATH", pathJoin(prefix, "lib/pkgconfig"))
-- Provide information
if (mode() == "load") then
LmodMessage("SoftABC version " .. version .. " has been loaded")
end
```
#### 解析
* \-\- 表示註解
* whatis
語法: whatis("xxxxx")
執行module whatis 顯示之說明
* local
語法:local 變數 = 值;與tcl 中的set功用相同
宣告為local變數,意即該變數值只於此modulefile中有效,不會傳給shell
例:
local prefix = "/home/demo_user/pkg",將prefix 宣告為local的變數,並設定為/home/demo_user/pkg,在此modulefile中之後的prefix最終都會解析為/home/demo_user/pkg
* help
語法: help([[敘述]])
執行module help 顯示之敘述說明
* setenv
語法: setenv("環境變數",local變數) or setenv("環境變數","值")
setenv設定之環境變數值可傳遞給shell,相當於sh/ksh/bash的 export 環境變數=值 或csh/tcsh 的setenv 環境變數 值
* prepend-path
語法:prepend-path("環境變數",值)
將"值"放到"環境變數"的前方,讓系統優先搜尋。前述的"值",可為local變數,亦可為絕對路徑;只要加入的"值"為非變數,如絕對路徑,請加上""。
* 詳細內容請參考[官方網站](https://lmod.readthedocs.io/en/latest/#introduction-to-lmod)
<!--
> Read more about sequence-diagrams here: http://bramp.github.io/js-sequence-diagrams/
-->
建立自己的module
---
1. 確認已安裝好程式或函式庫
2. 建立modulefile儲存目錄,如mkdir ~/module
3. 依上述的說明選擇以Tcl或Lua語法,撰寫modulefile。以上述說明為例,將以Tcl寫的modulefile儲存為\~/module/SoftABC;若是以Lua寫的modulefile,請以.lua作為檔案名稱結尾,如~/module/SoftABC.lua。Tcl 或 Lua的語法,擇一即可,在台灣杉三號上都可以使用。
4. 執行 module use \~/module,把\~/module加到\$MODULEPATH中;亦可於~/.bashrc中加入 export MODULEPATH ~/module:$MODULEPATH,之後登入系統時,自動加入您的個人module路徑。
5. 執行 module avail,就可以看到您的module。
以下為示範範例
```
[demo_user@lgn302 ~]$ mkdir ~/module
[demo_user@lgn302 ~]$ vim ~/module/SoftABC.lua
[demo_user@lgn302 ~]$ ml use ~/module
[demo_user@lgn302 ~]$ ml av
---------------------------------------------- /home/demo_user/module ----------------------------------------------
SoftABC
------------------------------------------------ Software Modules -------------------------------------------------
abaqus ansoft biology gaussian lsdyna molpro orea schrodinger
adf ansys chem lammps matlab nbo qchem tcad
------------------------------------------- Original and other Modules --------------------------------------------
miniconda3/24.1.2 old-module python/3.12.2 rcec twcc
---------------------------------------- Compiler and MPI library Modules -----------------------------------------
gcc/8.5.0 (D) gcc/11.4.0 (I) go/1.19.1 intel/2020u4 intel/2023
gcc/9.5.0 (I) gcc/12.3.0 (I) go/1.21.1 intel/2021 intel/2024 (D)
gcc/10.5.0 (I) gcc/13.2.0 (I) go/1.22.0 (D) intel/2022
---------------------------------------- System and serial library Modules ----------------------------------------
libfabric/1.20.0 singularity/4.1.1 xpmem
-------------------------------------------------- Core Modules ---------------------------------------------------
lmod settarg
[demo_user@lgn302 ~]$ ml show SoftABC
---------------------------------------------------------------------------------------------------------------
/home/demo_user/module/SoftABC.lua: <-- modulefile實際位置
---------------------------------------------------------------------------------------------------------------
whatis("Sets up environment for SoftABC version 1.0")
help([[ Sets up environment for SoftABC version 1.0]])
setenv("SOFTABC_ROOT","/home/demo_user/pkg")
prepend_path("PATH","/home/demo_user/pkg/bin")
prepend_path("LD_LIBRARY_PATH","/home/demo_user/pkg/lib")
prepend_path("MANPATH","/home/demo_user/pkg/man")
prepend_path("PKG_CONFIG_PATH","/home/demo_user/pkg/lib/pkgconfig")
```
<!--
> Read more about mermaid here: http://mermaid-js.github.io/mermaid/
## Appendix and FAQ
:::info
**Find this document incomplete?** Leave a comment!
:::
-->