'query what libraries are on the host of the python notebook
In a jupyter notebook, I can fairly easily check with python code if some libraries are installed on the current kernel of the python notebook.
However there is also the "host" kernel which has its own python env (ie the python process that was launched when jupyter notebook was called). Depending on what libraries/extensions were installed on the host, it may not be possible to do specific things on the jupyter notebook client itself.
Is there a way to query what libraries/modules/extensions are installed on the host, from the "client" notebook ? thanks
Solution 1:[1]
Did you test !pip list or !pip freeze?
To list locally installed packages and their version # within a pipenv environment, cd into a pipenv project and enter the following command:
pipenv lock -r
Solution 2:[2]
I guess you can use !pip list to show the modules/libraries installed on the current env. But I don't think you can view extensions.
Solution 3:[3]
Please use !pip list --local or !pip freeze --local if you're using a virtual environment.
Solution 4:[4]
This could get you started:
import sys
import os
for i in sys.path:
try:
print("->", i, ":")
print(repr(os.listdir(i or ".")))
except Exception:
print("can't list", i)
pass
NOTE that my code isn't a complete solution, but it is a start. I will attempt to improve it shortly, but even as it is it may be immediately useful for you to develop your own solution.
What it doesn't do:
- it does not determine whether it found a python file or a directory containing a valid package or something that isn't either (i.e. is not 'import'-able)
- It skips over zipped packages (such as this item that could be in sys.path: /home/pyodide/lib/python39.zip)
- it does not attempt to find built-in packages (those that are compiled into Python itself). Most of those would be already loaded, so you will find them in
sys.modules, though
If you need to test for individual packages, rather than get a whole list: you may be better off using 'import' inside a try/except statement:
try:
import something
except ImportError:
pass
# do what you want if the package isn't available
Solution 5:[5]
If I understand correctly, you can run:
help("modules")
Or you can use pydoc for your work:
import pydoc
!pydoc modules
Solution 6:[6]
I believe !pip list should work- if not, then pydoc most likely would, see above.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | BarzanHayati |
| Solution 2 | |
| Solution 3 | Esraa Abdelmaksoud |
| Solution 4 | Leo K |
| Solution 5 | Javad Nikbakht |
| Solution 6 | Pajama |
