繁体   English   中英

Shell 脚本:从 shell 脚本中执行 python 程序

[英]Shell Script: Execute a python program from within a shell script

我试过用谷歌搜索答案,但没有运气。

我需要使用我的作品超级计算机服务器,但要运行我的 python 脚本,它必须通过 shell 脚本执行。

例如我希望job.sh执行python_script.py

如何实现?

只需确保 python 可执行文件在您的 PATH 环境变量中,然后添加到您的脚本中

python path/to/the/python_script.py

细节:

  • 在文件job.sh中,把这个
#!/bin/sh python python_script.py
  • 执行此命令以使脚本可以为您运行: chmod u+x job.sh
  • 运行它: ./job.sh

方法 1 - 创建一个 shell 脚本:

假设你有一个 python 文件hello.py创建一个名为job.sh的文件,其中包含

#!/bin/bash
python hello.py

将其标记为可执行使用

$ chmod +x job.sh

然后运行它

$ ./job.sh

方法 2(更好) - 使 python 本身从 shell 运行:

修改您的脚本hello.py并将其添加为第一行

#!/usr/bin/env python

将其标记为可执行使用

$ chmod +x hello.py

然后运行它

$ ./hello.py

恕我直言,写作

python /path/to/script.py

是大错特错,尤其是这几天。 哪个蟒蛇? python2.6? 2.7? 3.0? 3.1? 大多数情况下,您需要在 python 文件的 shebang 标签中指定 python 版本。 我鼓励使用

#!/usr/bin/env python2 #or python2.6 or python3 or even python3.1
为了兼容性。

在这种情况下,让脚本可执行并直接调用它会更好:

\n #!/bin/bash\n\n /path/to/script.py\n

这样你需要的python版本就只写在一个文件里了。 现在大多数系统同时拥有 python2 和 python3 ,碰巧符号链接python指向python3 ,而大多数人期望它指向python2

将以下程序另存为print.py

#!/usr/bin/python3
print('Hello World')

然后在终端类型中:

chmod +x print.py
./print.py

你应该能够调用它作为python scriptname.py例如

# !/bin/bash

python /home/user/scriptname.py 

还要确保脚本具有运行权限。

您可以使用chmod u+x scriptname.py使其可执行。

这对我最有效:在脚本顶部添加:

#!c:/Python27/python.exe

(C:\\Python27\\python.exe 是我机器上 python.exe 的路径)然后通过以下方式运行脚本:

chmod +x script-name.py && script-name.py

这对我有用:

  1. 创建一个新的 shell 文件作业。 因此,让我们说: touch job.sh并添加命令以运行 python 脚本(您甚至可以向该 python 添加命令行参数,我通常预定义我的命令行参数)。

    chmod +x job.sh

  2. job.sh添加以下 py 文件,比方说:

    python_file.py argument1 argument2 argument3 >> testpy-output.txt && echo "Done with python_file.py"

    python_file1.py argument1 argument2 argument3 >> testpy-output.txt && echo "Done with python_file1.py"

job.sh 的输出应如下所示:

Done with python_file.py

Done with python_file1.py

我通常在必须运行具有不同参数的多个 python 文件时使用它,预定义。

注意:只需快速了解这里发生的事情:

 python_file.py argument1 argument2 argument3 >> testpy-output.txt && echo "completed with python_file.py" .

  • 这里 shell 脚本将运行文件python_file.py并在运行时向 python 文件添加多个命令行参数
  • 这并不一定意味着您还必须传递命令行参数。
  • 你可以像这样使用它: python python_file.py ,简单明了。 接下来, >>将打印此 .py 文件的输出并将其存储在 testpy-output.txt 文件中。
  • &&是一个逻辑运算符,只有在上述内容成功执行后才会运行,并且作为可选的回声“用 python_file.py 完成”将在运行时回显到您的 cli/终端。

我用这个,它工作正常

#/bin/bash
/usr/bin/python python python_script.py

由于其他帖子说明了一切(我在寻找以下内容时偶然发现了这篇文章)。
这是一种如何从另一个 python 脚本执行 python 脚本的方法:

蟒蛇2:

execfile("somefile.py", global_vars, local_vars)

蟒蛇3:

with open("somefile.py") as f:
    code = compile(f.read(), "somefile.py", 'exec')
    exec(code, global_vars, local_vars)

您可以通过提供其他一些sys.argv来提供 args

在这里,我演示了一个在 shell 脚本中运行python脚本的shell For different purposes you may need to read the output from a shell command , execute both python script and shell command within the same file.

要从python执行 shell 命令,请使用os.system()方法。 要从shell command中读取 output,请使用os.popen()

以下是一个示例,它将 grep 所有进程在其中包含文本sample_program.py 然后在收集进程ID(使用python)后,它将全部杀死。

#!/usr/bin/python3
import os

# listing all matched processes and taking the output into a variable s
s = os.popen("ps aux | grep 'sample_program.py'").read()
s = '\n'.join([l for l in s.split('\n') if "grep" not in l]) # avoiding killing the grep itself
print("To be killed:")
print(s)

# now manipulating this string s and finding the process IDs and killing them
os.system("kill -9 " + ' '.join([x.split()[1] for x in s.split('\n') if x]))

参考:

如果您有 bash 脚本并且您需要在其中运行 python3 脚本(带有外部模块),我建议您将 bash 脚本指向您的 python 路径,如下所示。

#!/usr/bin/env bash

-- bash code --

/usr/bin/python3 your_python.py

-- bash code --

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM