nlpub / chinese-whispers

An implementation of Chinese Whispers in Python.
https://chinese-whispers.readthedocs.io
MIT License
58 stars 13 forks source link

Avoid importing chinese_whispers to source version/license #5

Closed achambers8472 closed 4 years ago

achambers8472 commented 4 years ago

In order to single-source the version and license information, setup.py imported the chinese_whispers package. However, if installed as a library, the networkx package is not yet available, and this causes pip install to fail. This can be seen by trying to install chinese_whispers in a fresh python environment:

$ pip install chinese_whispers
Collecting chinese_whispers
  Using cached chinese-whispers-0.7.0.tar.gz (4.6 kB)
    ERROR: Command errored out with exit status 1:
     command: <PATH>/python -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-n50asvj5/chinese-whispers/setup.py'"'"'; __file__='"'"'/tmp/pip-install-n50asvj5/chinese-whispers/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' egg_info --egg-base /tmp/pip-install-n50asvj5/chinese-whispers/pip-egg-info
         cwd: /tmp/pip-install-n50asvj5/chinese-whispers/
    Complete output (9 lines):
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-install-n50asvj5/chinese-whispers/setup.py", line 3, in <module>
        from chinese_whispers import __version__, __license__
      File "/tmp/pip-install-n50asvj5/chinese-whispers/chinese_whispers/__init__.py", line 6, in <module>
        from .chinese_whispers import WEIGHTING, top_weighting, lin_weighting, log_weighting
      File "/tmp/pip-install-n50asvj5/chinese-whispers/chinese_whispers/chinese_whispers.py", line 10, in <module>
        from networkx.classes import Graph
    ModuleNotFoundError: No module named 'networkx'
    ----------------------------------------
ERROR: Command errored out with exit status 1: python setup.py egg_info Check the logs for full command output.

By directly reading the chinese_whispers/__init__.py file, one can source the package and license information without first needing to import the package.

This is the style of single-sourcing recommended by PyPA (see https://packaging.python.org/guides/single-sourcing-package-version/).

dustalov commented 4 years ago

Thank you very much! We will stick to the proposed approach until we switch to importlib.metadata in the future.