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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4377109/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 15:29:18  来源:igfitidea点击:

Shell Script: Execute a python program from within a shell script

pythonshell

提问by Harpal

I've tried googling the answer but with no luck.

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

I need to use my works supercomputer server, but for my python script to run, it must be executed via a shell script.

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

For example I want job.shto execute python_script.py

例如我想job.sh执行python_script.py

How can this be accomplished?

如何做到这一点?

采纳答案by Jean-Bernard Jansen

Just make sure the python executable is in your PATH environment variable then add in your script

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

python path/to/the/python_script.py

Details:

细节:

  • In the file job.sh, put this
  • 在文件job.sh中,把这个
#!/bin/sh
python python_script.py
#!/bin/sh
python python_script.py
  • Execute this command to make the script runnable for you : chmod u+x job.sh
  • Run it : ./job.sh
  • 执行此命令以使脚本可以为您运行: chmod u+x job.sh
  • 运行 : ./job.sh

回答by Shpongle

You should be able to invoke it as python scriptname.pye.g.

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

# !/bin/bash

python /home/user/scriptname.py 

Also make sure the script has permissions to run.

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

You can make it executable by using chmod u+x scriptname.py.

您可以使用chmod u+x scriptname.py.

回答by Jo?o Víctor

Method 1 - Create a shell script:

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

Suppose you have a python file hello.py Create a file called job.shthat contains

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

#!/bin/bash
python hello.py

mark it executable using

将其标记为可执行使用

$ chmod +x job.sh

then run it

然后运行它

$ ./job.sh

Method 2 (BETTER) - Make the python itself run from shell:

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

Modify your script hello.pyand add this as the first line

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

#!/usr/bin/env python

mark it executable using

将其标记为可执行使用

$ chmod +x hello.py

then run it

然后运行它

$ ./hello.py

回答by Enrico Carlesso

Imho, writing

恕我直言,写作

python /path/to/script.py

Is quite wrong, especially in these days. Which python? python2.6? 2.7? 3.0? 3.1? Most of times you need to specify the python version in shebang tag of python file. I encourage to use

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

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

In such case, is much better to have the script executable and invoke it directly:

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

#!/bin/bash

/path/to/script.py

This way the version of python you need is only written in one file. Most of system these days are having python2 and python3 in the meantime, and it happens that the symlink pythonpoints to python3, while most people expect it pointing to python2.

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

回答by peterb

This works best for me: Add this at the top of the script:

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

#!c:/Python27/python.exe

(C:\Python27\python.exe is the path to the python.exe on my machine) Then run the script via:

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

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

回答by geekidharsh

This works for me:

这对我有用:

  1. Create a new shell file job. So let's say: touch job.shand add command to run python script (you can even add command line arguments to that python, I usually predefine my command line arguments).

    chmod +x job.sh

  2. Inside job.shadd the following py files, let's say:

    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"

  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_file.py

Done with python_file1.py

Done with python_file1.py

I use this usually when I have to run multiple python files with different arguments, pre defined.

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

Note:Just a quick heads up on what's going on here:

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

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


  • Here shell script will run the file python_file.pyand add multiple command-line argumentsat run time to the python file.
  • This does not necessarily means, you have to pass command line arguments as well.
  • You can just use it like: python python_file.py, plain and simple. Next up, the >>will print and store the output of this .py file in the testpy-output.txt file.
  • &&is a logical operator that will run only after the above is executed successfully and as an optional echo"completed with python_file.py" will be echoed on to your cli/terminal at run time.
  • 这里 shell 脚本将运行文件python_file.py并在运行时向 python 文件添加多个命令行参数
  • 这并不一定意味着,您还必须传递命令行参数。
  • 你可以像这样使用它:python python_file.py,简单明了。接下来,>>将打印此 .py 文件的输出并将其存储在 testpy-output.txt 文件中。
  • &&是一个逻辑运算符,只有在上述内容成功执行后才会运行,并且作为可选的回声“用 python_file.py 完成”将在运行时回显到您的 cli/终端。

回答by Max Hay

I use this and it works fine

我用这个,它工作正常

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

回答by Nishant Ingle

Save the following program as print.py:

将以下程序另存为print.py

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

Then in the terminal type:

然后在终端类型中:

chmod +x print.py
./print.py

回答by dCSeven

Since the other posts say everything (and I stumbled upon this post while looking for the following).
Here is a way how to execute a python script from another python script:

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

Python 2:

蟒蛇2:

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

Python 3:

蟒蛇3:

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

and you can supply args by providing some other sys.argv

你可以通过提供一些其他的来提供参数sys.argv