简体   繁体   中英

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

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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