-
Notifications
You must be signed in to change notification settings - Fork 0
/
active_pkg.py
74 lines (56 loc) · 2.48 KB
/
active_pkg.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/Users/blacar/opt/anaconda3/envs/stats_rethinking/bin/python
"""
Purpose: Function to list packages in the current notebook
Created 2/8/21
@author: benlacar
"""
# I think this function has to be run in the jupyter notebook
# def imports():
# """
# This is a list of active packages, but without versions
# """
# import types
# for name, val in globals().items():
# if isinstance(val, types.ModuleType):
# yield val.__name__
# This will be run within the notebook.
# imported_packages = list(imports())
def show_pkg_vers_in_nb(imported_packages):
"""
Objective of this function is to show the packages and versions
that are active in this notebook.
Partly informed by these links:
https://stackoverflow.com/questions/40428931/package-for-listing-version-of-packages-used-in-a-jupyter-notebook
https://stackoverflow.com/questions/4256107/running-bash-commands-in-python/51950538
"""
# Shows list of packages and versions from pip...
# # but doesn't have conda hence the next command
# pip_freeze_contents = !pip freeze
# print('Test text at top of function')
# This line acccounts for imported submodules (e.g. matplotlib.pyplot)
imported_packages = [i.split('.')[0] for i in imported_packages]
# Shows list of packages and versions from pip and conda
bash_cmd = "conda list"
#bash_cmd = 'bash -c "source activate stats_rethinking";
import subprocess
# process = subprocess.Popen(bash_cmd.split(), stdout=subprocess.PIPE)
# process = subprocess.Popen(bash_cmd, shell=True, stdout=subprocess.PIPE)
# # conda_pkgs, error = process.communicate
process = subprocess.run(bash_cmd.split(), capture_output=True)
conda_pkgs = str(process.stdout).split('\\n') # turns bytes output to a list
# print("conda packages: ", conda_pkgs)
# This essentially takes an intersection of the two lists
for pkg_vers in conda_pkgs:
# Header row
if pkg_vers.startswith('# Name'):
print(pkg_vers)
elif pkg_vers.split(' ')[0] in imported_packages:
print(pkg_vers)
# Other research for historical purposes ----------
# From here:
# https://stackoverflow.com/questions/38267791/how-to-i-list-imported-modules-with-their-version
# This block below doesn't work for me
# import sys
# for module_name in modules:
# module = sys.modules[module_name]
# print module_name, getattr(module, '__version__', 'unknown')