python-3.x 用于列出Jupyter笔记本中使用的软件包版本的软件包

qvsjd97n  于 2023-01-14  发布在  Python
关注(0)|答案(9)|浏览(140)

我好像记得有一个软件包打印了Jupyter笔记本中使用的Python软件包的版本和相关信息,所以它的结果是可复制的。但是我不记得软件包的名称了。你们谁能给我指一下正确的方向吗?
先谢了!

7fhtutme

7fhtutme1#

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

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())
hxzsmxv2

hxzsmxv22#

我把已经提供的两个解决方案结合起来,拼凑出了这个答案。我最终想生成一个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 add
        # 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
  • 编辑日期:2018年4月21日 *:pip版本10停止支持.get_installed_distributions()方法。使用pkg_resources.working_set代替。
enxuqcxy

enxuqcxy3#

一行:

# In[1]:
import pandas as pd
import numpy as np
import tensorflow as tf

print('\n'.join(f'{m.__name__}=={m.__version__}' for m in globals().values() if getattr(m, '__version__', None)))

输出:

pandas==1.1.1
numpy==1.19.1
tensorflow==2.2.0
bweufnob

bweufnob4#

我对@Alex P.米勒的回答做了一些改进,这样(抱歉我没有足够的代表直接对他的回答“评论”)
1.在区分大小写导致问题时自动处理模块名称
1.还将没有版本号的模块列为“unknown”,以表明无法找到匹配项。
1.还列出了内置模块(如果可以检测到)。

# 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))
qyuhtwio

qyuhtwio5#

改编自加福蒂比的回答:一个更短的版本,只列出明确的包列表。我发现这适合记忆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())

输出:

rt4zxlrg

rt4zxlrg6#

另一种解决方案(基于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
zbq4xfa0

zbq4xfa07#

我认为基于pip的方法在功能方面上级,但OP可能试图回忆Jupyter的version_information扩展的名称:https://pypi.org/project/version_information/

2ekbmq32

2ekbmq328#

由于这些答案有点过时,简单的解决方案对我不起作用,我花了一些时间才在网上找到一个简单有效的解决方案:

from sinfo import sinfo
sinfo()
izkcnapc

izkcnapc9#

我有一些问题,只是做写在一个空的细胞pip列表,但一旦我运行它在一个全新的文件,我没有任何问题,并得到了所有的库安装在笔记本电脑!

相关问题