代码之家  ›  专栏  ›  技术社区  ›  Pablo Fernandez

我该怎么做才能在苍鹭身上使用巨蟒魔法?

  •  0
  • Pablo Fernandez  · 技术社区  · 6 年前

    我正在构建一个正在使用的应用程序 python-magic 0.4.15 . 在我的本地机器上, python-magic-bin 0.4.15 就像一个魅力(窗户),但我正在努力让它在Heroku上工作。首先,我只是想让苍鹭词的测试通过。我把包裹添加到我的 requirements.txt :

    python-magic==0.4.15
    

    把它推到希罗库。安装正确:

    -----> Installing requirements with pip
           Collecting python-magic==0.4.15 (from -r /app/requirements.txt (line 7))
             Downloading https://files.pythonhosted.org/packages/42/a1/76d30c79992e3750dac6790ce16f056f870d368ba142f83f75f694d93001/python_magic-0.4.15-py2.py3-none-any.whl
           Installing collected packages: python-magic
           Successfully installed python-magic-0.4.15
    

    但是它却像这样崩溃了:

    + python manage.py collectstatic --no-input
    Traceback (most recent call last):
      File "manage.py", line 20, in <module>
        execute_from_command_line(sys.argv)
      File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
        utility.execute()
      File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
        django.setup()
      File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 120, in populate
        app_config.ready()
      File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/apps.py", line 24, in ready
        self.module.autodiscover()
      File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
        autodiscover_modules('admin', register_to=site)
      File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
        import_module('%s.%s' % (app_config.name, module_to_search))
      File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 994, in _gcd_import
      File "<frozen importlib._bootstrap>", line 971, in _find_and_load
      File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 678, in exec_module
      File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
      File "/app/core/admin.py", line 8, in <module>
        from .forms import CrmDataUploadForm
      File "/app/core/forms.py", line 3, in <module>
        from magic import magic
    ImportError: cannot import name 'magic'
    

    我试着加 the NetsoftHoldings/heroku-buildpack-magic buildpack 我可以看到它正在被下载:

    -----> Fetching https://github.com/NetsoftHoldings/heroku-buildpack-magic.git buildpack...
           buildpack downloaded
    

    正在正确安装PIP包:

    ----->用PIP安装要求
    收集python magic==0.4.15(来自-r/app/requirements.txt(第7行))。
    下载https://files.pythonhosted.org/packages/42/a1/76d30c79992e3750dac6790ce16f056f870d368ba142f83f75f694d93001/python-magic-0.4.15-py2.py3-none-any.whl
    安装收集的包:python magic
    已成功安装python-magic-0.4.15
    

    但是,buildback似乎有一个错误(我猜是这样的):

    -----> libmagic app detected
    bash: /tmp/buildpacks/27353aa3b5edb2f8361186206e863f5d481f02805d02f0efdec439f3d30349a4fbdad2951af33a0492023fb0d90d26dafda6fe76f1b2d34f68564a98545f7022/export: No such file or directory
    RootDir: /tmp/buildpacks/ba9220b10d71b7b776013869da2c820ccb5c0f1733268ae2515b3b8ad5edd44d2016d1119bce32d1660fbd84da179891a404dcc57df043b9d82e22725739b072
    BuildDir: /app
    HOME: /app
    Build list
    config
    Home list
    config
    

    它最终会像这样崩溃:

    +python manage.py collectstatic--无输入
    回溯(最近一次呼叫的最后一次):
    文件“manage.py”,第20行,在<模块中>
    从命令行(sys.argv)执行
    文件“/app/.heroku/python/lib/python3.6/site packages/django/core/management/uu init_uuuuy”,第381行,在执行行中,来自命令行
    实用程序.execute()
    文件“/app/.heroku/python/lib/python3.6/site packages/django/core/management/uuu init_uuuy”,第357行,执行中
    Django.StUpUP()
    文件“/app/.heroku/python/lib/python3.6/site packages/django/uuu init_uuuy”,第24行,设置中
    apps.populate(设置。已安装的应用程序)
    文件“/app/.heroku/python/lib/python3.6/site packages/django/apps/registry.py”,第120行,填充
    应用程序配置就绪()
    文件“/app/.heroku/python/lib/python3.6/site packages/django/contrib/admin/apps.py”,第24行,就绪
    self.module.autodiscover()。
    文件“/app/.heroku/python/lib/python3.6/site packages/django/contrib/admin/uuu init_uuu.py”,第26行,在自动发现中
    autodiscover_modules('admin',register_to=site)
    文件“/app/.heroku/python/lib/python3.6/site packages/django/utils/module-loading.py”,第47行,在自动发现模块中
    导入_模块(“%s.%s%”(app_config.name,module_to_search))。
    文件“/app/.heroku/python/lib/python3.6/import lib/uuu init_uuuuy”,第126行,在import_模块中
    返回_bootstrap._gcd_import(name[level:],package,level)
    文件“<frozen importlib._bootstrap>”,第994行,在_gcd_import中
    文件“<frozed importlib.\u bootstrap>”,第971行,位于“查找”和“加载”中
    文件“<frozed importlib.\u bootstrap>”,第955行,在“查找”和“加载”中解锁
    文件“<frozed importlib._bootstrap>”,第665行,in _load_unlocked
    文件“<frozed importlib._bootstrap_external>”,第678行,在exec_模块中
    文件“<frozed importlib.\u bootstrap>”,第219行,在“调用”中删除“帧”后
    文件“/app/core/admin.py”,第8行,在<模块中>
    从.forms导入crmdataUploadForm
    文件“/app/core/forms.py”,第3行,在<模块中>
    从魔术进口魔术
    导入错误:无法导入名称“magic”
    

    然后我尝试添加 heroku-community/apt 构建包和包含以下内容的APT文件:

    libmagic-dev
    libmagic1
    

    我可以看到它们安装正确:

    -----> Fetching .debs for libmagic-dev
           Reading package lists...
           Building dependency tree...
           0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 23 not upgraded.
           Need to get 79.4 kB of archives.
           After this operation, 0 B of additional disk space will be used.
           Get:1 http://archive.ubuntu.com/ubuntu bionic-security/main amd64 libmagic-dev amd64 1:5.32-2ubuntu0.1 [79.4 kB]
           Fetched 79.4 kB in 0s (222 kB/s)
           Download complete and in download only mode
    W: --force-yes is deprecated, use one of the options starting with --allow instead.
    -----> Fetching .debs for libmagic1
           Reading package lists...
           Building dependency tree...
           0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 23 not upgraded.
           Need to get 68.4 kB of archives.
           After this operation, 0 B of additional disk space will be used.
           Get:1 http://archive.ubuntu.com/ubuntu bionic-security/main amd64 libmagic1 amd64 1:5.32-2ubuntu0.1 [68.4 kB]
           Fetched 68.4 kB in 0s (199 kB/s)
           Download complete and in download only mode
    W: --force-yes is deprecated, use one of the options starting with --allow instead.
    -----> Installing libmagic1_1%3a5.32-2ubuntu0.1_amd64.deb
    -----> Installing libmagic-dev_1%3a5.32-2ubuntu0.1_amd64.deb
    

    但应用程序仍然失败,错误相同:

    + pip install -r requirements-test.txt
    Requirement already satisfied: coverage==4.5.1 in ./.heroku/python/lib/python3.6/site-packages (from -r requirements-test.txt (line 1))
    Requirement already satisfied: selenium==3.141.0 in ./.heroku/python/lib/python3.6/site-packages (from -r requirements-test.txt (line 2))
    Requirement already satisfied: urllib3 in ./.heroku/python/lib/python3.6/site-packages (from selenium==3.141.0->-r requirements-test.txt (line 2))
    You are using pip version 9.0.2, however version 18.1 is available.
    You should consider upgrading via the 'pip install --upgrade pip' command.
    + python manage.py collectstatic --no-input
    Traceback (most recent call last):
      File "manage.py", line 20, in <module>
        execute_from_command_line(sys.argv)
      File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
        utility.execute()
      File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 357, in execute
        django.setup()
      File "/app/.heroku/python/lib/python3.6/site-packages/django/__init__.py", line 24, in setup
        apps.populate(settings.INSTALLED_APPS)
      File "/app/.heroku/python/lib/python3.6/site-packages/django/apps/registry.py", line 120, in populate
        app_config.ready()
      File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/apps.py", line 24, in ready
        self.module.autodiscover()
      File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
        autodiscover_modules('admin', register_to=site)
      File "/app/.heroku/python/lib/python3.6/site-packages/django/utils/module_loading.py", line 47, in autodiscover_modules
        import_module('%s.%s' % (app_config.name, module_to_search))
      File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
        return _bootstrap._gcd_import(name[level:], package, level)
      File "<frozen importlib._bootstrap>", line 994, in _gcd_import
      File "<frozen importlib._bootstrap>", line 971, in _find_and_load
      File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
      File "<frozen importlib._bootstrap_external>", line 678, in exec_module
      File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
      File "/app/core/admin.py", line 8, in <module>
        from .forms import CrmDataUploadForm
      File "/app/core/forms.py", line 3, in <module>
        from magic import magic
    ImportError: cannot import name 'magic'
    

    有什么关于如何让巨蟒魔法在Heroku上工作的想法吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Pablo Fernandez    6 年前

    导入的正确方法 magic 模块是:

    import magic
    

    from magic import magic
    

    python magic只提供了一个 魔术 模块和python magic bin的作用相同。问题是python magic bin有一个名为 magic that imports the module magic.magic 这有点像内部细节,因此后一个示例在python magic bin中工作,但不在python magic中工作。

    从python magic文档中:

    >>> import magic
    >>> magic.from_file("testdata/test.pdf")
    'PDF document, version 1.2'
    >>> magic.from_buffer(open("testdata/test.pdf").read(1024))
    'PDF document, version 1.2'
    >>> magic.from_file("testdata/test.pdf", mime=True)
    'application/pdf'