ImportError:无法导入名称“ safe_join”

时间:2019-02-05 15:51:59

标签: python django amazon-s3 boto3 django-storage

我想通过s3将我的静态文件上传到AWS CloudFront。 我遇到此错误:运行ImportError: cannot import name 'safe_join'

时发生python manage.py collectstatic

我正在使用Python 3.6和Django 2x。 django-storage-redux已安装,boto3botocore也已安装。

这是我的settings.py

import os
from django.utils.translation import ugettext_lazy as _
import boto3

...


# AWS CloudFront
AWS_S3_REGION_NAME = 'us-east-2'  # e.g. us-east-2
AWS_ACCESS_KEY_ID = '****'
AWS_SECRET_ACCESS_KEY = '***'
AWS_S3_HOST = 's3-us-east-2.amazonaws.com'
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=1209600, no-transform'
}

# static
BUCKET_STATIC_NAME = '***'
CLOUDFRONT_STATIC_DOMAIN = '***'

# media
BUCKET_MEDIA_NAME = '***'
CLOUDFRONT_MEDIA_DOMAIN = '***'

# storage
DEFAULT_FILE_STORAGE = 'elef.custom_storage.CachedStaticS3BotoStorage'
STATICFILES_STORAGE = 'elef.custom_storage.MediaS3BotoStorage'

MEDIA_URL = 'https://%s/' % CLOUDFRONT_MEDIA_DOMAIN

...

这是我的custom_storage.py

from storages.backends.s3boto3 import S3Boto3Storage
import boto3

from django.conf import settings
from django.contrib.staticfiles.storage import CachedFilesMixin


class CachedStaticS3BotoStorage(CachedFilesMixin, S3Boto3Storage):
    """
    S3BotoStorage backend which also saves a hashed copies of the files it saves.
    """
    bucket_name = settings.BUCKET_STATIC_NAME
    custom_domain = settings.CLOUDFRONT_STATIC_DOMAIN


class MediaS3BotoStorage(S3Boto3Storage):
    """
    S3BotoStorage backend for media files.
    """
    bucket_name = settings.BUCKET_MEDIA_NAME
    custom_domain = settings.CLOUDFRONT_MEDIA_DOMAIN

我还可以为您提供完整的追溯:

/Users/justine_dev/zapelef/lib/python3.6/site-packages/storages/__init__.py:9: UserWarning: This library has been designated as the official successor of django-storages and releases under that namespace. Please update your requirements files to point to django-storages.
  warnings.warn('This library has been designated as the official successor of django-storages and '
Traceback (most recent call last):
  File "manage.py", line 22, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/justine_dev/zapelef/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/justine_dev/zapelef/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Users/justine_dev/zapelef/lib/python3.6/site-packages/django/core/management/base.py", line 316, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/Users/justine_dev/zapelef/lib/python3.6/site-packages/django/core/management/base.py", line 353, in execute
    output = self.handle(*args, **options)
  File "/Users/justine_dev/zapelef/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 162, in handle
    if self.is_local_storage() and self.storage.location:
  File "/Users/justine_dev/zapelef/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 216, in is_local_storage
    return isinstance(self.storage, FileSystemStorage)
  File "/Users/justine_dev/zapelef/lib/python3.6/site-packages/django/utils/functional.py", line 213, in inner
    self._setup()
  File "/Users/justine_dev/zapelef/lib/python3.6/site-packages/django/contrib/staticfiles/storage.py", line 491, in _setup
    self._wrapped = get_storage_class(settings.STATICFILES_STORAGE)()
  File "/Users/justine_dev/zapelef/lib/python3.6/site-packages/django/core/files/storage.py", line 356, in get_storage_class
    return import_string(import_path or settings.DEFAULT_FILE_STORAGE)
  File "/Users/justine_dev/zapelef/lib/python3.6/site-packages/django/utils/module_loading.py", line 17, in import_string
    module = import_module(module_path)
  File "/Users/justine_dev/zapelef/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/justine_dev/Desktop/elefmarket/elef/custom_storage.py", line 1, in <module>
    from storages.backends.s3boto3 import S3Boto3Storage
  File "/Users/justine_dev/zapelef/lib/python3.6/site-packages/storages/backends/s3boto3.py", line 18, in <module>
    from storages.utils import safe_join, setting
ImportError: cannot import name 'safe_join'

我在回溯中看到我需要“更新我的需求文件以指向django-storages”。我不知道这是什么意思,并且还安装了django-storages

1 个答案:

答案 0 :(得分:0)

您不想使用django-storages-redux,警告是正确的,告诉您应该将其替换为django-storages。您的错误是由同时安装django-storagesdjango-storages-redux引起的。您应该删除-redux软件包,并通过单独重新安装django-storages来修复设置。

由于安装了不推荐使用的django-storages-redux project版本1.3.3,因此发出警告。这是only release with that warning。版本1.3.3相当旧,于2017年发布。

您需要卸载该项目,然后重新安装django-storages

pip uninstall django-storages-redux
pip install --force-reinstall django-storages

确保django-storages-redux未在您的requirements.txt文件中列出或未作为依赖项列出。

django-storages-reduxdjango-storages项目写入相同的程序包,并且发生冲突,从而创建了损坏的程序包。 storage.utils模块是the version from django-storages-redux,而storage.backends.s3boto3模块是new in version 1.5.0 of `django-storage