Skip to content

Instantly share code, notes, and snippets.

Last active March 14, 2021 02:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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'.') and not'Untitled'):
nb_json = json.loads(nb.read_bytes())
for cell in nb_json['cells']:
for line in cell['source']:
if match :='^\s*import ([a-zA-Z_]+)(?! from)', line):
elif match :='^\s*import [a-zA-Z_\.]+ from ([a-zA-Z_]+)', line):
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')
with Path('requirements-unpinned-tocheck.txt').open('w') as req_file:
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment