jquast / wcwidth

Python library that measures the width of unicode strings rendered to a terminal
Other
392 stars 58 forks source link

The import of pkg_resources creates issues packaging this library #41

Closed sonthonaxrk closed 4 years ago

sonthonaxrk commented 4 years ago

Hey this is a popular and useful project, it's a 2rd degree transitive dependency in a project I'm using, tabulate -> wcwidth.

Unfortunately some of our slightly dimmer witted customers who refuse to use virtual environments, and live within a hosted Jupyter notebook are getting this kind of error.

 File "/.pyenv/versions/3.7.3/envs/sdk/lib/python3.7/site-packages/tabulate.py", line 54, in <module>
    import wcwidth  # optional wide-character (CJK) support
  File "/.pyenv/versions/3.7.3/envs/sdk/lib/python3.7/site-packages/wcwidth/__init__.py", line 12, in <module>
    from .wcwidth import ZERO_WIDTH  # noqa
  File "/.pyenv/versions/3.7.3/envs/sdk/lib/python3.7/site-packages/wcwidth/wcwidth.py", line 72, in <module>
    import pkg_resources
  File "/.pyenv/versions/3.7.3/envs/sdk/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3260, in <module>
    @_call_aside
  File "/.pyenv/versions/3.7.3/envs/sdk/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3244, in _call_aside
    f(*args, **kwargs)
  File "/.pyenv/versions/3.7.3/envs/sdk/lib/python3.7/site-packages/pkg_resources/__init__.py", line 3273, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/Users/rollokonig-brock/.pyenv/versions/3.7.3/envs/sdk/lib/python3.7/site-packages/pkg_resources/__init__.py", line 583, in _build_master
    ws.require(__requires__)
  File "/.pyenv/versions/3.7.3/envs/sdk/lib/python3.7/site-packages/pkg_resources/__init__.py", line 900, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/.pyenv/versions/3.7.3/envs/sdk/lib/python3.7/site-packages/pkg_resources/__init__.py", line 786, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'ipython' distribution was not found and is required by mypackage

Because importing pkg_resources actually requires that the dependency structure of an environment be 100% okay, and this is actually a bit of an unexpected side effect (as we get non-devs messing about with pip).

(I'm going to raise this as a bug with the python core team).

jquast commented 4 years ago

Thanks @sonthonaxrk,

I use pkg_resources to safely load json files, as I understand, the filesystem cannot be guaranteed, using the technique, "find a json file in the same folder as my file", like

os.path.join(os.path.dirname(__file__), 'file.json')

is not portable, due to embedded environments using eggs and such, where the entire package is just one big zip file, you cannot access the filesystem like this.

But anyway, I understand the difficulty, I will try to use "code generation" to generate raw python code, rather than load json files. This is probably related to https://github.com/xonsh/xonsh/issues/3607 -- are your customers using fedora, by chance?

sonthonaxrk commented 4 years ago

Hey @jquast thanks for the fast response. I'm not entirely sure if it's the same issue since I can't find a stack trace linked. To be honest our customers are on everything and I've got to be really defensive about packaging.

Basically pkg_resources has some pretty gnarly side effects when at import time. It'd be really great if it was generated via code.

Out of interest, what is version.json for? Could it just be a plain python file?