Search code examples
pythondjangohomebrewsudo

Running Django with sudo can't find installed homebrew packages


I have a Django project that I want to run at port 80 with manage.py runserver. In my project I use the Wand library and I have installed freetype and imagemagick using homebrew:

brew install freetype imagemagick

To run Django at port 80 runserver is required to be run with sudo, but when I run runserver with sudo it doesn't seem to find imagemagick:

$ sudo ~/.pyenv/versions/myproject/bin/python manage.py runserver 0.0.0.0:80
Password:
Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x11068c840>
Traceback (most recent call last):
  File "/Users/Oskar/.pyenv/versions/myproject/lib/python3.6/site-packages/wand/api.py", line 180, in <module>
    libraries = load_library()
  File "/Users/Oskar/.pyenv/versions/myproject/lib/python3.6/site-packages/wand/api.py", line 135, in load_library
    raise IOError('cannot find library; tried paths: ' + repr(tried_paths))
OSError: cannot find library; tried paths: []

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/Oskar/.pyenv/versions/myproject/lib/python3.6/site-packages/django/utils/autoreload.py", line 228, in wrapper
    fn(*args, **kwargs)
  File "/Users/Oskar/.pyenv/versions/myproject/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
    self.check(display_num_errors=True)
  File "/Users/Oskar/.pyenv/versions/myproject/lib/python3.6/site-packages/django/core/management/base.py", line 359, in check
    include_deployment_checks=include_deployment_checks,
  File "/Users/Oskar/.pyenv/versions/myproject/lib/python3.6/site-packages/django/core/management/base.py", line 346, in _run_checks
    return checks.run_checks(**kwargs)
  File "/Users/Oskar/.pyenv/versions/myproject/lib/python3.6/site-packages/django/core/checks/registry.py", line 81, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Users/Oskar/.pyenv/versions/myproject/lib/python3.6/site-packages/django/core/checks/urls.py", line 16, in check_url_config
    return check_resolver(resolver)
  File "/Users/Oskar/.pyenv/versions/myproject/lib/python3.6/site-packages/django/core/checks/urls.py", line 26, in check_resolver
    return check_method()
  File "/Users/Oskar/.pyenv/versions/myproject/lib/python3.6/site-packages/django/urls/resolvers.py", line 254, in check
    for pattern in self.url_patterns:
  File "/Users/Oskar/.pyenv/versions/myproject/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/Oskar/.pyenv/versions/myproject/lib/python3.6/site-packages/django/urls/resolvers.py", line 405, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/Oskar/.pyenv/versions/myproject/lib/python3.6/site-packages/django/utils/functional.py", line 35, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/Oskar/.pyenv/versions/myproject/lib/python3.6/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Users/Oskar/.pyenv/versions/3.6.0/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/Users/Oskar/git/myproject/myproject/urls.py", line 12, in <module>
    from views import about, index, upload
  File "/Users/Oskar/git/myproject/views.py", line 7, in <module>
    from wand.image import Image
  File "/Users/Oskar/.pyenv/versions/myproject/lib/python3.6/site-packages/wand/image.py", line 20, in <module>
    from .api import MagickPixelPacket, libc, libmagick, library
  File "/Users/Oskar/.pyenv/versions/myproject/lib/python3.6/site-packages/wand/api.py", line 206, in <module>
    'Try to install:\n  ' + msg)
ImportError: MagickWand shared library not found.
You probably had not installed ImageMagick library.
Try to install:
  brew install freetype imagemagick

Imagemagick files in brew:

