Skip to content

get_distribution() not working in __init__.py #500

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
blazerguns opened this issue Nov 30, 2018 · 4 comments
Closed

get_distribution() not working in __init__.py #500

blazerguns opened this issue Nov 30, 2018 · 4 comments

Comments

@blazerguns
Copy link

I use pyinstaller/cx_freeze for my app. Recently I started using jsonschema and I ran into errors when using the above programs. On Ubuntu env when I ran the binary build by pyinstaller and cx_freeze I got the following issues. Iam using python 3.6.

Traceback (most recent call last):
  File "sslscanner.py", line 13, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "/home/vc/Documents/flash3.6/lib/python3.6/site-packages/PyInstaller/loader/pyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packages/jsonschema/__init__.py", line 33, in <module>
  File "site-packages/pkg_resources/__init__.py", line 476, in get_distribution
  File "site-packages/pkg_resources/__init__.py", line 352, in get_provider
  File "site-packages/pkg_resources/__init__.py", line 895, in require
  File "site-packages/pkg_resources/__init__.py", line 781, in resolve
pkg_resources.DistributionNotFound: The 'jsonschema' distribution was not found and is required by the application

It seems to work when I run it directly using #python sslscanner.py.
On further investigation, I ran into this code in site-packages/jsonschema/init.py +33

from pkg_resources import get_distribution
#__version__ = get_distribution(__name__).version
__version__ = '3.0.0a3'

I commented out the get_distribution code and copied the version directly. This solves the problem and the binaries work. Is this a bug on your end? I did not investigate this further.

@Julian
Copy link
Member

Julian commented Nov 30, 2018

Hi -- it's way more likely it's a bug in cx_freeze/pyinstaller/the way you're using them unfortunately.

Those tools aren't "supported" in the sense that jsonschema has no CI testing that using it under them functions, so you're kind of on your own there unless you can submit something bringing them under CI (along with ideally a fix for whatever is happening).

What jsonschema is doing is perfectly reasonable on that line there, and what you're doing to hard code the version is essentially stripping out the useful thing :), which is that jsonschema's version is automatically managed (by a project called setuptools_scm at this point). The get_distribution line is the correct way then to expose that version, but presumably cx_Freeze is blowing up by trying to do something like importing jsonschema before actually installing it.

That's not safe to do for any package unfortunately, so it's not really doing the right thing there, but yeah I don't know too much more about it.

So, TL;DR if you have both a test and a patch I'm happy to review them :), but otherwise yeah, it's those tools that are doing something funny, which I'm not quite sure how to help you get past but they likely might be able to!

@mikedarcy
Copy link

mikedarcy commented May 17, 2019

@Julian
I hate to zombie this, but it remains an issue (in particular for cx_freeze) and there is a simple thing you can do to mitigate the issue for these external freezing environments which don't always include setuputils/distutils installed package metadata in their output.

Simply wrap the get_distribution call in a try/except block and catch DistributionNotFound and then just set the __version__ to "unknown" or "0.0.0dev0' or whatever you like.

I've seen this handled similarly in other projects and it seems like a good practice in general rather than facilitating a initialization-time crash of the interpreter.

For example:

try:
    __version__= get_distribution(__name__).version
except DistributionNotFound:
    __version__= "unknown version"

From what I can see, you only ever reference __version__ in your sphinx doc build, so having this as a fallback doesn't look (to me) like it would really hurt anything functionally.

I hope you'll consider adding this relatively harmless mitigation. Cheers.

@Julian
Copy link
Member

Julian commented May 17, 2019

@mikedarcy I'm happy to take PRs for that, though my position still is it needs to come with adding CI for ensuring that cx_freeze continues to function -- I can't promise something is supported unless it's under test.

(And FWIW, __version__ is also public for end user use.)

@mikedarcy
Copy link

mikedarcy commented May 17, 2019

@Julian I'm not saying that you actually need to support it (be it cx_freeze or whatever environment that might get into trouble on this method call), I am just saying allow for a graceful degradation that avoids an init-time crash of python for whatever reason. Maybe even make the fallback string "Unsupported Version" 😉 .

Anyway, just trying to offer up something simple that I perceive as having little downside. In the short term I can work around this by pinning to 2.6.0.

Jkovarik added a commit to nasa/cumulus-message-adapter that referenced this issue Feb 6, 2020
Due to python-jsonschema/jsonschema#500 and related
concerns pyinstaller doesn't play nice with later versions of
jsonschema.

We may need to find another way to build this post py3.8
Julian added a commit that referenced this issue Jul 19, 2021
fd0aa9f8 Merge pull request #500 from anexia-it/master
d0107804 Extend if/then/else tests on unevaluated properties
017d4e56 Merge pull request #499 from json-schema-org/ether/dynamic-scope-enter-leave
aa621ed4 test that dynamic scopes are not always in scope

git-subtree-dir: json
git-subtree-split: fd0aa9f8e2497d9048e17f071abb8fa409f5cb52
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

3 participants