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.py [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.0.tar.gz (5.1 kB view details)

Uploaded Source

Built Distribution

ls_imports-0.1.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for ls-imports-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f9aabb1a55384c6fde135fd8cfe01481735c4ba7d1c99062de103854752e2e66
MD5 ebb3971cc4814639fcf12da05290eb11
BLAKE2b-256 cde42c2f21c0fcbc4a540b64838bf87f1d9408fc28ca9c06d77953fa932c7ef6

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for ls_imports-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 11064ff1af6860e2d96464032e7762e11a52b40ace6f72a4415679c9b4dda98d
MD5 15f328a34bff1ac68452e0780a142c80
BLAKE2b-256 96544590231f31e39acf8d070904d2bdc58a2a84e71e6c9e1e42616f780b7e39

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