问题:Shell脚本:从Shell脚本中执行python程序

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

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

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

如何做到这一点?

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.

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

How can this be accomplished?


回答 0

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

python path/to/the/python_script.py

细节:

  • 在文件job.sh中,放入
#!/bin/sh
python python_script.py
  • 执行以下命令以使脚本可运行: chmod u+x job.sh
  • 运行 : ./job.sh

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

python path/to/the/python_script.py

Details:

  • In the file job.sh, put this
#!/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

回答 1

方法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

Method 1 – Create a shell script:

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

#!/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

#!/usr/bin/env python

mark it executable using

$ chmod +x hello.py

then run it

$ ./hello.py

回答 2

恕我直言

python /path/to/script.py

是非常错误的,尤其是在这些日子里。哪个Python?python2.6?2.7?3.0?3.1?大多数时候,您需要在python文件的shebang标记中指定python版本。我鼓励使用

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

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

#!/ bin / bash

/path/to/script.py

这样,您所需的python版本就只写在一个文件中。如今,大多数系统同时具有python2和python3,并且碰巧symlink python指向python3,而大多数人希望它指向python2

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

#!/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.


回答 3

将以下程序另存为print.py

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

然后在终端中输入:

chmod +x print.py
./print.py

Save the following program as print.py:

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

Then in the terminal type:

chmod +x print.py
./print.py

回答 4

这最适合我:在脚本顶部添加以下内容:

#!c:/Python27/python.exe

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

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

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:

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

回答 5

这对我有用:

  1. 创建一个新的外壳文件作业。这么说吧: 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 / terminal上。

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).

    chmod +x job.sh

  2. Inside job.sh add 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"

Output of job.sh should look like this:

Done with python_file.py

Done with python_file1.py

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

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.
  • 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.

回答 6

您应该能够像python scriptname.py例如调用它

# !/bin/bash

python /home/user/scriptname.py 

另外,请确保脚本具有运行权限。

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

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

# !/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.


回答 7

我用这个并且很好用

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

I use this and it works fine

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

回答 8

由于其他帖子都说了所有(我在寻找以下内容时偶然发现了该帖子)。
这是从另一个python脚本执行python脚本的方法:

Python 2:

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

Python 3:

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

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

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 2:

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

Python 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


声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。