$ ls -l $(brew --prefix imagemagick)/lib
total 7904
drwxr-xr-x  4 Oskar admin     128 Nov 11 14:25 ImageMagick
-r--r--r--  1 Oskar admin  553388 Nov 15 18:20 libMagick++-7.Q16HDRI.3.dylib
-r--r--r--  1 Oskar admin  913560 Nov 11 14:25 libMagick++-7.Q16HDRI.a
lrwxr-xr-x  1 Oskar admin      29 Nov 11 14:25 libMagick++-7.Q16HDRI.dylib -> libMagick++-7.Q16HDRI.3.dylib
-rwxr-xr-x  1 Oskar admin    1272 Nov 15 18:20 libMagick++-7.Q16HDRI.la
-r--r--r--  1 Oskar admin 1608056 Nov 15 18:20 libMagickCore-7.Q16HDRI.4.dylib
-r--r--r--  1 Oskar admin 2321824 Nov 11 14:25 libMagickCore-7.Q16HDRI.a
lrwxr-xr-x  1 Oskar admin      31 Nov 11 14:25 libMagickCore-7.Q16HDRI.dylib -> libMagickCore-7.Q16HDRI.4.dylib
-rwxr-xr-x  1 Oskar admin    1144 Nov 15 18:20 libMagickCore-7.Q16HDRI.la
-r--r--r--  1 Oskar admin 1109812 Nov 15 18:20 libMagickWand-7.Q16HDRI.4.dylib
-r--r--r--  1 Oskar admin 1564192 Nov 11 14:25 libMagickWand-7.Q16HDRI.a
lrwxr-xr-x  1 Oskar admin      31 Nov 11 14:25 libMagickWand-7.Q16HDRI.dylib -> libMagickWand-7.Q16HDRI.4.dylib
-rwxr-xr-x  1 Oskar admin    1214 Nov 15 18:20 libMagickWand-7.Q16HDRI.la
drwxr-xr-x 10 Oskar admin     320 Nov 15 18:20 pkgconfig

All libMagickWand dylib files

$ sudo find / -name "libMagickWand*.dylib"
./usr/local/lib/libMagickWand-7.Q16HDRI.4.dylib
./usr/local/lib/libMagickWand-7.Q16HDRI.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-9/lib/libMagickWand-6.Q16.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-9/lib/libMagickWand-6.Q16.5.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-11/lib/libMagickWand-6.Q16.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-11/lib/libMagickWand-6.Q16.5.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-20/lib/libMagickWand-6.Q16.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-20/lib/libMagickWand-6.Q16.5.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-21/lib/libMagickWand-6.Q16.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-21/lib/libMagickWand-6.Q16.5.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-19/lib/libMagickWand-6.Q16.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-19/lib/libMagickWand-6.Q16.5.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-10/lib/libMagickWand-6.Q16.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-10/lib/libMagickWand-6.Q16.5.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-17/lib/libMagickWand-6.Q16.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-17/lib/libMagickWand-6.Q16.5.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-12/lib/libMagickWand-6.Q16.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-12/lib/libMagickWand-6.Q16.5.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-15/lib/libMagickWand-6.Q16.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-15/lib/libMagickWand-6.Q16.5.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-23/lib/libMagickWand-6.Q16.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-23/lib/libMagickWand-6.Q16.5.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-14/lib/libMagickWand-6.Q16.dylib
./usr/local/Cellar/imagemagick@6/6.9.9-14/lib/libMagickWand-6.Q16.5.dylib
./usr/local/Cellar/imagemagick/7.0.7-11/lib/libMagickWand-7.Q16HDRI.4.dylib
./usr/local/Cellar/imagemagick/7.0.7-11/lib/libMagickWand-7.Q16HDRI.dylib

Running this without sudo on another port works as expected, how can I also make it work on port 80?


Solution

  • Cleaning up different imagemagick installs

    1. First of all, the last version of imagemagick is currently not supported by wand, so you can safely uninstall it if you don't use it to avoid interference with imagemagick@6 that is necessary to fire up wand:

      $ brew uninstall imagemagick
      

      This is optional, though.

      If you want to leave both versions installed, I would still unlink imagemagick in favor of the old dylibs as wand works on my machine without setting the MAGICK_PATH:

      $ brew unlink imagemagick
      Unlinking /usr/local/Cellar/imagemagick/7.0.7-11... 72 symlinks removed
      
    2. Link the imagemagick@6:

      $ brew link imagemagick@6
      Linking /usr/local/Cellar/imagemagick@6/6.9.9-23... 76 symlinks created
      
    3. Check if this will already suffice (as I said, this setup works on my machine already):

      $ sudo python3.6 -c "from wand.image import Image"
      

      If no error is raised, you are done! You can also clear the MAGICK_PATH variable as it is obsolete now.

    If the points above did not help

    You should leave MAGICK_PATH as it is because it's the key for wand to find the libraries. To propagate the var into sudo session, modify sudo settings:

    $ sudo visudo
    

    Add line Defaults env_keep += "MAGICK_PATH", save and exit. You may need to start a new terminal session for changes to take effect. This will ensure that the MAGICK_PATH variable is passed to the sudo session.