Arun Abraham
Arun Abraham

Reputation: 4037

How to execute a python command line utility from the terminal in any directory

I have just written a python script to do some batch file operations. I was wondering how i could keep it in some common path like rest of the command line utilities like cd, ls, grep etc.

What i expect is something like this to be done from any directory -

$ script.py arg1 arg2

Upvotes: 0

Views: 248

Answers (3)

Att Righ
Att Righ

Reputation: 1779

An alternative approach is to create a python package with entry points and install the program, rather than changing $PATH (using setuptools in setup.py). This has some advantages:

  • Works outside of shells
  • Reduces the amount of system-wide configuration, or at least puts it all in side the package.

See Explain Python entry points? and python: simple example for a python egg with a one-file source file? for details.

This has the advantage of keeping all your settings in one place.

You can use the --develop option so that you can still edit your code in place and the --user option to avoid messing python for other users.

Upvotes: 0

hd1
hd1

Reputation: 34657

Add the directory it is stored in to your PATH variable? From your prompt, I'm guessing you're using an sh-like shell and from your tags, I'm further assuming OS X. Go into your .bashrc and make the necessary changes.

Upvotes: 0

poke
poke

Reputation: 387557

Just put the script directory into the PATH environment variable, or alternatively put the script in a location that is already in the PATH. On Unix systems, you usually use /home/<nick>/bin for your own scripts and add that to the PATH.

Upvotes: 1

Related Questions