Skip to content

Instantly share code, notes, and snippets.

@igniteflow
Created October 6, 2015 10:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igniteflow/0b26441d3617dc344565 to your computer and use it in GitHub Desktop.
Save igniteflow/0b26441d3617dc344565 to your computer and use it in GitHub Desktop.
List python packages installed in a directory (such as with pip install --target my_dir)
import sys
import pkg_resources
"""
List python packages installed in a directory (such as with pip install --target my_dir)
python list-target.py /path/to/my_dir # can be relative or absolute
"""
try:
target_dir = sys.argv[1]
except IndexError:
print('ERROR: Pass the target_dir as an argument. e.g. python list-target.py my_dir')
exit(1)
print('Looking for packages in {} ...'.format(target_dir))
env = pkg_resources.Environment([target_dir])
dists = []
for project in env:
for dist in env[project]:
dists.append("{}=={}".format(dist.project_name, dist.version))
print('\n'.join(sorted(dists)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment