Shell脚本:在Shell脚本中执行python程序

时间:2021-08-09 04:40:17

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.

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

For example I want job.sh to execute python_script.py

例如,我想要工作。sh执行python_script.py

How can this be accomplished?

这是如何实现的呢?

8 个解决方案

#1


101  

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
  • 在文件的工作。sh,把这个
#!/bin/sh
python python_script.py
  • Execute this command to make the script runnable for you : chmod u+x job.sh
  • 执行此命令使脚本可运行:chmod u+x .sh。
  • Run it : ./job.sh
  • :运行它。/ job.sh

#2


76  

Method 1 - Create a shell script:

Suppose you have a python file hello.py Create a file called job.sh that 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:

Modify your script hello.py and add this as the first line

修改脚本你好。py加上这个作为第一行。

#!/usr/bin/env python

mark it executable using

马克可执行使用

$ chmod +x hello.py

then run it

然后运行它

$ ./hello.py

#3


6  

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

这是完全错误的,尤其是在最近。python ?python2.6吗?2.7 ?3.0 ?3.1 ?大多数情况下,您需要在python文件的shebang标记中指定python版本。我鼓励使用

#!/usr/bin/env python2 #or python2.6 or python3 or even python3.1
for compatibility.

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 python points to python3, while most people expect it pointing to python2.

这样,您需要的python版本只被写入一个文件中。现在的大多数系统同时有python2和python3,而且碰巧symlink python指向python3,而大多数人期望它指向python2。

#4


3  

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

#5


2  

you should be able to invoke it as python scriptname.py

您应该能够以python scriptname.py的形式调用它

eg

# !/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使其可执行。

EDIT: beaten to it :-p

编辑:打败它:-p

#6


2  

This works for me:

这工作对我来说:

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

    创建一个新的shell文件作业。比如说,touch job。sh和add command来运行python脚本(您甚至可以向该python添加命令行参数,我通常预先定义命令行参数)。

    chmod +x job.sh

    chmod + x job.sh

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

    内部工作。sh添加以下py文件,比如:

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

    python_file。这是一个很好的例子。txt && echo "用python_file.py完成的"

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

    python_file1。这是一个很好的例子。txt && echo“用python_file1.py完成的”

Output of job.sh should look like this:

Done with python_file.py

完成了python_file.py

Done with python_file1.py

完成了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.py and add multiple command-line arguments at run time to the python file.
  • 在这里,shell脚本将运行文件python_file。py和在运行时向python文件添加多个命令行参数。
  • 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.
  • 您可以像:python python_file那样使用它。py,平原和简单。接下来,>>将在test -output中打印并存储这个.py文件的输出。txt文件。
  • && 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.
  • &&是一个逻辑运算符,只有在上面的操作成功执行之后才会运行,并且作为一个可选的echo“使用python_file完成”。py"将在运行时回显到您的cli/terminal。

#7


1  

I use this and it works fine

我用这个,它很好用。

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

#8


1  

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

#1


101  

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
  • 在文件的工作。sh,把这个
#!/bin/sh
python python_script.py
  • Execute this command to make the script runnable for you : chmod u+x job.sh
  • 执行此命令使脚本可运行:chmod u+x .sh。
  • Run it : ./job.sh
  • :运行它。/ job.sh

#2


76  

Method 1 - Create a shell script:

Suppose you have a python file hello.py Create a file called job.sh that 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:

Modify your script hello.py and add this as the first line

修改脚本你好。py加上这个作为第一行。

#!/usr/bin/env python

mark it executable using

马克可执行使用

$ chmod +x hello.py

then run it

然后运行它

$ ./hello.py

#3


6  

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

这是完全错误的,尤其是在最近。python ?python2.6吗?2.7 ?3.0 ?3.1 ?大多数情况下,您需要在python文件的shebang标记中指定python版本。我鼓励使用

#!/usr/bin/env python2 #or python2.6 or python3 or even python3.1
for compatibility.

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 python points to python3, while most people expect it pointing to python2.

这样,您需要的python版本只被写入一个文件中。现在的大多数系统同时有python2和python3,而且碰巧symlink python指向python3,而大多数人期望它指向python2。

#4


3  

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

#5


2  

you should be able to invoke it as python scriptname.py

您应该能够以python scriptname.py的形式调用它

eg

# !/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使其可执行。

EDIT: beaten to it :-p

编辑:打败它:-p

#6


2  

This works for me:

这工作对我来说:

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

    创建一个新的shell文件作业。比如说,touch job。sh和add command来运行python脚本(您甚至可以向该python添加命令行参数,我通常预先定义命令行参数)。

    chmod +x job.sh

    chmod + x job.sh

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

    内部工作。sh添加以下py文件,比如:

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

    python_file。这是一个很好的例子。txt && echo "用python_file.py完成的"

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

    python_file1。这是一个很好的例子。txt && echo“用python_file1.py完成的”

Output of job.sh should look like this:

Done with python_file.py

完成了python_file.py

Done with python_file1.py

完成了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.py and add multiple command-line arguments at run time to the python file.
  • 在这里,shell脚本将运行文件python_file。py和在运行时向python文件添加多个命令行参数。
  • 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.
  • 您可以像:python python_file那样使用它。py,平原和简单。接下来,>>将在test -output中打印并存储这个.py文件的输出。txt文件。
  • && 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.
  • &&是一个逻辑运算符,只有在上面的操作成功执行之后才会运行,并且作为一个可选的echo“使用python_file完成”。py"将在运行时回显到您的cli/terminal。

#7


1  

I use this and it works fine

我用这个,它很好用。

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

#8


1  

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