Script to list non-standard modules imported by Jupyter notebooks. Run this in a directory containing `.ipynb` files and it will generate a `requirements-unpinned-tocheck.txt` file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
import json | |
import re | |
import imp | |
import os.path | |
import sys | |
python_path = os.path.dirname(sys.executable).replace('bin', 'lib') | |
imports = [] | |
for nb in Path.cwd().glob('*.ipynb'): | |
if not nb.name.startswith('.') and not nb.name.startswith('Untitled'): | |
nb_json = json.loads(nb.read_bytes()) | |
for cell in nb_json['cells']: | |
for line in cell['source']: | |
if match := re.search(r'^\s*import ([a-zA-Z_]+)(?! from)', line): | |
imports.append(match.group(1)) | |
elif match := re.search(r'^\s*import [a-zA-Z_\.]+ from ([a-zA-Z_]+)', line): | |
imports.append(match.group(1)) | |
external_imports = ['jupyterlab\n'] | |
for imported_mod in list(set(imports)): | |
module_path = imp.find_module(imported_mod)[1] | |
if module_path: | |
if 'site-packages' in module_path or python_path in module_path: | |
external_imports.append(imported_mod + '\n') | |
#print(external_imports) | |
with Path('requirements-unpinned-tocheck.txt').open('w') as req_file: | |
req_file.writelines(external_imports) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment