Skip to content

setuptools removed from python 3.12 #664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
BishopWolf opened this issue Jan 10, 2025 · 2 comments
Closed

setuptools removed from python 3.12 #664

BishopWolf opened this issue Jan 10, 2025 · 2 comments

Comments

@BishopWolf
Copy link

BishopWolf commented Jan 10, 2025

The package setuptools is deprecated and removed from python 3.12. Some distribution may still add it for compatibility, but in general it is gone for good. The user is expected to use importlib instead.

This causes errors in the current CI for MacOS with python 3.12. Mac users with python 3.12 should be reporting this issue too.

File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/opengate/utility.py", line 9, in <module>
    import pkg_resources
ModuleNotFoundError: No module named 'pkg_resources'

Solution, switch to importlib

Switching from pkg_resources to importlib.resources in Python can help streamline your code and improve performance. Here’s how you can make the transition:

Using pkg_resources

import pkg_resources

Example of reading a resource file

resource_content = pkg_resources.resource_string('your_package', 'data/resource.txt')
print(resource_content.decode('utf-8'))

Switching to importlib.resources

For Python 3.7 and later, you can use importlib.resources:

import importlib.resources

Example of reading a resource file

with importlib.resources.open_text('your_package.data', 'resource.txt') as file:
    resource_content = file.read()
    print(resource_content)

Explanation

Importing the Module:

pkg_resources is replaced by importlib.resources.

Accessing the Resource:

pkg_resources.resource_string is replaced by importlib.resources.open_text.

Reading the Resource:

Use a context manager (with statement) to open and read the resource file.
Benefits of importlib.resources
Performance: importlib.resources is generally faster and more efficient.
Simplicity: The API is simpler and more intuitive.
Standard Library: It’s part of the standard library, so no additional dependencies are required.

By making this switch, your code will be more modern and maintainable. If you have any specific questions or need further assistance, feel free to ask!

Generated with GitHub Copilot

@BishopWolf
Copy link
Author

BishopWolf commented Jan 17, 2025

Update: I tried in Linux with a fresh python 3.12 environment, same error

Traceback (most recent call last):
  File "/Data/Programas/opengate/bin/opengate_tests", line 5, in <module>
    from opengate.bin.opengate_tests import go
  File "/Data/Programas/opengate/lib/python3.12/site-packages/opengate/__init__.py", line 162, in <module>
    import opengate.sources
  File "/Data/Programas/opengate/lib/python3.12/site-packages/opengate/sources/__init__.py", line 1, in <module>
    from . import generic, phspsources, voxelsources, gansources
  File "/Data/Programas/opengate/lib/python3.12/site-packages/opengate/sources/generic.py", line 5, in <module>
    from .base import (
  File "/Data/Programas/opengate/lib/python3.12/site-packages/opengate/sources/base.py", line 7, in <module>
    from ..actors.base import _setter_hook_attached_to
  File "/Data/Programas/opengate/lib/python3.12/site-packages/opengate/actors/__init__.py", line 1, in <module>
    from . import (
  File "/Data/Programas/opengate/lib/python3.12/site-packages/opengate/actors/digitizers.py", line 8, in <module>
    from .base import ActorBase
  File "/Data/Programas/opengate/lib/python3.12/site-packages/opengate/actors/base.py", line 7, in <module>
    from ..utility import insert_suffix_before_extension
  File "/Data/Programas/opengate/lib/python3.12/site-packages/opengate/utility.py", line 9, in <module>
    import pkg_resources
ModuleNotFoundError: No module named 'pkg_resources'

BishopWolf added a commit to BishopWolf/GateTools that referenced this issue Jan 22, 2025
@BishopWolf
Copy link
Author

This issue is solved

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

No branches or pull requests

1 participant