Skip to main content

Search Python source file(s) for imported modules.

Project description

Installation & Usage

$ pip install ls-imports
$ python -m ls_imports ...

Or use the CLI through pipx:

$ pipx run ls-imports --help
Usage: ls-imports [OPTIONS] [PATHS]...

  Search Python source file(s) in PATHS for imported modules.

  Directories specified by PATHS are searched recursively for *.py files.

Options:
  -i, --ignore MODULE             Ignore a module and all its submodules. This
                                  option may be given multiple times.
  -R, --ignore-relative / --no-ignore-relative
                                  Ignore relative imports.
  -S, --ignore-stdlib / --no-ignore-stdlib
                                  Ignore standard library modules.
  -f, --show-files / --no-show-files
                                  For each import, list the files that import
                                  it.
  --version                       Show the version and exit.
  --help                          Show this message and exit.

Examples

Library

Setup:

>>> source = """
... import ast
... import click
... from .base import pkgfunc
... 
... def lazy_import():
...     import math
...     return math.sin(20.0)
... """
>>> with open('file.py', 'w') as f:
...     f.write(source)

Parse imports from a file:

>>> import ls_imports
>>> ls_imports.parse_file('file.py')
['math', 'ast', 'click', '.base']

Parse imports from a string:

>>> ls_imports.parse_source(source)
['math', 'ast', 'click', '.base']

Command-line interface

Exclude standard library modules:

$ python -m ls_imports --ignore-stdlib file.py
click

Exclude relative imports:

$ python -m ls_imports --ignore-relative file.py
ast
click
math

Search a directory recursively:

$ python -m ls_imports dir/

Search a large package for all third-party imports, and show which files import them:

$ git clone https://github.com/denavit/libdenavit-py
$ python -m ls_imports -RSf --ignore libdenavit libdenavit-py/src
matplotlib.pyplot
        libdenavit-py/src/libdenavit/OpenSees/get_fiber_data.py
        libdenavit-py/src/libdenavit/OpenSees/plotting.py
        libdenavit-py/src/libdenavit/OpenSees/uniaxial_material_analysis.py
        libdenavit-py/src/libdenavit/camber.py
        libdenavit-py/src/libdenavit/interaction_diagram_2d.py
        libdenavit-py/src/libdenavit/non_sway_column_2d.py
        libdenavit-py/src/libdenavit/section/RC.py
        libdenavit-py/src/libdenavit/section/circle_shape.py
        libdenavit-py/src/libdenavit/section/encased_composite.py
        libdenavit-py/src/libdenavit/section/fiber_section.py
        libdenavit-py/src/libdenavit/section/obround_shape.py
        libdenavit-py/src/libdenavit/section/rectangle_shape.py
        libdenavit-py/src/libdenavit/section/reinf.py
        libdenavit-py/src/libdenavit/sway_column_2d.py
numpy
        libdenavit-py/src/libdenavit/OpenSees/fiber_section.py
        libdenavit-py/src/libdenavit/camber.py
        libdenavit-py/src/libdenavit/cross_section_2d.py
        libdenavit-py/src/libdenavit/interaction_diagram_2d.py
        libdenavit-py/src/libdenavit/joist.py
        libdenavit-py/src/libdenavit/non_sway_column_2d.py
        libdenavit-py/src/libdenavit/section/ACI_phi.py
        libdenavit-py/src/libdenavit/section/ACI_strain_compatibility.py
        libdenavit-py/src/libdenavit/section/RC.py
        libdenavit-py/src/libdenavit/section/ccft.py
        libdenavit-py/src/libdenavit/section/circle_shape.py
        libdenavit-py/src/libdenavit/section/database/to_numpy.py
        libdenavit-py/src/libdenavit/section/encased_composite.py
        libdenavit-py/src/libdenavit/section/fiber_patches.py
        libdenavit-py/src/libdenavit/section/fiber_section.py
        libdenavit-py/src/libdenavit/section/obround_shape.py
        libdenavit-py/src/libdenavit/section/rectangle_shape.py
        libdenavit-py/src/libdenavit/section/reinf.py
        libdenavit-py/src/libdenavit/sway_column_2d.py
openseespy.opensees
        libdenavit-py/src/libdenavit/OpenSees/fiber_section.py
        libdenavit-py/src/libdenavit/OpenSees/get_fiber_data.py
        libdenavit-py/src/libdenavit/OpenSees/plotting.py
        libdenavit-py/src/libdenavit/OpenSees/uniaxial_material_analysis.py
        libdenavit-py/src/libdenavit/cross_section_2d.py
        libdenavit-py/src/libdenavit/non_sway_column_2d.py
        libdenavit-py/src/libdenavit/section/RC.py
        libdenavit-py/src/libdenavit/section/encased_composite.py
        libdenavit-py/src/libdenavit/sway_column_2d.py
pandas
        libdenavit-py/src/libdenavit/section/database/to_numpy.py
        libdenavit-py/src/libdenavit/section/fiber_section.py
pint
        libdenavit-py/src/libdenavit/unit_convert.py
scipy.optimize
        libdenavit-py/src/libdenavit/effective_length_factor.py
        libdenavit-py/src/libdenavit/non_sway_column_2d.py
        libdenavit-py/src/libdenavit/sway_column_2d.py
shapely.geometry
        libdenavit-py/src/libdenavit/interaction_diagram_2d.py

Prior work

Based on list-imports.

findimports is similar, but shows the reverse relation (for each file, shows the modules/names it imports).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ls-imports-0.1.2.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

ls_imports-0.1.2-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file ls-imports-0.1.2.tar.gz.

File metadata

  • Download URL: ls-imports-0.1.2.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for ls-imports-0.1.2.tar.gz
Algorithm Hash digest
SHA256 57753e16f2ad09455f1b99e8506a5d80bceee53e6b51a6ccdcf29bb318913489
MD5 1376b9ec7c8f9aa4bf5ef2208e4797ae
BLAKE2b-256 d50b4c6c5a4cd9f778cc5844af33033cde48e98fa72f0e891983caf98b260578

See more details on using hashes here.

File details

Details for the file ls_imports-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: ls_imports-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for ls_imports-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 772c4b9520a5144632344c70bff568733bab7d264ebbb55828bba9a6162be2c2
MD5 e2fc334bc71640e16318c38c7070aa86
BLAKE2b-256 7324abd7b99d707d679018adb5563344ff2be210ccb2c9001e137983e2b3d815

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page