Skip to content

Conversation

thawancomt
Copy link

🔧 Replaces deprecated pkg_resources.resource_filename with importlib.resources.files() when available (Python ≥ 3.9).
Falls back to pkg_resources for compatibility with older Python versions, avoiding additional dependencies.

to fix the https://setuptools.pypa.io/en/latest/pkg_resources.html warning deprecated resource

issue issue #1909

@hwprinz
Copy link

hwprinz commented Sep 9, 2025

i don't know how to get to your fix....., i can't see anything new to download to fix the package...

so, i've made this....
which is working and tested on Windows in ComfyUI portable's embedded python/Lib/site-packages/ctranslate2
new init.py

import sys

if sys.platform == "win32":
    import ctypes
    import glob
    import os

    # Try to use the new importlib.metadata, fall back to pkg_resources
    try:
        from importlib import metadata
        from importlib import resources
        
        def get_package_dir(module_name):
            try:
                # For Python 3.9+
                return str(resources.files(module_name))
            except AttributeError:
                # For Python 3.7-3.8
                with resources.path(module_name, '') as path:
                    return str(path)
    except ImportError:
        import warnings
        warnings.filterwarnings("ignore", category=UserWarning, message="pkg_resources is deprecated")
        import pkg_resources
        
        def get_package_dir(module_name):
            return pkg_resources.resource_filename(module_name, "")

    module_name = sys.modules[__name__].__name__
    package_dir = get_package_dir(module_name)

    add_dll_directory = getattr(os, "add_dll_directory", None)
    if add_dll_directory is not None:
        add_dll_directory(package_dir)

    for library in glob.glob(os.path.join(package_dir, "*.dll")):
        ctypes.CDLL(library)

try:
    from ctranslate2._ext import (
        AsyncGenerationResult,
        AsyncScoringResult,
        AsyncTranslationResult,
        DataType,
        Device,
        Encoder,
        EncoderForwardOutput,
        ExecutionStats,
        GenerationResult,
        GenerationStepResult,
        Generator,
        MpiInfo,
        ScoringResult,
        StorageView,
        TranslationResult,
        Translator,
        contains_model,
        get_cuda_device_count,
        get_supported_compute_types,
        set_random_seed,
    )
    from ctranslate2.extensions import register_extensions
    from ctranslate2.logging import get_log_level, set_log_level

    register_extensions()
    del register_extensions
except ImportError as e:
    # Allow using the Python package without the compiled extension.
    if "No module named" in str(e):
        pass
    else:
        raise

from ctranslate2 import converters, models, specs
from ctranslate2.version import __version__

Thanks

@Lord-Memester
Copy link

Lord-Memester commented Sep 16, 2025

#1911 (comment) @hwprinz

Will this be merged? Has it already been merged?

@thawancomt
Copy link
Author

#1911 (comment) @hwprinz

Will this be merged? Has it already been merged?

I dont think so, they aren't even reviewing the PRs

@thawancomt
Copy link
Author

i don't know how to get to your fix....., i can't see anything new to download to fix the package...

so, i've made this.... which is working and tested on Windows in ComfyUI portable's embedded python/Lib/site-packages/ctranslate2 new init.py

import sys

if sys.platform == "win32":
    import ctypes
    import glob
    import os

    # Try to use the new importlib.metadata, fall back to pkg_resources
    try:
        from importlib import metadata
        from importlib import resources
        
        def get_package_dir(module_name):
            try:
                # For Python 3.9+
                return str(resources.files(module_name))
            except AttributeError:
                # For Python 3.7-3.8
                with resources.path(module_name, '') as path:
                    return str(path)
    except ImportError:
        import warnings
        warnings.filterwarnings("ignore", category=UserWarning, message="pkg_resources is deprecated")
        import pkg_resources
        
        def get_package_dir(module_name):
            return pkg_resources.resource_filename(module_name, "")

    module_name = sys.modules[__name__].__name__
    package_dir = get_package_dir(module_name)

    add_dll_directory = getattr(os, "add_dll_directory", None)
    if add_dll_directory is not None:
        add_dll_directory(package_dir)

    for library in glob.glob(os.path.join(package_dir, "*.dll")):
        ctypes.CDLL(library)

try:
    from ctranslate2._ext import (
        AsyncGenerationResult,
        AsyncScoringResult,
        AsyncTranslationResult,
        DataType,
        Device,
        Encoder,
        EncoderForwardOutput,
        ExecutionStats,
        GenerationResult,
        GenerationStepResult,
        Generator,
        MpiInfo,
        ScoringResult,
        StorageView,
        TranslationResult,
        Translator,
        contains_model,
        get_cuda_device_count,
        get_supported_compute_types,
        set_random_seed,
    )
    from ctranslate2.extensions import register_extensions
    from ctranslate2.logging import get_log_level, set_log_level

    register_extensions()
    del register_extensions
except ImportError as e:
    # Allow using the Python package without the compiled extension.
    if "No module named" in str(e):
        pass
    else:
        raise

from ctranslate2 import converters, models, specs
from ctranslate2.version import __version__

Thanks

Edit manually the changed file on my PR. none more, the deprecated msg will disappear

@hwprinz
Copy link

hwprinz commented Sep 17, 2025

Yeah there is sadly no big involvement by the programmers

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.

3 participants