Skip to content
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

Replace pkg_resources with importlib #462

Open
robnagler opened this issue Mar 27, 2024 · 1 comment
Open

Replace pkg_resources with importlib #462

robnagler opened this issue Mar 27, 2024 · 1 comment

Comments

@robnagler
Copy link
Member

No description provided.

@robnagler
Copy link
Member Author

This is really screwed up. They are adding things in 3.9 and deprecating in 3.12. We are going to have to change the model of pkresource to implement some form of caching of data, which aligns with how importlib.resources works. We'll have to provide backwards compatibility, because there's an assumption that the files can be in a wheel (zip) so you can't just symlink to those files.

I think at this time we can add a hook to pkcli to call clean up functions. I don't think pykern functions are invoked directly (e.g. uswgi) so they always start from a pkcli. We could check for this in side pkresource.

pkresource will create a temporary directory (in /tmp is probably best, especially for docker) that caches resources and removes them on program exit (with pkcli hook).

Here's something that's really broken:

$ cd ~/src/radiasoft
$ python
Python 3.9.15 (main, Jan  5 2024, 23:53:42)
[GCC 12.2.1 20221121 (Red Hat 12.2.1-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib.resources as r
>>> for f in r.files("pykern").iterdir():
...     print(f)
...
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/vagrant/.pyenv/versions/3.9.15/lib/python3.9/importlib/resources.py", line 147, in files
    return _common.from_package(_get_package(package))
  File "/home/vagrant/.pyenv/versions/3.9.15/lib/python3.9/importlib/_common.py", line 14, in from_package
    return fallback_resources(package.__spec__)
  File "/home/vagrant/.pyenv/versions/3.9.15/lib/python3.9/importlib/_common.py", line 18, in fallback_resources
    package_directory = pathlib.Path(spec.origin).parent
  File "/home/vagrant/.pyenv/versions/3.9.15/lib/python3.9/pathlib.py", line 1082, in __new__
    self = cls._from_parts(args, init=False)
  File "/home/vagrant/.pyenv/versions/3.9.15/lib/python3.9/pathlib.py", line 707, in _from_parts
    drv, root, parts = self._parse_args(args)
  File "/home/vagrant/.pyenv/versions/3.9.15/lib/python3.9/pathlib.py", line 691, in _parse_args
    a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not NoneType

I don't know how we'll work around this.

robnagler pushed a commit that referenced this issue Mar 27, 2024
- Added PYKERN_PKCLI_TEST_IGNORE_WARNINGS=1 to avoid deprecation warnings on tests.
  These are normally suppressed, but pytest enables all warnings.
robnagler added a commit that referenced this issue Mar 28, 2024
- Added PYKERN_PKCLI_TEST_IGNORE_WARNINGS=1 to avoid deprecation warnings on tests.
  These are normally suppressed, but pytest enables all warnings.
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