Last active
June 27, 2025 00:58
-
-
Save ernstki/61d554acf7e496130c8300620fb36910 to your computer and use it in GitHub Desktop.
`which` for Perl, R, and Python libraries
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
## | |
## Print where a Perl 5 library was included from | |
## | |
## Author: Symkat | |
## Source: http://www.symkat.com/find-a-perl-modules-path | |
## License: Distributed under the same terms as Perl itself | |
## <https://perldoc.perl.org/perlartistic> | |
## | |
use warnings; | |
use strict; | |
use File::Basename; | |
my $ME = basename($0); | |
my $ISSUESURL = 'https://gist.github.com/ernstki/61d554acf7e496130c8300620fb36910'; | |
my $USAGE = <<USAGE; | |
$ME - show where a Perl 5 library was loaded from | |
usage: | |
$ME <modulename> [<modulename> ...] | |
Problems? File an issue at $ISSUESURL. | |
see also: | |
http://www.symkat.com/find-a-perl-modules-path | |
USAGE | |
die "$USAGE" unless @ARGV; | |
# 0th place in @ARGV in Perl is first argument, not program name | |
if ($ARGV[0] =~ /--?h(elp)?/) { | |
print "$USAGE"; | |
exit; | |
} | |
foreach my $module (@ARGV) { | |
my $package = $module; | |
# From This::That to This/That.pm | |
s/::/\//g, s/$/.pm/ for $module; | |
if (require $module) { | |
print $package . " => " . $INC{$module} . "\n"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
Tell me where a Python package came from. | |
Author: Adam Anderson | |
Source: https://stackoverflow.com/a/42365058 | |
License: CC-BY-SA-4.0 | |
""" | |
from __future__ import print_function | |
ISSUESURL = 'https://gist.github.com/ernstki/61d554acf7e496130c8300620fb36910' | |
def pywhich(module_name): | |
module = __import__(module_name, globals(), locals(), [], 0) | |
return module.__file__ | |
if __name__ == "__main__": | |
import sys | |
import argparse | |
parser = argparse.ArgumentParser( | |
description="print where a Python package comes from", | |
epilog="Problems? Report bugs at: {}".format(ISSUESURL)) | |
parser.add_argument('pkgs', metavar='PKG', nargs='+', | |
help='package(s) to look for') | |
opts = parser.parse_args() | |
for pkg in opts.pkgs: | |
try: | |
print(pkg, "=>", pywhich(pkg)) | |
except ImportError: | |
parser.error("No such module '{}'.".format(pkg)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env Rscript | |
## | |
## Print the library path(s) of any package names given as arguments | |
## | |
## Author: Kevin Ernst | |
## Date: 22 April 2021 | |
## Source: https://gist.github.com/ernstki/61d554acf7e496130c8300620fb36910 | |
## License: Unlicense <https://unlicense.org> | |
## | |
# startsWith is not present in base until 3.6.x | |
if (R.Version()$major < 4 && R.Version()$minor < 6) { | |
startsWith <- function(x, prefix) { | |
substring(x, 1, nchar(prefix)) == prefix | |
} | |
} | |
helpmsg <- "usage: rwhich [-h|--help] PKG [PKG...]" | |
argv <- commandArgs(trailingOnly=TRUE) | |
if (!length(argv)) { | |
stop(helpmsg) | |
} | |
if (startsWith(argv[1], "-h") || startsWith(argv[1], "--h")) { | |
cat("rwhich - show where R libraries come from\n", helpmsg, sep="\n") | |
quit() | |
} | |
tryPackage <- function(p) { | |
tryCatch( | |
find.package(p), | |
# if you forget to make this a function, you'll get the error "attempt | |
# to apply to non-function" (without much helpful context) | |
# h/t: https://www.programmingr.com/r-error-messages/error-attempt-to-apply-non-function | |
error = function(e) { paste("No such package", p) } | |
) | |
} | |
ret <- 0 | |
for (pkg in argv) { | |
pkgpath <- tryPackage(pkg) | |
if (startsWith(pkgpath, "No such package")) { | |
ret <- ret + 1 | |
} | |
cat(cat(pkg, "=>", pkgpath), sep="\n") | |
} | |
quit(status=ret) | |
# vim: ft=r |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment