1

I apologize in advance if my terminology is off, but what I want to do is execute a python script using a custom command. So for example, I want to write a python script that accepts some shell arguments and performs some arbitrary shell commands. We'll call this script random.py. Instead of typing:

> python random.py arguments go here

I would like to be able to type something like:

> random arguments go here

So in other words, I want to get rid of the python command and omit the .py. I realize that I could use an alias but I also want to make this script available for anyone else that wants to use it, and I don't want to ask them to make an alias.

Basically what I'm after is something like meteor js, zurb foundation, or grunt. After I installed meteor I was able to create a new application by going to the shell and typing:

> meteor create --newapplicationname 

Same concept with foundation and grunt. That's the type of functionality I'm after. Any resources on how to do that sort of thing would be greatly appreciated.

2
  • 1
    Add the comment #!/usr/bin/env python to the beginning of your .py file and mark it as executable with chmod a+x random.py. Commented Feb 1, 2014 at 11:09
  • 1
    If you do what martineau suggests, you can also get rid of the .py extension if you want Commented Feb 1, 2014 at 12:08

2 Answers 2

3

What worked for me (Ubuntu) is putting a symbolic link in /usr/local/bin like this,

$> sudo ln -s /path/to/your/python/script/random.py /usr/local/bin/random
$> sudo chmod 755 /usr/local/bin/random

(If you want to automate this step just put these two commands in a bash script). Normally the terminal should now find the script and execute it if you just type

$> random arg1 arg2

Just make sure your python script starts with

#!/usr/bin/python
Sign up to request clarification or add additional context in comments.

Comments

2

If you use Setuptools for distributing you project, you can use Automatic Script Creation feature. Just put into setup.py script following code:

from setuptools import setup, find_packages
setup(
    name = "Random",
    version = "0.1",
    packages = find_packages(),
    entry_points = {
        'console_scripts': [
            'random = random:main_func',
        ],
    }
)

A shell script "random", which actually runs function "main_func" from module "random", will be available after project installation.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.