Skip to content

Instantly share code, notes, and snippets.

@tholeb
Last active June 23, 2022 22:12
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 tholeb/b510e7bbb842b1c4c4255047cebaac70 to your computer and use it in GitHub Desktop.
Save tholeb/b510e7bbb842b1c4c4255047cebaac70 to your computer and use it in GitHub Desktop.
Check if a python package exists (without pip)

Get installed python packages without pip

Use this simple py script to get all (or a few specific) installed py libs

# Get packages versions without pip
# Usage:
# - python script.py ==> To get all the installed packages
# - ptyhon script.py arg1 arg2 argn ==> to get the version of the packages passed in argmuents
import pkg_resources
import sys
installed_packages = {d.project_name: d.version for d in pkg_resources.working_set}
# Get args number
args = len(sys.argv)
# No args
if args == 1:
# List all packages
print("All packages:")
print(installed_packages)
else:
# List a specific package (passed in args via CLI)
for i in range(1, args):
pkg = sys.argv[i]
if pkg in installed_packages:
print(pkg, installed_packages[pkg])
else:
print(pkg, "is not installed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment