Skip to content

calling python_to_json with WindowsPath causes module import failure due to incorrect implicít path to string conversion #5

Description

@SabrCat
from grafana_dashboard.grafana_dashboard import json_to_python, python_to_json
from pathlib import Path

python_to_json(python_base_dir=Path("."), python_base_package="input", json_dir=Path("output"))

This script does not work on windows as I am passing a WindowsPath object into python_base_dir, which propagates to

mod = importlib.import_module(python_package_name)

where the path is implicitly converted into a string as it passes into importlib. Therefore inputlib ends up with name='input\\testout', when it should have been called with name=input.testout.

Changing the script to the following circumvents the issue (patch courtesy of ChatGPT 4o)

import importlib
_real_import_module = importlib.import_module

def patched_import_module(name, package=None):
    # Fix backslashes to dots
    fixed = name.replace("\\", ".")
    return _real_import_module(fixed, package)

importlib.import_module = patched_import_module

from grafana_dashboard.grafana_dashboard import json_to_python, python_to_json
from pathlib import Path

import input.testout


python_to_json(python_base_dir=Path("."), python_base_package="input", json_dir=Path("output"))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions