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))
# 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)))
# 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))
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}")
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)
9条答案
按热度按时间7fhtutme1#
这将获取所有已安装的软件包
从当前笔记本中获取包列表
hxzsmxv22#
我把已经提供的两个解决方案结合起来,拼凑出了这个答案。我最终想生成一个requirements.txt类型的文件,以便在令人敬畏的Binder网站上使用。显然,我不想
pip freeze
我的整个系统,但我也不想为每台笔记本创建单独的虚拟环境(这是我的问题的根源)。这将输出一个格式良好的requirements.txt类型字符串,并处理使用
import from
而不仅仅是import
时所涉及的一些复杂问题。从当前笔记本获取本地导入的模块
样本输出:
.get_installed_distributions()
方法。使用pkg_resources.working_set
代替。enxuqcxy3#
一行:
输出:
bweufnob4#
我对@Alex P.米勒的回答做了一些改进,这样(抱歉我没有足够的代表直接对他的回答“评论”)
1.在区分大小写导致问题时自动处理模块名称
1.还将没有版本号的模块列为“unknown”,以表明无法找到匹配项。
1.还列出了内置模块(如果可以检测到)。
qyuhtwio5#
改编自加福蒂比的回答:一个更短的版本,只列出明确的包列表。我发现这适合记忆jupyter笔记本中使用的最重要的包的版本(供其他读者或将来使用):
输出:
输出:
rt4zxlrg6#
另一种解决方案(基于Vivek的answer):
样本输出:
zbq4xfa07#
我认为基于
pip
的方法在功能方面上级,但OP可能试图回忆Jupyter的version_information
扩展的名称:https://pypi.org/project/version_information/2ekbmq328#
由于这些答案有点过时,简单的解决方案对我不起作用,我花了一些时间才在网上找到一个简单有效的解决方案:
izkcnapc9#
我有一些问题,只是做写在一个空的细胞pip列表,但一旦我运行它在一个全新的文件,我没有任何问题,并得到了所有的库安装在笔记本电脑!