Last active
August 29, 2015 14:08
-
-
Save ericchiang/38b9acc142c37d561fb2 to your computer and use it in GitHub Desktop.
Find pip versions of modules
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
import types | |
import warnings | |
def lookup(obj): | |
"""Take a python object or class and determine the module it's imported | |
from and the version of that module. | |
""" | |
if type(obj) == types.ModuleType: | |
try: | |
module = obj.__name__ | |
version = obj.__version__ | |
except AttributeError: | |
return | |
else: | |
try: | |
module = obj.__module__.split(".")[0] | |
exec("import %s" % (module,)) | |
version = eval("%s.__version__" % (module, )) | |
except AttributeError: | |
return | |
module = pip_names.get(module, module) | |
return (module, version) | |
pip_names = { | |
"skimage": "scikit-image", | |
"sklearn": "scikit-learn", | |
} |
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
import subprocess | |
import pandas as pd | |
""" | |
Question1: How many pip packages have the same import name as pip install name? | |
Question2: How many packages have a .__version__ property | |
""" | |
modules = subprocess.check_output(["pip","freeze"]).split() | |
modules = map(lambda m: m.split("==")[0], modules) | |
data = [] | |
for module in modules: | |
module_data = {} | |
module_data["pip-name"] = module | |
module_data["same_import"] = False | |
try: | |
import_statement = "import " + module | |
exec(import_statement) | |
module_data["same_import"] = True | |
version = eval(module + ".__version__") | |
module_data["has_version"] = True | |
except: | |
pass | |
data.append(module_data) | |
df = pd.DataFrame(data) | |
df.to_csv("module_data.csv", index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment