简体   繁体   English

在没有python命令的情况下在终端中运行python脚本

[英]run a python script in terminal without the python command

I have a python script let's name it script1.py. 我有一个python脚本,我们将它命名为script1.py。 I can run it in the terminal this way: 我可以这样在终端上运行它:

python /path/script1.py
...

but I want to run like a command-line program: 但我想像命令行程序一样运行:

arbitraryname
...

how can i do it ? 我该怎么做 ?

You use a shebang line at 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

There are three parts: 有三个部分:

  1. Add a 'shebang' at the top of your script which tells how to execute your script 在脚本顶部添加一个“shebang”,告诉您如何执行脚本
  2. Give the script 'run' permissions. 授予脚本“运行”权限。
  3. Make the script in your PATH so you can run it from anywhere. 在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. 您需要在脚本的顶部添加一个shebang,以便shell在解析脚本时知道要使用哪个解释器。 It is generally: 通常是:

#!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. 这将搜索您的PATH以查找您的python可执行文件的位置。 It should come back with a absolute path which you can then use to form your shebang. 它应该以绝对路径返回,然后您可以使用它来形成您的shebang。 Make sure your shebang is at the top of your python script: 确保你的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. 您必须使用运行权限标记脚本,以便在尝试将其用作命令时,shell知道您希望实际执行它。 To do this you can run this command: 为此,您可以运行此命令:

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. PATH环境变量是shell在查找您尝试运行的命令时将搜索的目录的有序列表。 So if you want your python script to be a command you can run from anywhere then it needs to be in your PATH. 因此,如果您希望您的python脚本是一个命令,您可以从任何地方运行,那么它需要在您的PATH中。 You can see the contents of your path running the command: 您可以看到运行该命令的路径的内容:

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. 现在您有两个选择:将脚本添加到PATH中已有的目录中,或者将新目录添加到PATH中。 I usually create a directory in my user home directory and then add it the PATH. 我通常在用户主目录中创建一个目录,然后将其添加到PATH中。 To add things to your path you can run the command: 要在路径中添加内容,可以运行以下命令:

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

Now you should be able to run your python script as a command anywhere. 现在你应该可以在任何地方将python脚本作为命令运行。 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. 如果您关闭shell窗口并打开一个新窗口,新窗口将不会记住您刚刚对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 因此,如果您希望保存此更改,则需要在.bashrc或.bash_profile底部添加该命令。

Add the following line to the beginning script1.py 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 PATH variable, 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 not be in your PATH . 这包括当前工作目录,该目录应该在您的PATH

$ ./script1.py

You need to use a hashbang . 你需要使用hashbang Add it to the first line of your python script. 将它添加到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, 如果它在当前目录中,

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

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