使用 bash 运行 python 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46548952/
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-09-18 16:30:28  来源:igfitidea点击:

Using bash to run python script

pythonbashpython-2.7shell

提问by Kay Carosfeild

I have a python script that takes in a .txt file and outputs a .txt file. I want to create a bash file that I can click on from my desktop to execute the python script.

我有一个 python 脚本,它接收一个 .txt 文件并输出一个 .txt 文件。我想创建一个 bash 文件,我可以从桌面单击该文件以执行 python 脚本。

So far I have:

到目前为止,我有:

#!/bin/bash
cd /Desktop; 
cd ./py-data;
python ./grab.py;
exit;

This just opens up the python script. Ideally I would like to click on the bash script and have the python script to run in the back ground and just produce the output without having to open the python script.

这只是打开了python脚本。理想情况下,我想单击 bash 脚本并让 python 脚本在后台运行,只需生成输出而无需打开 python 脚本。

Solution:

解决方案:

  1. change py.bat to py.command

  2. at terminal:

    $ cd ~/Desktop $ chmod 755 py.command

  3. open code on vim and place in (from: ./configure : /bin/sh^M : bad interpreter):

    :set fileformat=unix

  4. Changed code too:

    #!bin/bash cd ~/Desktop python search.py exit;

  1. 将 py.bat 更改为 py.command

  2. 在终端:

    $ cd ~/Desktop $ chmod 755 py.command

  3. 在 vim 上打开代码并放入(来自:./configure : /bin/sh^M : bad interpreter):

    :set fileformat=unix

  4. 代码也改了:

    #!bin/bash cd ~/Desktop python search.py exit;

采纳答案by pbuck

On MacOS, set the name of your script file to end in .command, and make the file executable. With that filename extension, you can double-click the file, and it will run Terminalapplication and any output (to stdout / stderr) will be displayed in the terminal window which pops up for execution.

在 MacOS 上,将脚本文件的名称设置为以 结尾.command,并使文件可执行。使用该文件扩展名,您可以双击该文件,它将运行Terminal应用程序,并且任何输出(到 stdout / stderr)都将显示在弹出的终端窗口中以供执行。

=== /Users/john/Desktop/foo.command ===
#!/bin/bash
echo 'hello'

Then:

然后:

===  At command prompt===
$ cd ~/Desktop
$ chmod 755 foo.command

Double click on foo.commandand you'll see window popup:

双击foo.command,你会看到弹出窗口:

Last login: Thu Oct 5
/Users/john/Desktop/foo.command ; exit;
iMac:~/Desktop john$ /Users/john/Desktop/foo.command ; exit;
hello
logout
[Process completed]

In the popup window, you'll see lots of lines, plus your output ("hello").

在弹出窗口中,您会看到很多行,以及您的输出(“hello”)。

In your particular example, I think you have two problems:

在您的特定示例中,我认为您有两个问题:

First, you mention /Desktop, which probably isn't what you want, as the user's Desktop is ~/Desktop. This would cause your script to fail.

首先,您提到/Desktop,这可能不是您想要的,因为用户的桌面是~/Desktop。这会导致您的脚本失败。

Second, the output you would see in the popup window is the output your script writes directly to standard out. If you script is writing to another file, it may be working great, but you'll not see thatinformation displayed in the popup (it will be in whatever file you wrote it to.) So it depends what your grab.pyfile actually does.

其次,您将在弹出窗口中看到的输出是您的脚本直接写入标准输出的输出。如果您的脚本正在写入另一个文件,它可能工作得很好,但您不会在弹出窗口中看到信息(它将在您写入的任何文件中。)所以这取决于您的grab.py文件实际做什么。

Finally, you say "run in the background". Technically, that's not what's happening, as it will run in a separate foreground process. I assume that's what you mean.

最后,你说“在后台运行”。从技术上讲,这不是正在发生的事情,因为它将在单独的前台进程中运行。我想这就是你的意思。

回答by Bhavay Pahuja

chmod +x *filename*

Execute the above command in the terminal then make the bash file as follows

在终端执行上面的命令然后制作bash文件如下

cd Desktop/*folder*/
python3 *filename.py*

回答by ejj28

You could try chmod 777on your bash script and then removing the extension. If you are on macOS then it should work. Not so sure about Linux, but it's worth a try.

您可以尝试chmod 777使用 bash 脚本,然后删除扩展名。如果您使用的是 macOS,那么它应该可以工作。对 Linux 不太确定,但值得一试。