如何在不键入“python ...”的情况下运行python脚本

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

how to run python script without typing 'python ...'

pythonbashshell

提问by Martin08

I want to run a python script without explicitly having to call "python" every time in my shell. I've tried to add the shebang #!/path/to/python but this does not seem to work. Does anyone know a work around this? Many thanks.

我想运行 python 脚本,而不必每次都在我的 shell 中显式调用“python”。我试图添加shebang #!/path/to/python 但这似乎不起作用。有谁知道解决这个问题?非常感谢。

采纳答案by David Wolever

You've got to add the shebang:

你必须添加shebang:

#!/usr/bin/env python

Then make the script executable:

然后使脚本可执行:

chmod +x foo

Then you can run it like any other executable:

然后你可以像其他任何可执行文件一样运行它:

./foo

And a note from Homer6: if you're editing the file from windows and invoking it on linux, you may run into the cryptic "No such file or directory" error. It's due to the line endings of the lines being CRLF instead of LF. If you convert them to LF, the script will execute as expected. Notepad++ > View > Show Symbols > Show End of Line to show the EOL characters. And Notepad++ > Edit > EOL Conversion > Unix Format to convert all line endings to use LF. Alternatively, you can use the dos2unixtool (dos2unix foo.py), which is present on most Linux systems.

来自 Homer6 的注释:如果您从 windows 编辑文件并在 linux 上调用它,您可能会遇到神秘的“没有这样的文件或目录”错误。这是由于行的行尾是 CRLF 而不是 LF。如果将它们转换为 LF,脚本将按预期执行。Notepad++ > 查看 > 显示符号 > 显示行尾以显示 EOL 字符。和 Notepad++ > Edit > EOL Conversion > Unix Format 将所有行结尾转换为使用 LF。或者,您可以使用大多数 Linux 系统上都有的dos2unix工具 ( dos2unix foo.py)。

回答by Anto Binish Kaspar

Ensure you are able to run /path/to/python on your terminal. And make sure you have given execute permission for your python file. You can give permission for the file by

确保您能够在终端上运行 /path/to/python。并确保您已为您的 python 文件授予执行权限。您可以通过以下方式授予文件权限

chmod +x mypythonfile.py

回答by Anthon

  1. Add a line at the top of your script:

    #! /usr/bin/env python
    
  2. Rename your script from script_name.pyto script_name
  3. make the script executable: chmod +x script_name
  1. 在脚本顶部添加一行:

    #! /usr/bin/env python
    
  2. 将您的脚本重命名script_name.pyscript_name
  3. 使脚本可执行: chmod +x script_name

The line at the top selects the same pythonthat you get when typing python at the prompt. You can also specify a direct path:

顶部的行选择与python在提示符下键入 python 时得到的相同。您还可以指定直接路径:

#!/opt/python/3.6/bin/python

回答by mgc

It didn't really apply to your personal scripts but as you are quoting beets, note that it is also possible to automate this action when you are distributing your packages, thanks to setuptools entry_pointoption.
So if you are distributing a package like myModuleand want to make the main_function()function accessible via typing mymodulescriptin the console you would probably add something like this to your setup.pyfile :

它并不真正适用于您的个人脚本,但是当您引用时beets,请注意,由于setuptoolsentry_point选项,还可以在分发包时自动执行此操作。
因此,如果您正在分发类似的包myModule并希望main_function()通过mymodulescript在控制台中键入来访问该功能,您可能会在您的setup.py文件中添加如下内容:

setup(
    # your other arguments ..
    entry_points={
        'console_scripts': [
            'mymodulescript = myModule:main_function'
        ]
    }
)

回答by Tassos Pan

Another workaround could be to use an alias defined in the .bashrc :

另一种解决方法可能是使用 .bashrc 中定义的别名:

e.g. add the following line in your .bachrc file :

例如,在您的 .bachrc 文件中添加以下行:

alias mypythonalias = "python mypyrhonfile.py"

type in terminal :

输入终端:

source ~/.bashrc

and then you may simply type:

然后你可以简单地输入:

mypythonalias

to execute the python file.

执行python文件。