Aziz Alfoudari
Aziz Alfoudari

Reputation: 5263

Django - Error importing storages.backends

I have created a custom storage backend, the file is called storages.py and is placed in an app called core:

from django.conf import settings
from storages.backends.s3boto import S3BotoStorage

class S3StaticBucket(S3BotoStorage):
    def __init__(self, *args, **kwargs):
        kwargs['bucket_name'] = getattr(settings, 'static.mysite.com')
        super(S3BotoStorage, self).__init__(*args, **kwargs)

In settings.py, I have the follwing:

STATICFILES_STORAGE = 'core.storages.S3StaticBucket'
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

When I try to do python manage.py collectstatic it shows the following error:

django.core.exceptions.ImproperlyConfigured: Error importing storage module core.storages: "No module named backends.s3boto"

And when I run python manage.py shell and try to import the same:

>>> 
>>> from django.conf import settings
>>> from storages.backends.s3boto import S3BotoStorage
>>> 

Any idea what I'm doing wrong?

Upvotes: 9

Views: 8657

Answers (4)

Roby96
Roby96

Reputation: 3

Typo error. Change:

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'

TO:

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3Boto3Storage'

Upvotes: 0

Wojciech Żółtak
Wojciech Żółtak

Reputation: 950

There is a namespace conflict; the storage absolute name clashes with a storage local name. It may be unintuitive, but you can import from module in itself:

// file my_module/clash.py
import clash
print clash.__file__

Now we run python shell in a dir containing a my_module:

$ python
>>> import my_module.clash
my_module.clash.py

In short, your module tries to import a backend from itself.

You need an absolute import - Trying to import module with the same name as a built-in module causes an import error.

Upvotes: 8

dulacp
dulacp

Reputation: 1206

I had another type of issue that can help others, I used to have another file named storages.py but I deleted that file days ago, and still getting the Exception... the thing is I didn't had deleted the file storages.pyc !

Upvotes: 4

Parker
Parker

Reputation: 8851

I had this same issue, but for me it turns out that despite django-storages being installed, boto was not. A simple pip install boto fixed the error in my scenario.

Upvotes: 5

Related Questions