Currently we have a few python modules which have their own modulefile, e.g. to make use of the foo python module, one would need to issue the commands
module load python
module load foo
python
import foo
This however, does not work using envmodules; e.g.
module load python
python
import envmodules
envmodules.load('foo')
import foo
will fail. This is understandable as generally a modulefile like for foo will only update the PYTHONPATH environmental variable (and there is no mechanism within env modulefiles to modify python data structures directly, as modulefiles are interpreter neutral), and the PYTHONPATH variable is only used by python when the interpreter first starts, which is too late see modules loaded by envmodules.
I would like to propose that envmodules detect any changes to PYTHONPATH, and make comparable changes to sys.path. This would allow the paths added to PYTHONPATH when
envmodules.load('foo') is run to be able available in the current python session, so a subsequent import foo will work.
I will submit a pull request to do this in a somewhat crude manner. It defines three new routines:
get_auto_fix_sys_path(), set_auto_fix_sys_path(): accessor/mutator for auto_fix_sys_path boolean data member. If set, the _modulecmd() routine will automatically invoke fix_sys_path().
The default is currently false (which means the existing behavior is default).
fix_sys_path(): This compares PYTHONPATH before and after the modulecmd calls, and attempts to update sys.path accordingly. This is somewhat crude for now; if simply notes paths which were in PYTHONPATH and no longer are, and deletes them from sys.path, and notes paths that were added to PYTHONPATH and adds them to sys.path. It is crude in that added paths are simply prepended to the front of sys.path --- more properly it should try to replicate the sys.path that would have been generated if python was invoked with the given PYTHONPATH, which means it should do a better job of keeping the system paths first, and maintaining the same order among all of the PYTHONPATH paths. But I figure this crude approach works for many cases, and is an improvement on the existing behavior (no modification of sys.path), and refinements can come later.
Currently we have a few python modules which have their own modulefile, e.g. to make use of the foo python module, one would need to issue the commands
This however, does not work using envmodules; e.g.
will fail. This is understandable as generally a modulefile like for foo will only update the PYTHONPATH environmental variable (and there is no mechanism within env modulefiles to modify python data structures directly, as modulefiles are interpreter neutral), and the PYTHONPATH variable is only used by python when the interpreter first starts, which is too late see modules loaded by envmodules.
I would like to propose that envmodules detect any changes to PYTHONPATH, and make comparable changes to sys.path. This would allow the paths added to PYTHONPATH when
envmodules.load('foo')is run to be able available in the current python session, so a subsequentimport foowill work.I will submit a pull request to do this in a somewhat crude manner. It defines three new routines:
get_auto_fix_sys_path(), set_auto_fix_sys_path(): accessor/mutator for auto_fix_sys_path boolean data member. If set, the _modulecmd() routine will automatically invoke fix_sys_path().
The default is currently false (which means the existing behavior is default).
fix_sys_path(): This compares PYTHONPATH before and after the modulecmd calls, and attempts to update sys.path accordingly. This is somewhat crude for now; if simply notes paths which were in PYTHONPATH and no longer are, and deletes them from sys.path, and notes paths that were added to PYTHONPATH and adds them to sys.path. It is crude in that added paths are simply prepended to the front of sys.path --- more properly it should try to replicate the sys.path that would have been generated if python was invoked with the given PYTHONPATH, which means it should do a better job of keeping the system paths first, and maintaining the same order among all of the PYTHONPATH paths. But I figure this crude approach works for many cases, and is an improvement on the existing behavior (no modification of sys.path), and refinements can come later.