简体   繁体   English

使 Python 脚本可执行并具有参数

[英]Make Python Script Executable and Have Parameter

I made a Python parser for a language I'm making, and I want the script to be executable in the terminal, I've added a shebang, and tried tried the chmod +x in the terminal, but I got an error when I typed it:我为我正在制作的语言制作了一个 Python 解析器,我希望脚本在终端中是可执行的,我添加了一个 shebang,并尝试在终端中尝试chmod +x ,但是当我出现错误时输入它:

   bash: ./source.py: No file or directory

Even though the file exists.即使文件存在。
Also, would anyone know how to add parameters/arguments to the command?另外,有人知道如何向命令添加参数/参数吗?

Your shell likely does not know to execute the script as a python script.您的 shell 可能不知道将脚本作为 python 脚本执行。

Add a shebang line like添加像这样的shebang

#!/usr/bin/env python

as the first line of the file and try again.作为文件的第一行,然后重试。

To check command line parameters, for simple things you can look in sys.argv .要检查命令行参数,您可以在sys.argv查看简单的内容。 For anything nontrivial, use the argparse module instead.对于任何重要的事情,请改用argparse模块。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM