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.1.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: ls-imports-0.1.1.tar.gz
  • Upload date:
  • Size: 5.2 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.1.tar.gz
Algorithm Hash digest
SHA256 e46557bb56572eb64a4846ca21c562aff80353bf974ee00f45f79cb73fc4ce03
MD5 c3628ce5d315df756fdcb404cc13f850
BLAKE2b-256 b212c7d9ab7611a6ef14d5b4ab7ccca609e4182a169a3185ddd1eb53a05570cd

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ls_imports-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 348984fb79a6f00265a896b6a3b7c2532a91866a25fdeffa020e6797b83c4de9
MD5 024adf0448ad1a716758adc67baf412a
BLAKE2b-256 be30f093bf06b2bc4039dfa55f7b45f3832fce3fa9485bf82153d39b5408f6fd

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