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

后端 未结 10 1116
梦谈多话
梦谈多话 2020-12-04 10:14

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

/path/to/file/MyScript [args]


        
相关标签:
10条回答
  • 2020-12-04 10:45

    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.

    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.
    2. Don't forget to chmod+x when you save your script in ~/bin.
    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.
    0 讨论(0)
  • 2020-12-04 10:46

    Walkthrough of making a python script available anywhere:

    Make a python script:

    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

    vi ~/.bashrc
    

    Add the file:

    export PATH=$PATH:/home/el/bin
    

    Save it out, re apply the .bashrc, and retry

    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.

    0 讨论(0)
  • 2020-12-04 10:50

    you can also use setuptools (https://pypi.org/project/setuptools/)

    1. your script will be:
    def hi():
        print("hi")
    

    (suppose the file name is hello.py)

    1. also add __init__.py file next to your script (with nothing in it).

    2. add setup.py script, with the content:

    #!/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
    2. then from anywhere, run cool_script and your script will run.
    0 讨论(0)
  • 2020-12-04 10:52

    Just create symbolic link to your script in /usr/local/bin/:

    sudo ln -s /path/to/your/script.py /usr/local/bin/script
    
    0 讨论(0)
提交回复
热议问题