7

I'm looking for a way to get a list of all installed/importable python modules from a within a Jupyterlab notebook.

From the command line, I can get the list by running

py -3 -m pip freeze

(or)

pip freeze

In the Jupyterlab console, running 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/

For older versions of pip, it was possible to import pip and get a list from within a notebook.

The command was

help('modules')

This now gives a warning and returns nothing.

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 year old stackoverflow solutions like How can I get a list of locally installed Python modules? also no longer work.

Is there a proper way of doing this (without using a subprocess hack or running pip as an external program like "!pip")

576i
  • 7,579
  • 12
  • 55
  • 92

5 Answers5

16

You may run following snippet to the result.

!pip list
amkr
  • 639
  • 5
  • 8
  • Thanks, using "!" works like running pip as any external program. This does work, still I'm wondering if there's a better way to interact with pip. I'm updating the question to make that clearer. – 576i Jan 31 '20 at 09:21
  • 2
    If you are using a custom kernel based on a venv, this will execute pip on the main env packages right ? – Pierrick Rambaud Mar 08 '22 at 14:08
6

you can also try

!pip freeze

in your jupyter notebook. Hope it helps you.

Priyanka Jain
  • 471
  • 2
  • 11
3
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']

This works in Win10 with Python 3.6 & 3.7 (ipython, pip.version: '20.0.1') at least. I took a look at the source code in Lib\site-packages\pip.

mikey
  • 2,158
  • 1
  • 19
  • 24
  • I'm trying to get the name that colab uses for a file in my Google Drive. So, it's not an installed file, unless this means also modules created by me. – David Epstein Nov 18 '20 at 15:24
2

%pip list
instead of
!pip list

Insouciant
  • 19
  • 2
1

Try this :


help("modules")


....