在 python 脚本中加载环境模块 [英] Loading environment modules within a python script

查看:24
本文介绍了在 python 脚本中加载环境模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python 脚本有没有办法加载和使用环境模块?os.system('module load xxx') 不起作用,因为它在子 shell 中执行它们(至少,我认为这就是正在发生的事情).

解决方案

我知道这个问题有点老了,但它仍然足够相关,我一直在寻找答案,所以我发布了我发现也有效的内容:

至少在 3.2.9+ 源代码中,您可以包含 python "init" 文件以获取模块的 python 函数版本:

<预><代码>>>>exec(open('/usr/local/Modules/default/init/python.py').read())>>>模块('列表')当前没有加载模块文件.>>>模块('加载','foo')>>>模块('列表')当前加载的模块文件:1) 富/1.0

有人告诉我早期版本可以在没有 .py 扩展名的情况下做同样的事情,但这是二手的,所以 ymmv.

替代init"文件位置(来自@lib 的评论):/usr/share/Modules/init/python.py

要与 Python 3 一起使用,需要 4.0 版或更高版本的环境模块,因为这是第一个具有无错误 Python3 兼容版本的 Python init 文件的版本.

Is there a way for a python script to load and use environment modules? os.system('module load xxx') doesn't work since it executes them in a subshell (at least, I think that's what's happening).

解决方案

I know this question's kind of old but it's still relevant enough that I was looking for the answer, so I'm posting what I found that works as well:

At least in the 3.2.9+ sources, you can include the python "init" file to get a python function version of module:

>>> exec(open('/usr/local/Modules/default/init/python.py').read())
>>> module('list')
No Modulefiles Currently Loaded.
>>> module('load','foo')
>>> module('list')
Currently Loaded Modulefiles:
  1) foo/1.0

I've been told earlier versions can do the same without the .py extension, but that's second hand, so ymmv.

Alternative "init" file location (from comment by @lib): /usr/share/Modules/init/python.py

To use with Python 3, version 4.0 or later of Environment Modules is required, as that is the first version to have a bug-free Python3-compliant version of the Python init file.

这篇关于在 python 脚本中加载环境模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