Use this simple py script to get all (or a few specific) installed py libs
Last active
June 23, 2022 22:12
-
-
Save tholeb/b510e7bbb842b1c4c4255047cebaac70 to your computer and use it in GitHub Desktop.
Check if a python package exists (without pip)
This file contains 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
# 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