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

pkg_resources deprecated? #68

Open
PRCseismo opened this issue Dec 31, 2020 · 1 comment
Open

pkg_resources deprecated? #68

PRCseismo opened this issue Dec 31, 2020 · 1 comment

Comments

@PRCseismo
Copy link

I am installing a package that uses pyasdf. I am a bit limited in the python version I can use. Only version 3.7.4 is available. I was just following instructions I was given which, among many other things, said I should run:
python3 -c "import pyasdf; pyasdf.print_sys_info()"

It failed in watermark.py, telling me I needed obspy even though i had installed obspy using pip (unfortunately it is difficult for me to use Anaconda on this machine). As you may know, watermark uses pkg_resources to get the version numbers of required modules. I did a little digging, and although I can't seem to find it again, somewhere on python.org there were instructions for transitioning from pkg_resources, which is being deprecated, to importlib. Unfortunately for me I couldn't find anything in importlib for 3.7.4 that would give me module version numbers (it is in importlib for 3.9).

Aside from pkg_resources being deprecated, it and/or its combination with pip just weren't working for me in python 3.7.4. Perhaps I should have used something other than pip to install.

So in the end I replaced the following in watermark.py:
watermark["module_versions"] = {
module: get_distribution(module).version for module in modules
}

with:

from pip._internal.operations.freeze import freeze
watermark["module_versions"] = {}
for requirement in freeze(local_only=False):
pkg = requirement.split('==')
if pkg[0] in modules:
watermark["module_versions"][pkg[0]] = pkg[1]

I have no idea if this is a good way to do it, but it worked for me so maybe it might help someone else using python 3.7.4. Also, I thought I'd point out the need (apparently) to eventually switch from pkg_resources to importlib.

@krischer
Copy link
Member

Hi @PRCseismo,

thanks a lot of the detailed report and the elaboration of what is going on.

You are right - we'll definitely have to switch to importlib at some time in the future. I'll let this rest for now for two reasons:

  1. Not a critical part of pyasdf - the print_sys_info() function is really only useful for debugging purposes.
  2. I did not investigate but according to your findings there does not seem to be a proper way to do this yet across Python versions without relying on some internal pip function which I don't think is the best idea.

I also just tried this with a clean Python 3.9 environment and pkg_resources still seems to work fine.

I'll leave this issue open because this will have to be tackled at one point.

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

2 participants