Skip to content

Instantly share code, notes, and snippets.

@wragge
Last active March 14, 2021 02:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wragge/0c1f3e70c5455c6eeeee08ded8cb6b0e to your computer and use it in GitHub Desktop.
Save wragge/0c1f3e70c5455c6eeeee08ded8cb6b0e to your computer and use it in GitHub Desktop.
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.
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