From 001e266e6def011495945089cc4367f4f0b91885 Mon Sep 17 00:00:00 2001 From: Byron Boulton Date: Mon, 10 Nov 2025 09:54:28 -0500 Subject: [PATCH 1/7] Make it more Pythonic to import env_modules.module Renaming python.py to env_modules.py. On Linux, symlink python.py to point to env_modules.py for backward compatibility. Windows doesn't typically support unix-style symlinks, but we don't need to worry about that since the Windows installations of Environment Modules never had python.py. Signed-off-by: Byron Boulton --- init/.gitignore | 1 + init/Makefile | 5 +++-- init/{python.py.in => env_modules.py.in} | 1 - 3 files changed, 4 insertions(+), 3 deletions(-) rename init/{python.py.in => env_modules.py.in} (97%) diff --git a/init/.gitignore b/init/.gitignore index e58033ea0..351ec7127 100644 --- a/init/.gitignore +++ b/init/.gitignore @@ -6,6 +6,7 @@ /tcsh /perl.pm /python.py +/env_modules.py /ruby.rb /lisp /cmake diff --git a/init/Makefile b/init/Makefile index 19c51a258..1a679eed9 100644 --- a/init/Makefile +++ b/init/Makefile @@ -11,7 +11,7 @@ INSTALL_DATA = $(INSTALL) -m 644 SH_LIKE = sh ksh bash zsh profile.sh CSH_LIKE = csh tcsh tcsh_completion profile.csh -OTHER = perl.pm python.py ruby.rb lisp tcl fish cmake r.R pwsh.ps1 +OTHER = perl.pm env_modules.py ruby.rb lisp tcl fish cmake r.R pwsh.ps1 # example modulefiles to install EXAMPLE_MODFILES_SRCDIR := ../share/modulefiles @@ -246,6 +246,7 @@ endif $(INSTALL_DIR) '$(DESTDIR)$(zshcompletiondir)' $(INSTALL_DIR) '$(DESTDIR)$(modulefilesdir)' $(INSTALL_DATA) $(ALL_SHELLS) '$(DESTDIR)$(initdir)/' + ln -s env_modules.py '$(DESTDIR)$(initdir)/python.py' ifeq ($(windowssupport),y) $(INSTALL_DATA) cmd.cmd '$(DESTDIR)$(initdir)/' endif @@ -324,7 +325,7 @@ ifeq ($(VERBOSE),1) V = 1 endif # let verbose by default the install/clean/test and other specific non-build targets -$(V).SILENT: sh ksh bash zsh csh tcsh fish perl.pm python.py ruby.rb lisp tcl \ +$(V).SILENT: sh ksh bash zsh csh tcsh fish perl.pm env_modules.py ruby.rb lisp tcl \ cmake r.R bash_completion tcsh_completion zsh-functions/_module profile.sh \ profile.csh modulespath initrc $(EXAMPLE_MODFILES_SRCDIR)/modules \ $(EXAMPLE_MODFILES_SRCDIR)/version pwsh.ps1 diff --git a/init/python.py.in b/init/env_modules.py.in similarity index 97% rename from init/python.py.in rename to init/env_modules.py.in index f68b1803d..fa46d025b 100644 --- a/init/python.py.in +++ b/init/env_modules.py.in @@ -1,4 +1,3 @@ -from __future__ import print_function import os, sys, re, subprocess # define modules runtime quarantine configuration From c94533ee32dea1e416bfb642515a9821c5c67e5a Mon Sep 17 00:00:00 2001 From: Byron Boulton Date: Mon, 10 Nov 2025 13:59:12 -0500 Subject: [PATCH 2/7] Document initialization of PYTHONPATH Signed-off-by: Byron Boulton --- INSTALL.rst | 19 +++++++++++++++++++ doc/source/module.rst | 14 ++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/INSTALL.rst b/INSTALL.rst index 9322b9d1e..7df6aac68 100644 --- a/INSTALL.rst +++ b/INSTALL.rst @@ -504,6 +504,15 @@ instance :instopt:`--disable-set-manpath<--enable-set-manpath>`): .. versionadded:: 4.2 +.. instopt:: --enable-append-pythonpath + + Append rather prepend init directory to the PYTHONPATH environment variable + when the :instopt:`--enable-set-pythonpath` option is enabled. (default=no) + + .. only:: html + + .. versionadded:: 5.7 + .. instopt:: --enable-auto-handling Set modulecmd.tcl to automatically apply automated modulefiles handling @@ -799,6 +808,16 @@ instance :instopt:`--disable-set-manpath<--enable-set-manpath>`): .. versionadded:: 4.0 +.. instopt:: --enable-set-pythonpath + + Prepend init directory defined by the :instopt:`--initdir` option to the + PYTHONPATH environment variable in the shell initialization scripts. + (default=yes) + + .. only:: html + + .. versionadded:: 5.7 + .. instopt:: --enable-set-shell-startup Set when module function is defined the shell startup file to ensure that the diff --git a/doc/source/module.rst b/doc/source/module.rst index fff674790..2126696cb 100644 --- a/doc/source/module.rst +++ b/doc/source/module.rst @@ -128,10 +128,20 @@ Perl: Python: +Default installation should have added |file initdir| to the :envvar:`PYTHONPATH` environment variable allowing initialization as follows + +.. parsed-literal:: + + from env_modules import module + module("load", "modulefile", "modulefile", "...") + +Otherwise + .. parsed-literal:: - import os - exec(open("\ |initdir|\ /python.py").read(), globals()) + import sys + sys.path.insert(1, "\ |initdir|\ ") + from env_modules import module module("load", "modulefile", "modulefile", "...") Ruby: From 9ad195b6c3c6859d173e888056fef1d516f91bf8 Mon Sep 17 00:00:00 2001 From: Byron Boulton Date: Fri, 14 Nov 2025 08:33:34 -0500 Subject: [PATCH 3/7] Implement PYTHONPATH initialization Adding the initdir to PYTHONPATH allows users to "import" the "module" function in Python scripts. Signed-off-by: Byron Boulton --- Makefile | 2 ++ Makefile.inc.in | 5 +++++ configure | 13 ++++++++++++- site.exp.in | 2 ++ tcl/subcmd.tcl.in | 5 +++++ 5 files changed, 26 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 93af6cbec..7bbe5502b 100644 --- a/Makefile +++ b/Makefile @@ -479,6 +479,8 @@ sed -e 's|@prefix@|$(prefix)|g' \ -e 's|@setmanpath@|$(setsetmanpath)|g' \ -e 's|@appendmanpath@|$(setappendmanpath)|g' \ -e 's|@usemanpath@|$(setusemanpath)|g' \ + -e 's|@setpythonpath@|$(setsetpythonpath)|g' \ + -e 's|@appendpythonpath@|$(setappendpythonpath)|g' \ -e 's|@notusemanpath@|$(setnotusemanpath)|g' \ -e 's|@shellcompsource@|$(shellcompsource)|g' \ -e 's|@tcllintercmd@|$(tcllintercmd)|g' \ diff --git a/Makefile.inc.in b/Makefile.inc.in index e9ad51750..f2077a0f9 100644 --- a/Makefile.inc.in +++ b/Makefile.inc.in @@ -48,6 +48,8 @@ libdir32 := @libdir32@ # enable or not some specific definition setmanpath := @setmanpath@ appendmanpath := @appendmanpath@ +setpythonpath := @setpythonpath@ +appendpythonpath := @appendpythonpath@ setbinpath := @setbinpath@ appendbinpath := @appendbinpath@ setmodulespath := @setmodulespath@ @@ -179,3 +181,6 @@ BASENAME := @BASENAME@ RMDIR_IGN_NON_EMPTY := @RMDIR_IGN_NON_EMPTY@ SED_ERE := @SED_ERE@ +# profile init scripts put init dir on PYTHONPATH +setpythonpath := @setpythonpath@ + diff --git a/configure b/configure index 005eded75..c5b062de7 100755 --- a/configure +++ b/configure @@ -41,7 +41,7 @@ listterseoutput editor variantshortcut bashcompletiondir fishcompletiondir \ zshcompletiondir tcllinter tcllinteropts nagelfardatadir nagelfaraddons \ stickypurge uniquenameloaded abortonerror sourcecache logger loggeropts \ loggedevents conflictunload spideroutput spiderterseoutput spiderindepth \ -emacsdatadir emacsaddons requirevia" +emacsdatadir emacsaddons requirevia setpythonpath appendpythonpath" libarglist=() # flags to know if argument has been specified on command-line @@ -52,6 +52,8 @@ defloggeropts=1 prefix=/usr/local/Modules setmanpath=y appendmanpath=n +setpythonpath=y +appendpythonpath=n setbinpath=y appendbinpath=n setmodulespath=n @@ -237,6 +239,9 @@ Optional Features: --enable-FEATURE[=ARG] include FEATURE [ARG=yes] --enable-set-manpath set mandir to MANPATH in init scripts [yes] --enable-append-manpath append rather prepend mandir to MANPATH [no] + --enable-set-pythonpath set initdir to PYTHONPATH in init scripts [yes] + --enable-append-pythonpath + append rather prepend initdir to PYTHONPATH [no] --enable-set-binpath set bindir to PATH in init scripts [yes] --enable-append-binpath append rather prepend bindir to PATH [no] --enable-modulespath, --enable-dotmodulespath @@ -720,6 +725,12 @@ for arg in "$@"; do --enable-append-manpath*|--disable-append-manpath) # shellcheck disable=SC2034 appendmanpath=$(get_feature_value "$arg") ;; + --enable-set-pythonpath*|--disable-set-pythonpath) + # shellcheck disable=SC2034 + setpythonpath=$(get_feature_value "$arg") ;; + --enable-append-pythonpath*|--disable-append-pythonpath) + # shellcheck disable=SC2034 + appendpythonpath=$(get_feature_value "$arg") ;; --enable-set-binpath*|--disable-set-binpath) # shellcheck disable=SC2034 setbinpath=$(get_feature_value "$arg") ;; diff --git a/site.exp.in b/site.exp.in index 6d35a6b27..26a0934a7 100644 --- a/site.exp.in +++ b/site.exp.in @@ -32,6 +32,8 @@ set install_libdir32 "@libdir32@" set install_setmanpath "@setmanpath@" set install_appendmanpath "@appendmanpath@" set install_usemanpath "@usemanpath@" +set install_setpythonpath "@setpythonpath@" +set install_appendpythonpath "@appendpythonpath@" set install_setbinpath "@setbinpath@" set install_appendbinpath "@appendbinpath@" set install_examplemodulefiles "@examplemodulefiles@" diff --git a/tcl/subcmd.tcl.in b/tcl/subcmd.tcl.in index 9becb4eaf..b052c585e 100644 --- a/tcl/subcmd.tcl.in +++ b/tcl/subcmd.tcl.in @@ -2157,6 +2157,11 @@ proc cmdModuleAutoinit {} { @setmanpath@} } + # add Modules init directory to PYTHONPATH if enabled + @setpythonpath@if {{@initdir@} ni [split [get-env PYTHONPATH] :]} { + @setpythonpath@ @appendpythonpath@-path PYTHONPATH {@initdir@} + @setpythonpath@} + # source shell completion script if available, not installed in default # completion locations and only if shell is interactive if {[getState shell] in {@shellcompsource@} && [getState is_stderr_tty]} { From 354029f5877bbb26d3acf83a37dd464ed0c07abd Mon Sep 17 00:00:00 2001 From: Byron Boulton Date: Fri, 21 Nov 2025 19:18:10 -0500 Subject: [PATCH 4/7] Don't ignore python.py Signed-off-by: Byron Boulton --- init/.gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/init/.gitignore b/init/.gitignore index 351ec7127..3bda98b78 100644 --- a/init/.gitignore +++ b/init/.gitignore @@ -5,7 +5,6 @@ /csh /tcsh /perl.pm -/python.py /env_modules.py /ruby.rb /lisp From c0942e9392f71401b9dfea5a50901ad2bcc581cc Mon Sep 17 00:00:00 2001 From: Byron Boulton Date: Fri, 21 Nov 2025 19:19:35 -0500 Subject: [PATCH 5/7] Remove duplicate defnition of setythonpath make variable Signed-off-by: Byron Boulton --- Makefile.inc.in | 3 --- 1 file changed, 3 deletions(-) diff --git a/Makefile.inc.in b/Makefile.inc.in index f2077a0f9..b60f0cbf9 100644 --- a/Makefile.inc.in +++ b/Makefile.inc.in @@ -181,6 +181,3 @@ BASENAME := @BASENAME@ RMDIR_IGN_NON_EMPTY := @RMDIR_IGN_NON_EMPTY@ SED_ERE := @SED_ERE@ -# profile init scripts put init dir on PYTHONPATH -setpythonpath := @setpythonpath@ - From 5940a05da8fa0ee1ea687cbd69b571c9acb02c98 Mon Sep 17 00:00:00 2001 From: Byron Boulton Date: Fri, 21 Nov 2025 19:48:00 -0500 Subject: [PATCH 6/7] define the value of {set,append}pythonpath Signed-off-by: Byron Boulton --- Makefile | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Makefile b/Makefile index 7bbe5502b..367190408 100644 --- a/Makefile +++ b/Makefile @@ -231,6 +231,16 @@ ifeq ($(appendbinpath),y) else setappendbinpath := prepend endif +ifeq ($(setpythonpath),y) + setsetpythonpath := +else + setsetpythonpath := \# +endif +ifeq ($(appendpythonpath),y) + setappendpythonpath := append +else + setappendpythonpath := prepend +endif ifeq ($(setmanpath),y) setsetmanpath := else From d588c28620fe9d33482b53c79d0f69eb8d2c9016 Mon Sep 17 00:00:00 2001 From: Byron Boulton Date: Fri, 21 Nov 2025 20:11:17 -0500 Subject: [PATCH 7/7] Don't forget to uninstall python.py Signed-off-by: Byron Boulton --- init/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/init/Makefile b/init/Makefile index 1a679eed9..e86ae7eaf 100644 --- a/init/Makefile +++ b/init/Makefile @@ -277,7 +277,7 @@ ifeq ($(versioning),y) endif uninstall: - rm -f $(foreach initscript,$(ALL_SHELLS),'$(DESTDIR)$(initdir)/$(initscript)') + rm -f $(foreach initscript,$(ALL_SHELLS) python.py,'$(DESTDIR)$(initdir)/$(initscript)') ifeq ($(windowssupport),y) rm -f '$(DESTDIR)$(initdir)/cmd.cmd' endif