Skip to content

Conversation

@dbrattli
Copy link
Collaborator

@dbrattli dbrattli commented Dec 6, 2025

This PR adds support for Python-style relative import syntax in importMember, import, and importAll functions, making Python interop more idiomatic for Python developers. Wanted to this fix for a long time.

Changes

New Pythonic syntax for relative imports:

  • .module → resolves to module in current directory (like from .module import ...)
  • ..module → resolves to module in parent directory (like from ..module import ...)
  • ...module → resolves to module in grandparent directory, and so on

Examples:

// Current directory
let add5 (x: int): int = importMember ".native_code"
// Generates: from tests.Python.native_code import add5

// Parent directory  
let foo: int -> int = importMember "..utils"
// Generates: from tests.utils import foo

// Subpackage
let bar: string = importMember ".subpkg.helpers"
// Generates: from tests.Python.subpkg.helpers import bar

// External packages (unchanged - pass through as-is)
let Field: obj = importMember "pydantic"
// Generates: from pydantic import Field

Backwards compatibility:

  • File path syntax (./native_code.py) still works
  • Absolute package imports ("pydantic", "collections.abc") work unchanged

Files Changed

  • src/Fable.Cli/Pipeline.fs - Convert Python-style paths to file paths for resolution
  • src/Fable.Transforms/Python/Fable2Python.Transforms.fs - Handle identifier extraction for relative imports
  • src/Fable.Core/Fable.Core.PyInterop.fs - Updated documentation
  • tests/Python/TestPyInterop.fs - Updated tests to use new syntax

Motivation

The previous JS-style syntax ("./native_code.py") was not idiomatic for Python. Python developers expect module imports to use dot notation without file extensions. This change aligns Fable's Python import syntax with Python's native import conventions.

@dbrattli dbrattli merged commit a0f2106 into main Dec 6, 2025
22 checks passed
@dbrattli dbrattli deleted the python-import-member branch December 6, 2025 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants