Skip to main content

Small Python utility to extract imported libraries from source code files in various programming languages using tree-sitter.

Project description

Extract Imported Libraries

Small Python utility to extract imported libraries from source code files in various programming languages using tree-sitter.

Supported Languages

  • Python
  • R

Installation

You can install the package via pip:

pip install pyeil

Usage

Single File

from eil import Extractor

extractor = Extractor()
imported_libs = extractor.extract_from_file("path/to/your/file.py")
print(imported_libs)
# Output:
# ImportedLibraries(
#   stdlib={'os', 'sys'},
#   third_party={'requests', 'pandas'},
#   first_party={'utils'},
# )

Directory Processing

Extract imports from all files in a directory with optional progress bar:

from eil import Extractor, ExtractorType

extractor = Extractor()

# Extract from all supported files in a directory
result = extractor.extract_from_directory(
    "src/",
    extractor_type=ExtractorType.ALL,  # or PYTHON, R
    recursive=True,                     # search subdirectories
    show_progress=True,                 # show tqdm progress bar
)

# Access results
for file_path, libs in result.extracted.items():
    print(f"{file_path}: {libs.third_party}")

# Check for failures
for file_path, error in result.failed.items():
    print(f"Failed: {file_path}\n{error}")

Ignored External/Vendored Directories

By default, directories commonly used for vendored or copied code (e.g., external, vendor, third_party, deps) are ignored when extracting imports from a repository. This prevents analyzing large bundled dependencies and avoids falsely classifying those packages as first-party.

Ignored directories are not scanned for the purpose of promoting module names to first-party or adding them to a third-party whitelist. If you want the analyzer to consider those directories, override the default ignore list by passing ignore_directories_list=set() to Extractor.extract_from_directory().

Example:

from eil import Extractor, ExtractorType

extractor = Extractor()
result = extractor.extract_from_directory(
    "src/",
    extractor_type=ExtractorType.ALL,
    recursive=True,
    ignore_directories_list=set(),  # disable default ignore list
)

# Proceed with `result.extracted` as usual

Processing Notebook Formats

If you want to extract imports from Jupyter notebooks or Rmd files, you should first convert them to their script counterparts using: py-nb-to-src which will convert .ipynb of all types (R, Julia, Python, Matlab) to their respective script formats as well as .Rmd to .R scripts.

Install with: pip install py-nb-to-src

Then use the following code to convert and extract imports:

from nb_to_src import convert_directory, ConverterType
from eil import Extractor

converted_file_results = convert_directory(
    "notebooks/",
    # recursive=True,
    progress_leave=False,
)
extractor = Extractor()
extracted_results = extractor.extract_from_directory(
    "notebooks/",
    # recursive=True,
    progress_leave=False,
)
for file_path, libs in extracted_results.extracted.items():
    print(f"{file_path}: {libs.third_party}")

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

pyeil-0.3.1.tar.gz (21.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyeil-0.3.1-py3-none-any.whl (18.4 kB view details)

Uploaded Python 3

File details

Details for the file pyeil-0.3.1.tar.gz.

File metadata

  • Download URL: pyeil-0.3.1.tar.gz
  • Upload date:
  • Size: 21.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyeil-0.3.1.tar.gz
Algorithm Hash digest
SHA256 73b7e60a54d69d8a7a6cc22f8d428e86abbae5c6a4a2d35588cc010c8f8c82d4
MD5 cc53876281a430bd3a4d32c40ecc8dfd
BLAKE2b-256 32750b5b04e11b02110ddc575061a0a403a53d7081ef995387aa6652073ea7b0

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyeil-0.3.1.tar.gz:

Publisher: ci.yml on evamaxfield/py-extract-imported-libraries

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pyeil-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: pyeil-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 18.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pyeil-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 df5b11a98f7ab17e991f341dbb3879df2d4d092048cefe7a40e39a062728acdd
MD5 9a2032e1ce65c00cf52277beb339db19
BLAKE2b-256 1e98371e877d67dc9cceb70a30bbae871a07c2803c73467c0e72652284a59d57

See more details on using hashes here.

Provenance

The following attestation bundles were made for pyeil-0.3.1-py3-none-any.whl:

Publisher: ci.yml on evamaxfield/py-extract-imported-libraries

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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