如何使用 Python 3.7 或更新版本查看 Jupyter Lab 中所有已安装的 Python 模块(如 pip freeze)?

How can I see all installed Python modules in Jupyter Lab (like pip freeze) with Python 3.7 or newer?

我正在寻找一种方法来获取 所有 installed/importable python 模块 的列表 来自a 在 Jupyterlab 笔记本中。

在命令行中,我可以通过 运行

获取列表
py -3 -m pip freeze

(或)

pip freeze

在 Jupyterlab 控制台中,运行 pip freeze returns

The following command must be run outside of the IPython shell:

    $ pip freeze

The Python package manager (pip) can only be used from outside of IPython.
Please reissue the `pip` command in a separate terminal or command prompt.

See the Python documentation for more information on how to install packages:

https://docs.python.org/3/installing/

对于旧版本的 pip,可以导入 pip 并从笔记本中获取列表。

命令是

help('modules')

这现在给出了警告,returns什么也没有。

c:\python37\lib\site-packages\IPython\kernel\__init__.py:13: ShimWarning: The `IPython.kernel` package has been deprecated since IPython 4.0.You should import from ipykernel or jupyter_client instead.
  "You should import from ipykernel or jupyter_client instead.", ShimWarning)

10 年前的 Whosebug 解决方案,如 How can I get a list of locally installed Python modules? 也不再有效。

是否有正确的方法(不使用子进程 hack 或 运行 pip 作为外部程序,如“!pip”)

您可以运行跟随结果的片段。

!pip list

试试这个:


help("modules")


.....

你也可以试试

!pip freeze

在你的 jupyter 笔记本中。希望对你有帮助。

import pip._internal.operations.freeze
_ = pip._internal.operations.freeze.get_installed_distributions()
print(sorted(["%s==%s" % (i.key, i.version) for i in _])[:10])
['absl-py==0.7.1',
 'aiml==0.9.2',
 'aio-utils==0.0.1',
 'aiocache==0.10.1',
 'aiocontextvars==0.2.2',
 'aiocqhttp==0.6.7',
 'aiodns==2.0.0',
 'aiofiles==0.4.0',
 'aiohttp-proxy==0.1.1',
 'aiohttp==3.6.2']

这至少适用于 Python 3.6 和 3.7 (ipython, pip.version: '20.0.1') 的 Win10。我查看了Lib\site-packages\pip.

中的源代码