ImportError: cannot import name 'safe_join'

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-12 01:22:08

问题


I want to upload my static files to AWS CloudFront through s3. I have this error : ImportError: cannot import name 'safe_join' when running python manage.py collectstatic

I am using Python 3.6 and Django 2x. django-storage-redux is installed, boto3 and botocore too.

here is my 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

...

here is my 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

i can also give you the full traceback:

/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'

I can see in the traceback that i need to "update my requirements files to point to django-storages". i don't know what it means, and django-storages is also installed.


回答1:


You do not want to be using django-storages-redux, the warning is correct in telling you that you should replace it with django-storages. Your error was caused by having both django-storages and django-storages-redux installed at the same time. You should remove the -redux package, and repair your setup by re-installing django-storages, alone.

The warning is raised because you have version 1.3.3 of the deprecated django-storages-redux project installed; this is the only release with that warning. Version 1.3.3 is quite old, it was released in 2017.

You need to uninstall that project, and then re-install django-storages:

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

Make sure that django-storages-redux is not listed in your requirements.txt file or as a dependency anywhere.

The django-storages-redux and django-storages projects write to the same package, and they clashed, creating a broken package. The storage.utils module is the version from django-storages-redux, while the storage.backends.s3boto3 module is new in version 1.5.0 of `django-storage.



来源:https://stackoverflow.com/questions/54538239/importerror-cannot-import-name-safe-join

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!