简体   繁体   English

如何使Shell脚本启动python脚本?

[英]How to make shell script launch python script?

I need a .command file that can launch a python script in the same directory as it. 我需要一个.command文件,该文件可以在与其所在的目录中启动python脚本。

I have so far gotten this: 到目前为止,我得到了:

#!bin/bash
python /Users/username/Desktop/IF.py

But every time the Terminal returns the same error. 但是终端每次返回相同的错误。

Last login: Sun Oct 11 01:48:38 on ttys000
MacBook-Pro:~ username$ /var/folders/m7/yf7p3vdj2mx0l9r7qb68rttc0000gn/T/Cleanup\ At\ Startup/run 466235498.368.command.command ; exit;
/var/folders/m7/yf7p3vdj2mx0l9r7qb68rttc0000gn/T/Cleanup At Startup/run-466235498.368.command.command: line 3: bin/bash: No such file or directory
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.

[Process completed]

Please note the error is on line 3 of the 2 lines of code I have... 请注意错误是我有两行代码的第3行...

Why not just launch the python script itself from the get-go? 为什么不从一开始就启动python脚本本身呢? You can add a shebang at the top like so: 您可以在顶部添加shebang,如下所示:

#! /user/bin/env python
... the rest of your python script here

Then just give the script executable permissions: 然后只需赋予脚本可执行权限:

chmod +x nameofscript.py

Then just launch it like so: 然后像这样启动它:

./nameofscript.py

In case you don't want the script to rely on the environment, just use the following shebang instead: 如果您不希望脚本依赖于环境,则只需使用以下shebang即可:

#! /usr/bin/python

Maybe because the shebang (or hashbang ). 也许是因为shebang (或hashbang )。 About what is shebang (wiki) . 关于什么是shebang (wiki)


#!bin/bash
  ^ 
python /Users/username/Desktop/IF.py

This means the system will use bin/bash to run this program, but bin/bash means path/bin/bash . 这意味着系统将使用bin/bash来运行该程序,但是bin/bash意味着path/bin/bash

For example, if you're in /home then the system will use /home/bin/bash to run this program. 例如,如果您在/home则系统将使用/home/bin/bash运行该程序。


I think you need this: 我认为您需要这样做:

#!/bin/bash
python /Users/username/Desktop/IF.py

Note that I replaced #!bin/bash to #!/bin/bash . 请注意,我将#!bin/bash替换为#!/bin/bash

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 双击.desktop文件运行shell脚本以启动python脚本 - Run shell script by double clicking .desktop file to launch python script 在 python 脚本中启动一个 shell 命令,等待终止并返回到脚本 - Launch a shell command with in a python script, wait for the termination and return to the script 如何在 Python 中使用 argparse 传递 Shell 脚本样式参数 - How to make Shell script style arguments passing with argparse in Python 如何在Shell上出现提示时按下'enter'进行python脚本 - How to make python script press 'enter' when prompted on Shell 在shell上提示时如何制作python脚本类型 - How to make python script type when prompted on shell 如何将 shell 脚本转换为 python? - How to convert shell script into python? 如何在使用vscode进行调试时执行设置python虚拟环境的shell脚本(我需要对launch.json进行哪些更改) - how to execute a shell script which sets up the python virtual environment when debugging with vscode ( What changes do i need to make to launch.json) 从Python启动脚本 - Launch a script from Python 如何将python脚本作为Windows服务启动 - How to launch python script as a windows Services 如何从Python脚本启动winpdb? - How to launch winpdb from a Python script?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM