2016-11-04 77 views
3

我似乎记得有一个软件包可以打印Jupyter笔记本中使用的Python软件包的版本和相关信息,因此其结果是可重现的。但我不记得包裹的名字。你们中的任何一个人都能指引我走向正确的方向吗Jupyter笔记本中使用的软件包列表版本的包装

在此先感谢!

+1

'PIP freeze'显示有关每个包信息。或者你可以使用'conda list'。 – estebanpdl

+1

pip是否冻结打印关于笔记本内包装的信息? – msx

+0

这些示例显示* terminal *或*命令提示符*中的信息。 – estebanpdl

回答

5

这得到所有已安装的软件包

import pip #needed to use the pip functions 
for i in pip.get_installed_distributions(local_only=True): 
    print(i) 

从目前的笔记本

import types 
def imports(): 
    for name, val in globals().items(): 
     if isinstance(val, types.ModuleType): 
      yield val.__name__ 
list(imports()) 
+1

感谢您的回复,但我正在查找仅列出相关笔记本电脑中使用的软件包的软件包。 – msx

+0

这个问题可能的重复 –

+0

http://stackoverflow.com/questions/4858100/how-to-list-imported-modules –

0

得到的软件包列表,我通过合并已经提供了两种解决方案鹅卵石这个答案。我最终想要生成一个requirements.txt类型的文件,以便与真棒Binder网站一起使用。显然,我不想为我的整个系统pip freeze,但我也不想为每个笔记本创建单独的虚拟环境(这最终是我的问题源于此)。

这将输出一个格式良好的requirements.txt类型字符串,并处理在使用import from而不仅仅是import时涉及的一些错综复杂的情况。

# Get locally imported modules from current notebook 
import pip 
import types 
def get_imports(): 
    for name, val in globals().items(): 
     if isinstance(val, types.ModuleType): 
      # Split ensures you get root package, 
      # not just imported function 
      name = val.__name__.split(".")[0] 

      # Some packages are weird and have different 
      # imported names vs. system names 
      if name == "PIL": 
       name = "Pillow" 
      yield name 
imports = list(set(get_imports())) 

# The only way I found to get the version of the root package 
# from only the name of the package is to cross-check the names 
# of installed packages vs. imported packages 
requirements = [] 
for m in pip.get_installed_distributions(): 
    if m.project_name in imports and m.project_name!="pip": 
     requirements.append((m.project_name, m.version)) 

for r in requirements: 
    print("{}=={}".format(*r)) 

示例输出:

scipy==0.19.0 
requests==2.18.1 
Pillow==5.0.0 
numpy==1.13.0 
matplotlib==2.0.2