简体   繁体   English

如何安装脚本以从命令行在任何地方运行?

[英]How do I install a script to run anywhere from the command line?

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?我只是将脚本放在特定目录中,还是需要其他东西?

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 .这是放置自定义安装的二进制文件的正常位置,应该在您的PATH早期。

Simply copy the script there (probably using sudo ), and it should work for any user.只需将脚本复制到那里(可能使用sudo ),它应该适用于任何用户。

Walkthrough of making a python script available anywhere:使 Python 脚本在任何地方可用的演练:

Make a python script:制作一个python脚本:

cd /home/el/bin
touch stuff.py
chmod +x stuff.py

Find out where your python is:找出你的python在哪里:

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 知道文件的语言。

Just create ~/bin and put export PATH=$PATH:$HOME/bin in your bashrc/profile.只需创建~/bin并将export PATH=$PATH:$HOME/bin放在您的 bashrc/profile 中。 Don't mess with the system, it will bite you back, trust me.不要弄乱系统,它咬你的,相信我。

Few more things (relevant to the question but not part of the answer):还有几件事(与问题有关,但不是答案的一部分):

  1. 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其他可执行文件匹配,您会感到意外/ 非工作命令执行。
  2. Don't forget to chmod+x when you save your script in ~/bin .将脚本保存在~/bin时不要忘记chmod+x
  3. 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 your CWD to execute the script than putting it under ~/bin .请注意您在~/bin文件夹中放入的内容,如果您只是在测试某些内容或处理未完成的脚本,那么使用CWD ./$SCRIPT_NAME 来执行脚本总是比将其放在~/bin下更好。

The quick answer is to symlink your script to any directory included in your system $PATH .快速答案是将您的脚本symlink到系统$PATH包含的任何目录。

The long answer is described below with a walk through example, (this is what I normally do):下面通过一个演练示例描述了长答案,(这是我通常做的):

a) Create the script eg $HOME/Desktop/myscript.py : a) 创建脚本,例如$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

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"

d) Create a symbolic link to the script as follows: d) 创建脚本的符号链接,如下所示:

$ ln -s $HOME/Desktop/myscript.py $HOME/bin/hello

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, ie ii) 要创建符号链接,您应该使用完整正确的路径,即

$HOME/bin GOOD ~/bin NO GOOD! $HOME/bin~/bin不好!

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!

只需在 /usr/local/bin/ 中创建指向脚本的符号链接:

sudo ln -s /path/to/your/script.py /usr/local/bin/script

you can also use setuptools ( https://pypi.org/project/setuptools/ )您还可以使用setuptools ( https://pypi.org/project/setuptools/ )

  1. your script will be:你的脚本将是:
def hi():
    print("hi")

(suppose the file name is hello.py ) (假设文件名为hello.py

  1. also add __init__.py file next to your script (with nothing in it).还要在脚本旁边添加__init__.py文件(其中没有任何内容)。

  2. add setup.py script, with the content:添加setup.py脚本,内容为:

#!/usr/bin/env python3

import setuptools

install_requires = [
        'WHATEVER PACKAGES YOU NEED GOES HERE'
        ]

setuptools.setup(
    name="some_utils",
    version="1.1",
    packages=setuptools.find_packages(),
    install_requires=install_requires,
    entry_points={
        'console_scripts': [
            'cool_script = hello:hi',
        ],
    },
    include_package_data=True,
    )

  1. you can now run python setup.py develop in this folder您现在可以在此文件夹中运行python setup.py develop
  2. then from anywhere, run cool_script and your script will run.然后从任何地方运行cool_script ,你的脚本就会运行。

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.将脚本放在 PATH 中的某个位置(如/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./etc/profile添加别名可能是一种允许系统用户使用unalias命令撤消此操作的unalias The line to be added would be:要添加的行是:

alias MyScript=/path/to/file/MyScript

我在~/.bash_profile~/.zshrc找到一个简单的别名是最简单的:

alias myscript="python path/to/my/script.py"

Type echo $PATH in a shell.在 shell 中输入echo $PATH Those are the directories searched when you type command , so put it in one of those.这些是您键入command时搜索的目录,因此请将其放入其中之一。

Edit: Apparently don't use /usr/bin , use /usr/local/bin编辑:显然不要使用/usr/bin ,使用/usr/local/bin

Acording to FHS , the /usr/local/bin/ is the good place for custom scripts.根据FHS/usr/local/bin/是自定义脚本的好地方。 I prefer to make them 755 root:root , after copying them there.我更喜欢将它们复制到755 root:root之后。

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

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