README.txt 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. which.py -- a portable GNU which replacement
  2. ============================================
  3. Download the latest which.py packages from here:
  4. (source) http://trentm.com/downloads/which/1.1.0/which-1.1.0.zip
  5. Home : http://trentm.com/projects/which/
  6. License : MIT (see LICENSE.txt)
  7. Platforms : Windows, Linux, Mac OS X, Unix
  8. Current Version : 1.1
  9. Dev Status : mature, has been heavily used in a commercial product for
  10. over 2 years
  11. Requirements : Python >= 2.3 (http://www.activestate.com/ActivePython/)
  12. What's new?
  13. -----------
  14. I have moved hosting of `which.py` from my old [Starship
  15. pages](http://starship.python.net/~tmick/) to this site. These starter
  16. docs have been improved a little bit. See the [Change Log](#changelog)
  17. below for more.
  18. **WARNING**: If you are upgrading your `which.py` and you also use my
  19. [process.py](../process/) module, you must upgrade `process.py` as well
  20. because of the `_version_/__version__` change in v1.1.0.
  21. Why which.py?
  22. -------------
  23. `which.py` is a small GNU-which replacement. It has the following
  24. features:
  25. - it is portable (Windows, Linux, Mac OS X, Un*x);
  26. - it understands PATHEXT and "App Paths" registration on Windows
  27. (i.e. it will find everything that `start` does from the command shell);
  28. - it can print all matches on the PATH;
  29. - it can note "near misses" on the PATH (e.g. files that match but may
  30. not, say, have execute permissions); and
  31. - it can be used as a Python module.
  32. I also would be happy to have this be a replacement for the `which.py` in the
  33. Python CVS tree at `dist/src/Tools/scripts/which.py` which is
  34. Unix-specific and not usable as a module; and perhaps for inclusion in
  35. the stdlib.
  36. Please send any feedback to [Trent Mick](mailto:TrentM@ActiveState.com).
  37. Install Notes
  38. -------------
  39. Download the latest `which.py` source package, unzip it, and run
  40. `python setup.py install`:
  41. unzip which-1.1.0.zip
  42. cd which-1.1.0
  43. python setup.py install
  44. If your install fails then please visit [the Troubleshooting
  45. FAQ](http://trentm.com/faq.html#troubleshooting-python-package-installation).
  46. `which.py` can be used both as a module and as a script. By default,
  47. `which.py` will be installed into your Python's `site-packages`
  48. directory so it can be used as a module. On *Windows only*, `which.py`
  49. (and the launcher stub `which.exe`) will be installed in the Python
  50. install dir to (hopefully) put `which` on your PATH.
  51. On Un*x platforms (including Linux and Mac OS X) there is often a
  52. `which` executable already on your PATH. To use this `which` instead of
  53. your system's on those platforms you can manually do one of the
  54. following:
  55. - Copy `which.py` to `which` somewhere on your PATH ahead of the system
  56. `which`. This can be a symlink, as well:
  57. ln -s /PATH/TO/site-packages/which.py /usr/local/bin/which
  58. - Python 2.4 users might want to use Python's new '-m' switch and setup
  59. and alias:
  60. alias which='python -m which'
  61. or stub script like this:
  62. #!/bin/sh
  63. python -m which $@
  64. Getting Started
  65. ---------------
  66. Currently the best intro to using `which.py` as a module is its module
  67. documentation. Either install `which.py` and run:
  68. pydoc which
  69. take a look at `which.py` in your editor or [here](which.py), or read
  70. on. Most commonly you'll use the `which()` method to find an
  71. executable:
  72. >>> import which
  73. >>> which.which("perl")
  74. '/usr/local/bin/perl'
  75. Or you might want to know if you have multiple versions on your path:
  76. >>> which.whichall("perl")
  77. ['/usr/local/bin/perl', '/usr/bin/perl']
  78. Use `verbose` to see where your executable is being found. (On Windows
  79. this might not always be so obvious as your PATH environment variable.
  80. There is an "App Paths" area of the registry where the `start` command
  81. will find "registered" executables -- `which.py` mimics this.)
  82. >>> which.whichall("perl", verbose=True)
  83. [('/usr/local/bin/perl', 'from PATH element 10'),
  84. ('/usr/bin/perl', 'from PATH element 15')]
  85. You can restrict the searched path:
  86. >>> which.whichall("perl", path=["/usr/bin"])
  87. ['/usr/bin/perl']
  88. There is a generator interface:
  89. >>> for perl in which.whichgen("perl"):
  90. ... print "found a perl here:", perl
  91. ...
  92. found a perl here: /usr/local/bin/perl
  93. found a perl here: /usr/bin/perl
  94. An exception is raised if your executable is not found:
  95. >>> which.which("fuzzywuzzy")
  96. Traceback (most recent call last):
  97. ...
  98. which.WhichError: Could not find 'fuzzywuzzy' on the path.
  99. >>>
  100. There are some other options too:
  101. >>> help(which.which)
  102. ...
  103. Run `which --help` to see command-line usage:
  104. $ which --help
  105. Show the full path of commands.
  106. Usage:
  107. which [<options>...] [<command-name>...]
  108. Options:
  109. -h, --help Print this help and exit.
  110. -V, --version Print the version info and exit.
  111. -a, --all Print *all* matching paths.
  112. -v, --verbose Print out how matches were located and
  113. show near misses on stderr.
  114. -q, --quiet Just print out matches. I.e., do not print out
  115. near misses.
  116. -p <altpath>, --path=<altpath>
  117. An alternative path (list of directories) may
  118. be specified for searching.
  119. -e <exts>, --exts=<exts>
  120. Specify a list of extensions to consider instead
  121. of the usual list (';'-separate list, Windows
  122. only).
  123. Show the full path to the program that would be run for each given
  124. command name, if any. Which, like GNU's which, returns the number of
  125. failed arguments, or -1 when no <command-name> was given.
  126. Near misses include duplicates, non-regular files and (on Un*x)
  127. files without executable access.
  128. Change Log
  129. ----------
  130. ### v1.1.0
  131. - Change version attributes and semantics. Before: had a _version_
  132. tuple. After: __version__ is a string, __version_info__ is a tuple.
  133. ### v1.0.3
  134. - Move hosting of which.py to trentm.com. Tweaks to associated bits
  135. (README.txt, etc.)
  136. ### v1.0.2:
  137. - Rename mainline handler function from _main() to main(). I can
  138. conceive of it being called from externally.
  139. ### v1.0.1:
  140. - Add an optimization for Windows to allow the optional
  141. specification of a list of exts to consider when searching the
  142. path.
  143. ### v1.0.0:
  144. - Simpler interface: What was which() is now called whichgen() -- it
  145. is a generator of matches. The simpler which() and whichall()
  146. non-generator interfaces were added.
  147. ### v0.8.1:
  148. - API change: 0.8.0's API change making "verbose" output the default
  149. was a mistake -- it breaks backward compatibility for existing
  150. uses of which in scripts. This makes verbose, once again, optional
  151. but NOT the default.
  152. ### v0.8.0:
  153. - bug fix: "App Paths" lookup had been crippled in 0.7.0. Restore that.
  154. - feature/module API change: Now print out (and return for the module
  155. interface) from where a match was found, e.g. "(from PATH element 3)".
  156. The module interfaces now returns (match, from-where) tuples.
  157. - bug fix: --path argument was broken (-p shortform was fine)
  158. ### v0.7.0:
  159. - bug fix: Handle "App Paths" registered executable that does not
  160. exist.
  161. - feature: Allow an alternate PATH to be specified via 'path'
  162. optional argument to which.which() and via -p|--path command line
  163. option.
  164. ### v0.6.1:
  165. - first public release