Linux 在没有 python 命令的终端中运行 python 脚本

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

run a python script in terminal without the python command

pythonlinuxcommand-line

提问by Alpagut

I have a python script let's name it script1.py. I can run it in the terminal this way:

我有一个 python 脚本,我们将它命名为 script1.py。我可以这样在终端中运行它:

python /path/script1.py
...

but I want to run like a command-line program:

但我想像命令行程序一样运行:

arbitraryname
...

how can i do it ?

我该怎么做 ?

采纳答案by Martijn Pieters

You use a shebang lineat the start of your script:

您在脚本的开头使用shebang 行

#!/usr/bin/env python

make the file executable:

使文件可执行:

chmod +x arbitraryname

and put it in a directory on your PATH (can be a symlink):

并将其放在 PATH 上的目录中(可以是符号链接):

cd ~/bin/
ln -s ~/some/path/to/myscript/arbitraryname

回答by chepner

Add the following line to the beginning script1.py

将以下行添加到开头 script1.py

#!/usr/bin/env python

and then make the script executable:

然后使脚本可执行:

$ chmod +x script1.py

If the script resides in a directory that appears in your PATHvariable, you can simply type

如果脚本驻留在出现在PATH变量中的目录中,您只需键入

$ script1.py

Otherwise, you'll need to provide the full path (either absolute or relative). This includes the current working directory, which should notbe in your PATH.

否则,您需要提供完整路径(绝对路径或相对路径)。这包括当前工作目录,它应该在您的PATH.

$ ./script1.py

回答by asheeshr

You need to use a hashbang. Add it to the first line of your python script.

您需要使用hashbang。将它添加到 python 脚本的第一行。

#! <full path of python interpreter>

Then change the file permissions, and add the executing permission.

然后更改文件权限,并添加执行权限。

chmod +x <filename>

And finally execute it using

最后使用

./<filename>

If its in the current directory,

如果它在当前目录中,

回答by arajek

There are three parts:

分为三个部分:

  1. Add a 'shebang' at the top of your script which tells how to execute your script
  2. Give the script 'run' permissions.
  3. Make the script in your PATH so you can run it from anywhere.
  1. 在您的脚本顶部添加一个“shebang”,它告诉您如何执行您的脚本
  2. 授予脚本“运行”权限。
  3. 在您的 PATH 中制作脚本,以便您可以从任何地方运行它。

Adding a shebang

添加一个shebang

You need to add a shebang at the top of your script so the shell knows which interpreter to use when parsing your script. It is generally:

您需要在脚本顶部添加一个shebang,以便shell 知道在解析脚本时使用哪个解释器。它一般是:

#!path/to/interpretter

To find the path to your python interpretter on your machine you can run the command:

要在您的机器上找到 Python 解释器的路径,您可以运行以下命令:

which python

This will search your PATH to find the location of your python executable. It should come back with a absolute path which you can then use to form your shebang. Make sure your shebang is at the top of your python script:

这将搜索您的 PATH 以找到您的 python 可执行文件的位置。它应该返回一个绝对路径,然后您可以使用它来形成您的shebang。确保您的shebang位于python脚本的顶部:

#!/usr/bin/python

Run Permissions

运行权限

You have to mark your script with run permissions so that your shell knows you want to actually execute it when you try to use it as a command. To do this you can run this command:

您必须使用运行权限标记您的脚本,以便您的 shell 在您尝试将其用作命令时知道您想要实际执行它。为此,您可以运行以下命令:

chmod +x myscript.py

Add the script to your path

将脚本添加到您的路径

The PATH environment variable is an ordered list of directories that your shell will search when looking for a command you are trying to run. So if you want your python script to be a command you can run from anywhere then it needs to be in your PATH. You can see the contents of your path running the command:

PATH 环境变量是一个有序的目录列表,您的 shell 在查找您尝试运行的命令时将搜索这些目录。所以如果你想让你的 python 脚本成为一个可以从任何地方运行的命令,那么它需要在你的 PATH 中。您可以看到运行命令的路径的内容:

echo $PATH

This will print out a long line of text, where each directory is seperated by a semicolon. Whenever you are wondering where the actual location of an executable that you are running from your PATH, you can find it by running the command:

这将打印出一长行文本,其中每个目录由分号分隔。每当您想知道从 PATH 运行的可执行文件的实际位置时,您可以通过运行以下命令找到它:

which <commandname>

Now you have two options: Add your script to a directory already in your PATH, or add a new directory to your PATH. I usually create a directory in my user home directory and then add it the PATH. To add things to your path you can run the command:

现在您有两个选择:将脚本添加到 PATH 中已有的目录中,或者将新目录添加到 PATH 中。我通常在我的用户主目录中创建一个目录,然后将其添加到 PATH。要将内容添加到您的路径,您可以运行以下命令:

export PATH=/my/directory/with/pythonscript:$PATH

Now you should be able to run your python script as a command anywhere. BUT! if you close the shell window and open a new one, the new one won't remember the change you just made to your PATH. So if you want this change to be saved then you need to add that command at the bottom of your .bashrc or .bash_profile

现在您应该可以在任何地方将您的 Python 脚本作为命令运行。但!如果您关闭 shell 窗口并打开一个新窗口,新窗口将不会记住您刚刚对 PATH 所做的更改。因此,如果您希望保存此更改,则需要在 .bashrc 或 .bash_profile 的底部添加该命令