#import #pyimportparse

pyimportparse

A rust crate to parse python imports

2 releases

Uses new Rust 2024

new 0.1.1 Apr 17, 2025
0.1.0 Apr 17, 2025

#1 in #imports

22 downloads per month

MIT license

22MB
390K SLoC

PO File 238K SLoC // 0.1% comments Python 109K SLoC // 0.2% comments Modelica 22K SLoC // 0.0% comments JavaScript 20K SLoC // 0.2% comments Rust 426 SLoC // 0.0% comments Pest 62 SLoC

pyimportparse

A rust crate to parse python imports (ignoring the rest of the code).

Motivation:

Use with care. I've run the parser over the Django codebase and the results suggests that all imports were successfully parsed. However, there could still be cases where the parser does not behave correctly.

use pyimportparse::{parse_imports, Import};

let code = r#"
import a
from b import c
from .d import (e, f)
from ..g import *

if TYPE_CHECKING:
    import h

def foo():
    import i
"#;
    
let imports = parse_imports(&code).unwrap();
    
assert_eq!(vec![
    // (imported_object, line_number, typechecking_only)
    Import::new("a", 2, false),
    Import::new("b.c", 3, false),
    Import::new(".d.e", 4, false),
    Import::new(".d.f", 4, false),
    Import::new("..g.*", 5, false),
    Import::new("h", 8, true),
    Import::new("i", 11, false),
], imports);




Dependencies