If I have a basic Python script, with it's hashbang and what-not in place, so that from the terminal on Linux I can run
如果我有一个基本的Python脚本,它是hashbang和什么的——不合适,这样我就可以从Linux上的终端运行了
/path/to/file/MyScript [args]
without executing through the interpreter or any file extensions, and it will execute the program.
不通过解释器或任何文件扩展名执行,它将执行程序。
So would I install this script so that I can type simply
所以我要安装这个脚本以便我可以简单地输入
MyScript [args]
anywhere in the system and it will run? Can this be implemented for all users on the system, or must it be redone for each one? Do I simply place the script in a specific directory, or are other things necessary?
系统中的任何地方,它都会运行吗?可以为系统上的所有用户实现这一点吗?还是必须为每个用户重做?我只是将脚本放在一个特定的目录中,还是需要其他的东西?
8 个解决方案
#1
19
The best place to put things like this is /usr/local/bin
.
放置这样东西的最佳位置是/usr/ local/bin。
This is the normal place to put custom installed binaries, and should be early in your PATH
.
这是放置自定义二进制文件的正常位置,并且应该在您的路径的早期。
Simply copy the script there (probably using sudo
), and it should work for any user.
只需在那里复制脚本(可能使用sudo),它应该适用于任何用户。
#2
7
Walkthrough of making a python script available anywhere:
Make a python script:
python脚本:
cd /home/el/bin
touch stuff.py
chmod +x stuff.py
Find out where your python is:
找出你的蟒蛇在哪里:
which python
/usr/bin/python
Put this code in there:
把这个代码放在那里:
#!/usr/bin/python
print "hi"
Run in it the same directory:
在同一目录下运行:
python stuff.py
Go up a directory and it's not available:
上一个目录,它是不可用的:
cd ..
stuff.py
-bash: stuff.py: command not found
Not found! It's as we expect, add the file path of the python file to the $PATH
未找到!正如我们所期望的那样,将python文件的文件路径添加到$ path中。
vi ~/.bashrc
Add the file:
添加文件:
export PATH=$PATH:/home/el/bin
Save it out, re apply the .bashrc, and retry
保存它,重新应用。bashrc,并重试
source ~/.bashrc
Try again:
再试一次。
cd /home/el
stuff.py
Prints:
打印:
hi
The trick is that the bash shell knows the language of the file via the shebang.
诀窍在于,bash shell通过shebang知道文件的语言。
#3
7
Just create ~/bin
and put export PATH=$PATH:$HOME/bin
in your bashrc/profile. Don't mess with the system, it will bite you back, trust me.
只需在bashrc/profile中创建~/bin并将export PATH=$PATH:$HOME/bin。别搞砸了,你会吃苦头的,相信我。
Few more things (relevant to the question but not part of the answer):
没有更多的事情(与问题相关但不是答案的一部分):
- The other way
export PATH=$HOME/bin:$PATH
is NOT safe, for bash will will look into your~/bin
folder for executables, and if their name matches with other executables in your original$PATH
you will be surprised by unexpected/non working command execution. - 另一种方法是export PATH=$HOME/bin:$PATH不安全,因为bash将查看您的~/bin文件夹中的可执行文件,如果它们的名称与您最初的$PATH中的其他可执行文件相匹配,您将会对意外/非工作命令执行感到意外。
- Don't forget to
chmod+x
when you save your script in~/bin
. - 在~/bin中保存脚本时,不要忘记chmod+x。
- Be aware of what you are putting in your
~/bin
folder, if you are just testing something or working on unfinished script, its always better to use ./$SCRIPT_NAME from yourCWD
to execute the script than putting it under~/bin
. - 请注意您在~/bin文件夹中放置的内容,如果您只是在测试某些内容或正在处理未完成的脚本,那么最好使用CWD中的/$SCRIPT_NAME来执行脚本,而不是将其放在~/bin下。
#4
5
The quick answer is to symlink
your script to any directory included in your system $PATH
.
快速的答案是将脚本符号链接到系统$PATH中包含的任何目录。
The long answer is described below with a walk through example, (this is what I normally do):
下面用一个例子来描述长答案(这是我通常做的):
a) Create the script e.g. $HOME/Desktop/myscript.py
:
创建脚本,例如$HOME/Desktop/myscript.py:
#!/usr/bin/python
print("Hello Pythonista!")
b) Change the permission of the script file to make it executable:
b)修改脚本文件的权限,使其可执行:
$ chmod +x myscript.py
chmod + x myscript.py美元
c) Add a customized directory to the $PATH
(see why in the notes below) to use it for the user's scripts:
c)将自定义目录添加到$PATH中(参见下面的说明),以便将其用于用户的脚本:
$ export PATH="$PATH:$HOME/bin"
美元出口路径= " $路径:$ HOME / bin”
d) Create a symbolic link to the script as follows:
d)创建到脚本的符号链接如下:
$ ln -s $HOME/Desktop/myscript.py $HOME/bin/hello
ln - s $ HOME /桌面/ myscript美元。py $ HOME / bin /你好
Notice that hello
(can be anything) is the name of the command that you will use to invoke your script.
请注意,hello(可以是任何东西)是用于调用脚本的命令的名称。
Note:
注意:
i) The reason to use $HOME/bin
instead of the /usr/local/bin
is to separate the local scripts from those of other users (if you wish to) and other installed stuff.
i)使用$HOME/bin而不是/usr/local/bin的原因是要将本地脚本与其他用户(如果您愿意)和其他已安装的内容分开。
ii) To create a symlink you should use the complete correct path, i.e.
要创建一个符号链接,您应该使用完整的正确路径,即。
$HOME/bin
GOOD ~/bin
NO GOOD!
$HOME/bin GOOD ~/bin NO GOOD!
Here is a complete example:
这里有一个完整的例子:
$ pwd
~/Desktop
$ cat > myscript.py << EOF
> #!/usr/bin/python
> print("Hello Pythonista!")
> EOF
$ export PATH="$PATH:$HOME/bin"
$ ln -s $HOME/Desktop/myscript.py $HOME/bin/hello
$ chmod +x myscript.py
$ hello
Hello Pythonista!
#5
3
Type echo $PATH
in a shell. Those are the directories searched when you type command
, so put it in one of those.
在shell中输入echo $PATH。这些是你输入命令时搜索的目录,所以把它放在其中之一。
Edit: Apparently don't use /usr/bin
, use /usr/local/bin
编辑:显然不要使用/usr/bin,使用/usr/local/bin
#6
3
Putting the script somewhere in the PATH (like /usr/local/bin
) is a good solution, but this forces all the users of your system to use/see your script.
将脚本放在路径中的某个位置(比如/usr/local/bin)是一个很好的解决方案,但这将迫使系统的所有用户使用/查看您的脚本。
Adding an alias in /etc/profile
could be a way to do what you want allowing the users of your system to undo this using the unalias
command. The line to be added would be:
在/etc/profile中添加别名可以实现您想要的操作,允许系统用户使用unalias命令撤消此操作。将增加的一行是:
alias MyScript=/path/to/file/MyScript
#7
1
Just create symbolic link to your script in /usr/local/bin/:
只需在/usr/local/bin/中为脚本创建符号链接:
sudo ln -s /path/to/your/script.py /usr/local/bin/script
#8
0
Acording to FHS, the /usr/local/bin/
is the good place for custom scripts. I prefer to make them 755
root:root
, after copying them there.
与FHS相适应,/usr/local/bin/是定制脚本的好地方。我更喜欢把它们复制到755 root:root。
#1
19
The best place to put things like this is /usr/local/bin
.
放置这样东西的最佳位置是/usr/ local/bin。
This is the normal place to put custom installed binaries, and should be early in your PATH
.
这是放置自定义二进制文件的正常位置,并且应该在您的路径的早期。
Simply copy the script there (probably using sudo
), and it should work for any user.
只需在那里复制脚本(可能使用sudo),它应该适用于任何用户。
#2
7
Walkthrough of making a python script available anywhere:
Make a python script:
python脚本:
cd /home/el/bin
touch stuff.py
chmod +x stuff.py
Find out where your python is:
找出你的蟒蛇在哪里:
which python
/usr/bin/python
Put this code in there:
把这个代码放在那里:
#!/usr/bin/python
print "hi"
Run in it the same directory:
在同一目录下运行:
python stuff.py
Go up a directory and it's not available:
上一个目录,它是不可用的:
cd ..
stuff.py
-bash: stuff.py: command not found
Not found! It's as we expect, add the file path of the python file to the $PATH
未找到!正如我们所期望的那样,将python文件的文件路径添加到$ path中。
vi ~/.bashrc
Add the file:
添加文件:
export PATH=$PATH:/home/el/bin
Save it out, re apply the .bashrc, and retry
保存它,重新应用。bashrc,并重试
source ~/.bashrc
Try again:
再试一次。
cd /home/el
stuff.py
Prints:
打印:
hi
The trick is that the bash shell knows the language of the file via the shebang.
诀窍在于,bash shell通过shebang知道文件的语言。
#3
7
Just create ~/bin
and put export PATH=$PATH:$HOME/bin
in your bashrc/profile. Don't mess with the system, it will bite you back, trust me.
只需在bashrc/profile中创建~/bin并将export PATH=$PATH:$HOME/bin。别搞砸了,你会吃苦头的,相信我。
Few more things (relevant to the question but not part of the answer):
没有更多的事情(与问题相关但不是答案的一部分):
- The other way
export PATH=$HOME/bin:$PATH
is NOT safe, for bash will will look into your~/bin
folder for executables, and if their name matches with other executables in your original$PATH
you will be surprised by unexpected/non working command execution. - 另一种方法是export PATH=$HOME/bin:$PATH不安全,因为bash将查看您的~/bin文件夹中的可执行文件,如果它们的名称与您最初的$PATH中的其他可执行文件相匹配,您将会对意外/非工作命令执行感到意外。
- Don't forget to
chmod+x
when you save your script in~/bin
. - 在~/bin中保存脚本时,不要忘记chmod+x。
- Be aware of what you are putting in your
~/bin
folder, if you are just testing something or working on unfinished script, its always better to use ./$SCRIPT_NAME from yourCWD
to execute the script than putting it under~/bin
. - 请注意您在~/bin文件夹中放置的内容,如果您只是在测试某些内容或正在处理未完成的脚本,那么最好使用CWD中的/$SCRIPT_NAME来执行脚本,而不是将其放在~/bin下。
#4
5
The quick answer is to symlink
your script to any directory included in your system $PATH
.
快速的答案是将脚本符号链接到系统$PATH中包含的任何目录。
The long answer is described below with a walk through example, (this is what I normally do):
下面用一个例子来描述长答案(这是我通常做的):
a) Create the script e.g. $HOME/Desktop/myscript.py
:
创建脚本,例如$HOME/Desktop/myscript.py:
#!/usr/bin/python
print("Hello Pythonista!")
b) Change the permission of the script file to make it executable:
b)修改脚本文件的权限,使其可执行:
$ chmod +x myscript.py
chmod + x myscript.py美元
c) Add a customized directory to the $PATH
(see why in the notes below) to use it for the user's scripts:
c)将自定义目录添加到$PATH中(参见下面的说明),以便将其用于用户的脚本:
$ export PATH="$PATH:$HOME/bin"
美元出口路径= " $路径:$ HOME / bin”
d) Create a symbolic link to the script as follows:
d)创建到脚本的符号链接如下:
$ ln -s $HOME/Desktop/myscript.py $HOME/bin/hello
ln - s $ HOME /桌面/ myscript美元。py $ HOME / bin /你好
Notice that hello
(can be anything) is the name of the command that you will use to invoke your script.
请注意,hello(可以是任何东西)是用于调用脚本的命令的名称。
Note:
注意:
i) The reason to use $HOME/bin
instead of the /usr/local/bin
is to separate the local scripts from those of other users (if you wish to) and other installed stuff.
i)使用$HOME/bin而不是/usr/local/bin的原因是要将本地脚本与其他用户(如果您愿意)和其他已安装的内容分开。
ii) To create a symlink you should use the complete correct path, i.e.
要创建一个符号链接,您应该使用完整的正确路径,即。
$HOME/bin
GOOD ~/bin
NO GOOD!
$HOME/bin GOOD ~/bin NO GOOD!
Here is a complete example:
这里有一个完整的例子:
$ pwd
~/Desktop
$ cat > myscript.py << EOF
> #!/usr/bin/python
> print("Hello Pythonista!")
> EOF
$ export PATH="$PATH:$HOME/bin"
$ ln -s $HOME/Desktop/myscript.py $HOME/bin/hello
$ chmod +x myscript.py
$ hello
Hello Pythonista!
#5
3
Type echo $PATH
in a shell. Those are the directories searched when you type command
, so put it in one of those.
在shell中输入echo $PATH。这些是你输入命令时搜索的目录,所以把它放在其中之一。
Edit: Apparently don't use /usr/bin
, use /usr/local/bin
编辑:显然不要使用/usr/bin,使用/usr/local/bin
#6
3
Putting the script somewhere in the PATH (like /usr/local/bin
) is a good solution, but this forces all the users of your system to use/see your script.
将脚本放在路径中的某个位置(比如/usr/local/bin)是一个很好的解决方案,但这将迫使系统的所有用户使用/查看您的脚本。
Adding an alias in /etc/profile
could be a way to do what you want allowing the users of your system to undo this using the unalias
command. The line to be added would be:
在/etc/profile中添加别名可以实现您想要的操作,允许系统用户使用unalias命令撤消此操作。将增加的一行是:
alias MyScript=/path/to/file/MyScript
#7
1
Just create symbolic link to your script in /usr/local/bin/:
只需在/usr/local/bin/中为脚本创建符号链接:
sudo ln -s /path/to/your/script.py /usr/local/bin/script
#8
0
Acording to FHS, the /usr/local/bin/
is the good place for custom scripts. I prefer to make them 755
root:root
, after copying them there.
与FHS相适应,/usr/local/bin/是定制脚本的好地方。我更喜欢把它们复制到755 root:root。