gpt4 book ai didi

python - 使用 sudo 运行 Django 找不到已安装的 Homebrew 程序包

转载 作者:行者123 更新时间:2023-11-28 18:17:28 27 4
gpt4 key购买 nike

我有一个 Django 项目,我想使用 manage.py runserver 在端口 80 上运行。在我的项目中,我使用 Wand库,我已经使用 Homebrew 软件安装了 freetype 和 imagemagick:

brew install freetype imagemagick

要在端口 80 上运行 Django runserver 需要使用 sudo 运行,但是当我使用 sudo 运行 runserver 时它不会似乎找到 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

brew 中的 Imagemagick 文件:

$ 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

所有 libMagickWand dylib 文件

$ 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

在没有 sudo 的情况下在另一个端口上运行它会按预期工作,我怎样才能让它在端口 80 上也能工作?

最佳答案

清理不同的 imagemagick 安装

  1. 首先,last version of imagemagick is currently not supported by wand , 所以如果你不使用它可以安全地卸载它以避免干扰启动 wand 所必需的 imagemagick@6:

    $ brew uninstall imagemagick

    不过这是可选的。

    如果你想保留安装的两个版本,我仍然会取消链接 imagemagick 以支持旧的 dylib,因为 wand 在我的机器上工作而无需设置 MAGICK_PATH :

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

    $ brew link imagemagick@6
    Linking /usr/local/Cellar/imagemagick@6/6.9.9-23... 76 symlinks created
  3. 检查这是否已经足够(正如我所说,此设置已经在我的机器上运行):

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

    如果没有错误出现,你就完成了!您还可以清除 MAGICK_PATH 变量,因为它现在已过时。

如果以上几点没有帮助

您应该保留 MAGICK_PATH 原样,因为它是 wand 查找库的关键。要将 var 传播到 sudo session 中,请修改 sudo 设置:

$ sudo visudo

添加行 Defaults env_keep += "MAGICK_PATH",保存并退出。您可能需要启动一个新的终端 session 才能使更改生效。这将确保将 MAGICK_PATH 变量传递给 sudo session 。

关于python - 使用 sudo 运行 Django 找不到已安装的 Homebrew 程序包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47429321/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com