pytroll / aggdraw

Python package wrapping AGG2 drawing functionality

Home Page:https://aggdraw.readthedocs.io/en/latest/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Trouble installing on ubuntu 16.04

mfschmidt opened this issue · comments

I used this command, in bash, to install aggdraw for the entire system.

sudo -H pip3 install aggdraw

The complete output follows:

Collecting aggdraw
  Downloading https://files.pythonhosted.org/packages/42/33/2f6ba9ed2199181d1f2a6b346396134f98ff69a308f732acb36605bbc0b0/aggdraw-1.3.6.tar.gz (269kB)
100% |████████████████████████████████| 276kB 7.5MB/s 
Building wheels for collected packages: aggdraw
  Running setup.py bdist_wheel for aggdraw ... error
  Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-fyrbgiq4/aggdraw/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d /tmp/pip-wheel-tnafb16k --python-tag cp35:
  Trying freetype-config to find freetype library...
  Using ctypes to find freetype library...
  === freetype found: '/tmp/pip-install-fyrbgiq4'
  running bdist_wheel
  running build
  running build_ext
  building 'aggdraw' extension
  creating build
  creating build/temp.linux-x86_64-3.5
  creating build/temp.linux-x86_64-3.5/agg2
  creating build/temp.linux-x86_64-3.5/agg2/src
  creating build/temp.linux-x86_64-3.5/agg2/font_freetype
  x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -DVERSION=1.3.6 -DHAVE_FREETYPE2 -Iagg2/include -Iagg2/font_freetype -I/tmp/pip-install-fyrbgiq4/include -I/tmp/pip-install-fyrbgiq4/include/freetype -I/tmp/pip-install-fyrbgiq4/include/freetype2 -I/usr/include/python3.5m -c aggdraw.cxx -o build/temp.linux-x86_64-3.5/aggdraw.o
  cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
  In file included from aggdraw.cxx:84:0:
  agg2/font_freetype/agg_font_freetype.h:23:22: fatal error: ft2build.h: No such file or directory
  compilation terminated.
  error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

  ----------------------------------------
  Failed building wheel for aggdraw
  Running setup.py clean for aggdraw
Failed to build aggdraw
Installing collected packages: aggdraw
  Running setup.py install for aggdraw ... error
    Complete output from command /usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-fyrbgiq4/aggdraw/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-hv7_mve1/install-record.txt --single-version-externally-managed --compile:
    Trying freetype-config to find freetype library...
    Using ctypes to find freetype library...
    === freetype found: '/tmp/pip-install-fyrbgiq4'
    running install
    running build
    running build_ext
    building 'aggdraw' extension
    creating build
    creating build/temp.linux-x86_64-3.5
    creating build/temp.linux-x86_64-3.5/agg2
    creating build/temp.linux-x86_64-3.5/agg2/src
    creating build/temp.linux-x86_64-3.5/agg2/font_freetype
    x86_64-linux-gnu-gcc -pthread -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -g -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -fPIC -DVERSION=1.3.6 -DHAVE_FREETYPE2 -Iagg2/include -Iagg2/font_freetype -I/tmp/pip-install-fyrbgiq4/include -I/tmp/pip-install-fyrbgiq4/include/freetype -I/tmp/pip-install-fyrbgiq4/include/freetype2 -I/usr/include/python3.5m -c aggdraw.cxx -o build/temp.linux-x86_64-3.5/aggdraw.o
    cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
    In file included from aggdraw.cxx:84:0:
    agg2/font_freetype/agg_font_freetype.h:23:22: fatal error: ft2build.h: No such file or directory
    compilation terminated.
    error: command 'x86_64-linux-gnu-gcc' failed with exit status 1

    ----------------------------------------
Command "/usr/bin/python3 -u -c "import setuptools, tokenize;__file__='/tmp/pip-install-fyrbgiq4/aggdraw/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-record-hv7_mve1/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-install-fyrbgiq4/aggdraw/`

I found that I have 3 files named ft2build.h, but all are within other software's directories (docker and fsl) where pip would have no business looking. Do I need to install freetype source files before installing aggdraw? If so, can this be made an explicity dependency in aggdraw so it just works? Thanks for any help in getting this working the "right" way.

freetype is not a python library, it is a C library so it can't be listed as a dependency to "just work". pip has no way of installing C libraries. You could use conda and add the conda-forge channel and install aggdraw with conda. This will install aggdraw without compiling and I have aggdraw build on conda-forge with freetype support included.

If you know that you have freetype installed in the system and the aggdraw installed isn't finding it you can force it by setting the AGGDRAW_FREETYPE_ROOT environment variable and pointing it to the prefix where freetype is installed (the parent directory of the lib directory). However, it seems that aggdraw is finding the freetype library in the temporary directory built by pip which seems very strange to me. Perhaps this is a bug in the setup.py that it is incorrectly finding a freetype library that isn't actually there (see printed messages in the output above).

