D-Bug
D-Bug

Reputation: 666

How to define a system-wide alias for a Python script?

I am working on Mac OS X and I have a Python script which is going to be called by other scripts and programs (Apple's launchd in particular). I could call it with

python /Users/xyz/long/absolute/path/to/script.py arg1 arg2

Since the location of the script might change, I want to decouple other scripts and the launchd configuration files from the actual location, so that a call to the script looks like

script arg1 arg2

Defining an alias for Bash in $HOME/.bash_profile does not work, since launchd does not know about the alias.

What is the best way to define a "sytem-wide alias" or something equivalent?

Upvotes: 2

Views: 2204

Answers (2)

Mark Biek
Mark Biek

Reputation: 150819

I usually make a symbolic link and put it in /usr/bin (assuming /usr/bin is part of your PATH)

(In a terminal. You may have to use sudo ln -s depending on the permissions.

ln -s /Users/xyz/long/absolute/path/to/script.py /usr/bin/script.py

If you take Rory's advice and put the #!/usr/bin/python at the beginning of the script, you'll also need to make the script executable.

chmod a+x /Users/xyz/long/absolute/path/to/script.py

Upvotes: 6

Amandasaurus
Amandasaurus

Reputation: 60759

As well as doing a symlink, you can put "#! /path/to/python" at the start of the script and make it executabe. Then you don't have to call it with "python /Users/big/long/path/script.py"

Upvotes: 3

Related Questions