简体   繁体   English

当我尝试通过 python 脚本执行 shell 命令时出现错误“找不到模块”

[英]I'm getting an error “module not found ” when I'm trying to execute shell commands through a python script

When the below statement is executed执行以下语句时

out1 = subprocess.run("module load python",shell = True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)

This error is generated.生成此错误。

/bin/sh: module:command not found. /bin/sh:模块:命令未找到。

I want to execute shell commands using a python script and this above statement is not working, but when I execute the same statement on the shell then it is working fine and no error is generated.我想使用 python 脚本执行 shell 命令,上面的语句不起作用,但是当我在 shell 上执行相同的语句时,它工作正常并且没有生成错误。

module is not an external command but a function to define in the shell/script language used. module不是外部命令,而是 function 以使用的 shell/脚本语言定义。 So to enable the module command within a Python script, you need to initialize it with the following code:因此,要在 Python 脚本中启用module命令,您需要使用以下代码对其进行初始化:

import os
exec(open('/usr/share/Modules/init/python.py').read())

Replace /usr/share/Modules/init , by the location where the python.py script (from the environment-modules software) is installed on your machine./usr/share/Modules/init替换为您机器上安装python.py脚本(来自 environment-modules 软件)的位置。

Once initialized, you can call the module function from your script:初始化后,您可以从脚本中调用module function:

module('load', 'modulefile')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在做密码挑战 5。尝试在 kali linux 中运行此脚本时,第 5 行出现语法错误 - I'm doing the cryptopals challenge 5. I'm getting a syntax error in line 5 when trying to run this script in kali linux 尝试在 Ubuntu 16.04 虚拟机中安装“xgboost”时出现“找不到命令”错误 - I'm getting a "command not found" error when trying to install "xgboost" in an Ubuntu 16.04 Virtual Machine 尝试安装scipy时出现奇怪的错误 - I'm getting a weird error when trying to install scipy 我正在尝试实现:np.maximum.outer in Python 3x 但我收到此错误:NotImplementedError - I´m trying to implement: np.maximum.outer in Python 3x but I´m getting this error: NotImplementedError Python:我正在尝试从模块2导入一个实例,并通过模块1中的类运行它 - Python: I'm trying to import an instance from module 2 and run it through a class in module 1 为什么我在 python 中运行此代码时出现输入错误? - Why i m getting Ran out of input Error when i m running this code in python? 我正在尝试编写一个 python 脚本来使用 Mapreducer 读取 csv 并且我收到错误 ValueError: too many values to unpack (expected 2) - I'm trying to write a python script to read a csv using Mapreducer and im getting the error ValueError: too many values to unpack (expected 2) 我正在尝试用 python 制作游戏,首先是让角色移动,但我一直遇到同样的错误 - I'm trying to make a game in python, starting by getting the character moving, but i keep getting the same error 我正在尝试使用python请求模块下载Excel工作表并获取垃圾输出 - I'm trying to get an excel sheet downloaded using python requests module and getting junk output 当我尝试通过cmd运行python测试脚本时出现未找到模块错误 - Getting module not found error when i try to run my python test scripts through cmd
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM