googlefonts / fontdiffenator

Font comparison tool
Apache License 2.0
80 stars 13 forks source link

ImportError: cannot import name 'diff_fonts' from 'diffenator' #40

Closed thundernixon closed 5 years ago

thundernixon commented 5 years ago

The readme suggests that fontdifferenator can be used in Python:

from diffenator import diff_fonts
from diffenator.font import InputFont
font_a = InputFont('./path/to/font_a.ttf')
font_b = InputFont('./path/to/font_b.ttf')
diff_fonts(font_a, font_b)

However, when I do try to use this in Python, I get a cannot import name error:

(venv) ️️➡️ python gfonts-qa/diff.py
----------------------------------------------------------
Traceback (most recent call last):
  File "gfonts-qa/diff.py", line 3, in <module>
    from diffenator import diff_fonts
ImportError: cannot import name 'diff_fonts' from 'diffenator' (/Users/stephennixon/type-repos/google-font-repos/plex/venv/lib/python3.7/site-packages/diffenator/__init__.py)

Searching in the bin of fontdiffenator, I see that diffenator/diff.py contains no diff_fonts class, but does contain DiffFonts class. So, I've tried to instead import and use that in my Python script, but with a similar result:

(venv) ➡️ python gfonts-qa/diff.py
----------------------------------------------------------
Traceback (most recent call last):
  File "gfonts-qa/diff.py", line 3, in <module>
    from diffenator import DiffFonts
ImportError: cannot import name 'DiffFonts' from 'diffenator' (/Users/stephennixon/type-repos/google-font-repos/plex/venv/lib/python3.7/site-packages/diffenator/__init__.py)

Am I missing something here, or has the tool changed since that part of the readme was written?

chrissimpkins commented 5 years ago

DiffFonts is located in the diff.py sub-module of diffenator. This import statement should address your issue with the attempt to import the (correct) DiffFonts class:

from diffenator.diff import DiffFonts
f42qb-image
thundernixon commented 5 years ago

Ohhh I wasn't aware I needed the .diff in the import. Thank you! -- ——

Stephen Nixon

www.thundernixon.com

chrissimpkins commented 5 years ago

Marc's implementation is in the fontdiffenator.__main__ file. It looks like his approach with the DiffFonts class combines this dictionary that is parsed from command line args:

https://github.com/googlefonts/fontdiffenator/blob/d10a18a7fd30686021de599b1002b4fb7eda8bdc/Lib/diffenator/__main__.py#L88-L96

with this class instantiation using the before/after paths in the call:

https://github.com/googlefonts/fontdiffenator/blob/master/Lib/diffenator/__main__.py#L112

then you can choose how you want to view the diff:

https://github.com/googlefonts/fontdiffenator/blob/d10a18a7fd30686021de599b1002b4fb7eda8bdc/Lib/diffenator/__main__.py#L114-L120

thundernixon commented 5 years ago

Thanks so much for the helpful information, @chrissimpkins!

For now, I just cued diffenator via gftools qa in a simple shell script.