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

时间:2022-07-25 00:15:20

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 ?

我该怎么做呢?

4 个解决方案

#1


72  

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):

并将其放在路径上的目录中(可以是symlink):

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

#2


49  

There are three parts:

有三个部分:

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

Adding a 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:

这将搜索您的路径以查找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:

您必须使用run权限对脚本进行标记,以便当您试图将脚本用作命令时,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脚本成为一个命令,你可以从任何地方运行,那么它需要在你的路径中。您可以看到运行以下命令的路径内容:

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:

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

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:

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

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窗口并打开一个新的窗口,新的窗口将不记得您刚刚对路径所做的更改。因此,如果希望保存此更改,则需要在.bashrc或.bash_profile的底部添加该命令

#3


10  

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 PATH variable, you can simply type

如果脚本位于路径变量中出现的目录中,您可以简单地输入

$ 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.

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

$ ./script1.py

#4


2  

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,

如果它在当前目录中,

#1


72  

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):

并将其放在路径上的目录中(可以是symlink):

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

#2


49  

There are three parts:

有三个部分:

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

Adding a 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:

这将搜索您的路径以查找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:

您必须使用run权限对脚本进行标记,以便当您试图将脚本用作命令时,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脚本成为一个命令,你可以从任何地方运行,那么它需要在你的路径中。您可以看到运行以下命令的路径内容:

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:

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

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:

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

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窗口并打开一个新的窗口,新的窗口将不记得您刚刚对路径所做的更改。因此,如果希望保存此更改,则需要在.bashrc或.bash_profile的底部添加该命令

#3


10  

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 PATH variable, you can simply type

如果脚本位于路径变量中出现的目录中,您可以简单地输入

$ 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.

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

$ ./script1.py

#4


2  

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,

如果它在当前目录中,