使用 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
Using bash to run python script
提问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:
解决方案:
change py.bat to py.command
at terminal:
$ cd ~/Desktop $ chmod 755 py.command
open code on vim and place in (from: ./configure : /bin/sh^M : bad interpreter):
:set fileformat=unix
Changed code too:
#!bin/bash cd ~/Desktop python search.py exit;
将 py.bat 更改为 py.command
在终端:
$ cd ~/Desktop $ chmod 755 py.command
在 vim 上打开代码并放入(来自:./configure : /bin/sh^M : bad interpreter):
:set fileformat=unix
代码也改了:
#!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 Terminal
application 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.command
and 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.py
file 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 777
on 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 不太确定,但值得一试。