@saimn had similar issues on ArchLinux and has been able to pip install aggdraw (#31).

Lastly, it is recommended that you don't installed aggdraw or any other third party python library with sudo. Typically people make virtual environments or use conda to not use the system environments. Although I wouldn't expect sudo privileges to mess with the installation, I've seen weirder things by doing sudo pip.

What packages do you install before installing aggdraw? Do you installing anything specific with apt before doing this pip install? Basically, how can I reproduce your environment?

Thanks, djhoese. Digging a little...

The setup.py uses three functions to determine freetype libraries, and my setup seems to have found the file 'libfreetype.so.6' with the ctype function. But it never could ascertain the correct full path for it. It's at /usr/lib/x86_64-linux-gnu/libfreetype.so.6. I'm going to clone the repo, add a path to setup.py, and see if it installs OK, or kicks the can down the road, or maybe I'm just wrong. :)

The part that confuses me about the output you had is this:

  === freetype found: '/tmp/pip-install-fyrbgiq4'

Which is the temporary directory created by pip. This print message means that setup.py found '/tmp/pip-install-fyrbgiq4/lib/libfreetype.so.6'...WTF.

Yeah, I was pasting snippets of code from setup.py into a python console and it basically got a filename without a full path and ended up thinking it was in /home/mike for me. That was my current directory when I invoked the python kernel. I assume /tmp is the working directory for the install and where the path defaults to after falling out of the _get_freetype_with_ctypes() function. Give me a few minutes and I may have something to share.

So the _with_ctypes function used by setup.py has to guess at some paths on linux due to some "issues" with ctypes on linux (find_library doesn't return a full path to a library). It tries the current prefix for the current python interpreter which should be /usr if python3 is in /usr/bin/python3. I could see pip modifying this to /tmp/pip-install-fyrbgiq4 so maybe pip is copying/linking the /usr/ directory to the temporary pip directory.

The _with_ctypes will try sys.prefix, then /usr, then /usr/local. Your freetype is installed in /usr/lib/x86_64-linux-gnu which is not one of the directories it searches. So...it should be installing without trying to use libfreetype.

It's a bug in setup.py! I think I've figured it out. My mistake. Updates coming soon...

Sorry, I wrote that poorly. More clearly and specifically, The function _get_freetype_with_ctypes() does this

from ctypes.util import find_library
ft_lib_path = find_library('freetype')

And find_library returns the string 'libfreetype.so.6'. This is the name of the file, but with no information about where it was found. Later in the function, it tries '/usr' and '/usr/local' and sys.prefix(), which is '/usr' for me. But my system copy of libfreetype.so.6 is in /usr/lib/x86_64-linux-gnu/, which is never checked. So the function knows there's a library, but doesn't know where, and returns '..', which is the working directory.

So, I hard-coded a path into the function to find the .so file, and it did, but then errored the same anyway because it could not find the c header, ft2build.h. I only have the compiled .so library on my system, not the development files.

In response to your previous question, I'm running Ubuntu 16.04 and I don't think I've done anything strange to configure it. And just to rule it out, I did my last test in a virtual environment with python 3.5.2 and pip 18.0. Same results.

@mfschmidt See the associated pull request (#35) where I have fixed this. I will merge this when the tests pass and make a new release. Just a dumb logic bug by me that I didn't think could happen.

I just pasted the code from your pull request into my environment. The setup churned for a little longer than before (still sub-second) and completed successfully. I opened python3 and imported aggdraw with no problems. Thank you for your help!

FYI This installs aggdraw without freetype support (so no fonts). You can test this with:

davidh@ubuntu:~/aggdraw$ python3
Python 3.6.5 (default, Apr  1 2018, 05:46:30) 
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from aggdraw import Font
>>> font = Font('black', '/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: cannot load font (no text renderer)

If you use the environment variable (mentioned above) to point to freetype (if not installed in a normal location) it should have this font functionality.

Yep, no fonts.

And setting AGGDRAW_FREETYPE_ROOT to /usr/lib/x86_64-linux-gnu resulted in the same errors from before, because I don't have development files available, just the compiled .so file.

For future reference for anyone else ending up here, to make everything work, including fonts, I did this:

unset AGGDRAW_FREETYPE_ROOT
pip uninstall aggdraw

That was just to clean up what I had already done, unnecessary for clean installs.

sudo apt install libfreetype6-dev
pip install aggdraw

Installing libfreetype6-dev first put the development files, including ft2build.h, into /usr/include/freetype2/ where aggdraw's setup.py could use freetype-config to find them. And if you install libfreetype6-dev first, the code fix wouldn't matter, because setup would find the correct files early, before ever searching the system libraries for them.

Thanks for the notes on installing the dev files. Still waiting on unit tests before merging the PR.