用于列出Jupyter笔记本中使用的软件包版本的软件包

时间:2016-11-04 17:50:17

标签: python python-3.x

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

提前致谢!

9 个答案:

答案 0 :(得分:8)

这将获得所有已安装的软件包

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 :(得分:6)

我通过结合已经提供的两种解决方案来拼凑这个答案。我最终想要生成一个requirements.txt类型文件,以便与令人敬畏的Binder网站一起使用。显然,我不想pip freeze我的整个系统,但我也不想为每个笔记本创建单独的虚拟环境(最终我的问题源于此)。

这会输出格式良好的requirements.txt类型字符串,并处理使用import from而不仅仅是import时涉及的一些复杂问题。

从当前笔记本

获取本地导入的模块
import pkg_resources
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]

        elif isinstance(val, type):
            name = val.__module__.split(".")[0]

        # Some packages are weird and have different
        # imported names vs. system/pip names. Unfortunately,
        # there is no systematic way to get pip names from
        # a package's imported name. You'll have to had
        # exceptions to this list manually!
        poorly_named_packages = {
            "PIL": "Pillow",
            "sklearn": "scikit-learn"
        }
        if name in poorly_named_packages.keys():
            name = poorly_named_packages[name]

        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 pkg_resources.working_set:
    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

EDITED 2018-04-21 :pip版本10停止支持.get_installed_distributions()方法。改为使用pkg_resources.working_set

答案 2 :(得分:4)

单线:

->

输出:

x86

答案 3 :(得分:2)

改编自gafortiby的答案:一种较短的版本,仅列出了明确的软件包列表。我发现这适合记住jupyter笔记本中使用的最重要软件包的版本(供其他读者或将来使用):

import pkg_resources
# list packages to be checked
root_packages = [
    'geoviews', 'geopandas', 'pandas', 'numpy', 
    'matplotlib', 'shapely', 'cartopy', 'holoviews',
    'mapclassify', 'fiona', 'bokeh']
# print versions, but check if package is imported first
for m in pkg_resources.working_set:
    if m.project_name.lower() in root_packages:
        print(f"{m.project_name}=={m.version}")

输出:

Shapely==1.7.0
pandas==1.0.1
numpy==1.18.1
matplotlib==3.1.3
mapclassify==2.2.0
holoviews==1.12.7
geoviews==1.6.6
geopandas==0.6.3
Fiona==1.8.13
Cartopy==0.17.0
bokeh==1.4.0

显示效果更好的增强版:

import pkg_resources
from IPython.display import display
import pandas as pd

root_packages = [
    'geoviews', 'geopandas', 'pandas', 'numpy', 'cloudpickle',
    'matplotlib', 'shapely', 'cartopy', 'holoviews',
    'mapclassify', 'fiona', 'bokeh', 'pyproj', 'ipython',
    'jupyterlab']
root_packages.sort(reverse=True)
root_packages_list = []

for m in pkg_resources.working_set:
    if m.project_name.lower() in root_packages:
        root_packages_list.append([m.project_name, m.version])

display(pd.DataFrame(
            root_packages_list,
            columns=["package", "version"]
        ).set_index("package").transpose())

输出: Example output package versions

答案 4 :(得分:1)

我对@Alex P. Miller的答案做了一些改进,以便 (对不起,我没有足够的代表直接在他的回答上“评论”)

  1. 自动使用区分大小写会导致问题的模块名称
  2. 还将没有版本号的模块列为“未知”,以明确找不到匹配项。
  3. 还列出了内置模块(如果可以检测到的话)。
# show versions of packages
# adopted from https://stackoverflow.com/questions/40428931/package-for-listing-version-of-packages-used-in-a-jupyter-notebook

    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]
            elif isinstance(val, type):
                name = val.__module__.split(".")[0]
            # Some packages are weird and have different
            # imported names vs. system/pip names. Unfortunately,
            # there is no systematic way to get pip names from
            # a package's imported name. You'll have to add
            # exceptions to this list manually!
            poorly_named_packages = {
                "sklearn": "scikit-learn"
            }
            if name in poorly_named_packages.keys():
                name = poorly_named_packages[name]
            yield name.lower()
    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
    modules = []
    for m in sys.builtin_module_names:
        if m.lower() in imports and m !='builtins':
            modules.append((m,'Python BuiltIn'))
            imports.remove(m.lower())

    for m in pkg_resources.working_set:
        if m.project_name.lower() in imports and m.project_name!="pip":
            modules.append((m.project_name, m.version))
            imports.remove(m.project_name.lower())

    for m in sys.modules:
        if m.lower() in imports and m !='builtins':
            modules.append((m,'unknown'))

    # print('System=='+platform.system()+' '+platform.release()+'; Version=='+platform.version())
    for r in modules:
        print("{}=={}".format(*r))

答案 5 :(得分:0)

我认为基于... File filetoopen = chooser.getSelectedFile(); List<String> allLines = null; try { allLines = Files.readAllLines(filetoopen.toPath(), StandardCharsets.UTF_8); } catch (IOException e1) { e1.printStackTrace(); } ... 的方法在功能方面是优越的,但是OP可能试图为Jupyter召回pip扩展名:https://pypi.org/project/version_information/ < / p>

答案 6 :(得分:0)

另一种解决方案(基于Vivek的answer):

import types

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

excludes = ['builtins', 'types', 'sys']

imported_modules = [module for module in imports() if module not in excludes]

clean_modules = []

for module in imported_modules:

    sep = '.'  # to handle 'matplotlib.pyplot' cases
    rest = module.split(sep, 1)[0]
    clean_modules.append(rest)

changed_imported_modules = list(set(clean_modules))  # drop duplicates

pip_modules = !pip freeze  # you could also use `!conda list` with anaconda

for module in pip_modules:
    name, version = module.split('==')
    if name in changed_imported_modules:
        print(name + '\t' + version)

示例输出:

astropy 3.2.1
matplotlib  3.1.0
numpy   1.16.4
pandas  0.25.0

答案 7 :(得分:0)

由于这些答案有点过时,所以更简单的解决方案对我不起作用,找到easy, working solution elsewhere online需要一些绊脚石:

from sinfo import sinfo
sinfo()

答案 8 :(得分:0)

我在空单元格中写作时遇到了一些问题 点子列表 但是一旦我在一个全新的文件中运行它,我就完全没有问题,并且在笔记本中安装了所有库!